github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonconfig/expression_test.go (about)

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