github.com/opentofu/opentofu@v1.7.1/internal/command/jsonconfig/expression_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package jsonconfig 7 8 import ( 9 "encoding/json" 10 "reflect" 11 "testing" 12 13 "github.com/zclconf/go-cty/cty" 14 15 "github.com/hashicorp/hcl/v2" 16 "github.com/hashicorp/hcl/v2/hclsyntax" 17 "github.com/hashicorp/hcl/v2/hcltest" 18 "github.com/opentofu/opentofu/internal/configs/configschema" 19 ) 20 21 func TestMarshalExpressions(t *testing.T) { 22 tests := []struct { 23 Input hcl.Body 24 Want expressions 25 }{ 26 { 27 &hclsyntax.Body{ 28 Attributes: hclsyntax.Attributes{ 29 "foo": &hclsyntax.Attribute{ 30 Expr: &hclsyntax.LiteralValueExpr{ 31 Val: cty.StringVal("bar"), 32 }, 33 }, 34 }, 35 }, 36 expressions{ 37 "foo": expression{ 38 ConstantValue: json.RawMessage([]byte(`"bar"`)), 39 References: []string(nil), 40 }, 41 }, 42 }, 43 { 44 hcltest.MockBody(&hcl.BodyContent{ 45 Attributes: hcl.Attributes{ 46 "foo": { 47 Name: "foo", 48 Expr: hcltest.MockExprTraversalSrc(`var.list[1]`), 49 }, 50 }, 51 }), 52 expressions{ 53 "foo": expression{ 54 References: []string{"var.list[1]", "var.list"}, 55 }, 56 }, 57 }, 58 { 59 hcltest.MockBody(&hcl.BodyContent{ 60 Attributes: hcl.Attributes{ 61 "foo": { 62 Name: "foo", 63 Expr: hcltest.MockExprTraversalSrc(`data.template_file.foo[1].vars["baz"]`), 64 }, 65 }, 66 }), 67 expressions{ 68 "foo": expression{ 69 References: []string{"data.template_file.foo[1].vars[\"baz\"]", "data.template_file.foo[1].vars", "data.template_file.foo[1]", "data.template_file.foo"}, 70 }, 71 }, 72 }, 73 { 74 hcltest.MockBody(&hcl.BodyContent{ 75 Attributes: hcl.Attributes{ 76 "foo": { 77 Name: "foo", 78 Expr: hcltest.MockExprTraversalSrc(`module.foo.bar`), 79 }, 80 }, 81 }), 82 expressions{ 83 "foo": expression{ 84 References: []string{"module.foo.bar", "module.foo"}, 85 }, 86 }, 87 }, 88 { 89 hcltest.MockBody(&hcl.BodyContent{ 90 Blocks: hcl.Blocks{ 91 { 92 Type: "block_to_attr", 93 Body: hcltest.MockBody(&hcl.BodyContent{ 94 95 Attributes: hcl.Attributes{ 96 "foo": { 97 Name: "foo", 98 Expr: hcltest.MockExprTraversalSrc(`module.foo.bar`), 99 }, 100 }, 101 }), 102 }, 103 }, 104 }), 105 expressions{ 106 "block_to_attr": expression{ 107 References: []string{"module.foo.bar", "module.foo"}, 108 }, 109 }, 110 }, 111 } 112 113 for _, test := range tests { 114 schema := &configschema.Block{ 115 Attributes: map[string]*configschema.Attribute{ 116 "foo": { 117 Type: cty.String, 118 Optional: true, 119 }, 120 "block_to_attr": { 121 Type: cty.List(cty.Object(map[string]cty.Type{ 122 "foo": cty.String, 123 })), 124 }, 125 }, 126 } 127 128 got := marshalExpressions(test.Input, schema) 129 if !reflect.DeepEqual(got, test.Want) { 130 t.Errorf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want) 131 } 132 } 133 } 134 135 func TestMarshalExpression(t *testing.T) { 136 tests := []struct { 137 Input hcl.Expression 138 Want expression 139 }{ 140 { 141 nil, 142 expression{}, 143 }, 144 } 145 146 for _, test := range tests { 147 got := marshalExpression(test.Input) 148 if !reflect.DeepEqual(got, test.Want) { 149 t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want) 150 } 151 } 152 }