github.com/hashicorp/hcl/v2@v2.20.0/hclsyntax/expression_static_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package hclsyntax
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/go-test/deep"
    10  	"github.com/hashicorp/hcl/v2"
    11  	"github.com/zclconf/go-cty/cty"
    12  )
    13  
    14  func TestTraversalStatic(t *testing.T) {
    15  	expr, diags := ParseExpression([]byte(`a.b.c`), "", hcl.Pos{Line: 1, Column: 1})
    16  	got, moreDiags := hcl.AbsTraversalForExpr(expr)
    17  	diags = append(diags, moreDiags...)
    18  
    19  	if len(diags) != 0 {
    20  		t.Errorf("wrong number of diags %d; want 0", len(diags))
    21  		for _, diag := range diags {
    22  			t.Logf("- %s", diag)
    23  		}
    24  		return
    25  	}
    26  
    27  	want := hcl.Traversal{
    28  		hcl.TraverseRoot{
    29  			Name: "a",
    30  			SrcRange: hcl.Range{
    31  				Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
    32  				End:   hcl.Pos{Line: 1, Column: 2, Byte: 1},
    33  			},
    34  		},
    35  		hcl.TraverseAttr{
    36  			Name: "b",
    37  			SrcRange: hcl.Range{
    38  				Start: hcl.Pos{Line: 1, Column: 2, Byte: 1},
    39  				End:   hcl.Pos{Line: 1, Column: 4, Byte: 3},
    40  			},
    41  		},
    42  		hcl.TraverseAttr{
    43  			Name: "c",
    44  			SrcRange: hcl.Range{
    45  				Start: hcl.Pos{Line: 1, Column: 4, Byte: 3},
    46  				End:   hcl.Pos{Line: 1, Column: 6, Byte: 5},
    47  			},
    48  		},
    49  	}
    50  
    51  	for _, problem := range deep.Equal(got, want) {
    52  		t.Errorf(problem)
    53  	}
    54  }
    55  
    56  func TestTupleStatic(t *testing.T) {
    57  	expr, diags := ParseExpression([]byte(`[true, false]`), "", hcl.Pos{Line: 1, Column: 1})
    58  	exprs, moreDiags := hcl.ExprList(expr)
    59  	diags = append(diags, moreDiags...)
    60  	if len(diags) != 0 {
    61  		t.Errorf("wrong number of diags %d; want 0", len(diags))
    62  		for _, diag := range diags {
    63  			t.Logf("- %s", diag)
    64  		}
    65  		return
    66  	}
    67  
    68  	if got, want := len(exprs), 2; got != want {
    69  		t.Fatalf("wrong length %d; want %d", got, want)
    70  	}
    71  
    72  	got := make([]cty.Value, len(exprs))
    73  	want := []cty.Value{
    74  		cty.True,
    75  		cty.False,
    76  	}
    77  	for i, itemExpr := range exprs {
    78  		val, valDiags := itemExpr.Value(nil)
    79  		if len(valDiags) != 0 {
    80  			t.Errorf("wrong number of diags %d; want 0", len(valDiags))
    81  			for _, diag := range valDiags {
    82  				t.Logf("- %s", diag)
    83  			}
    84  			return
    85  		}
    86  		got[i] = val
    87  	}
    88  
    89  	for _, problem := range deep.Equal(got, want) {
    90  		t.Errorf(problem)
    91  	}
    92  }
    93  
    94  func TestMapStatic(t *testing.T) {
    95  	expr, diags := ParseExpression([]byte(`{"foo":true,"bar":false}`), "", hcl.Pos{Line: 1, Column: 1})
    96  	items, moreDiags := hcl.ExprMap(expr)
    97  	diags = append(diags, moreDiags...)
    98  	if len(diags) != 0 {
    99  		t.Errorf("wrong number of diags %d; want 0", len(diags))
   100  		for _, diag := range diags {
   101  			t.Logf("- %s", diag)
   102  		}
   103  		return
   104  	}
   105  
   106  	if got, want := len(items), 2; got != want {
   107  		t.Fatalf("wrong length %d; want %d", got, want)
   108  	}
   109  
   110  	got := make(map[cty.Value]cty.Value)
   111  	want := map[cty.Value]cty.Value{
   112  		cty.StringVal("foo"): cty.True,
   113  		cty.StringVal("bar"): cty.False,
   114  	}
   115  	for _, item := range items {
   116  		var itemDiags hcl.Diagnostics
   117  		key, keyDiags := item.Key.Value(nil)
   118  		itemDiags = append(itemDiags, keyDiags...)
   119  		val, valDiags := item.Value.Value(nil)
   120  		itemDiags = append(itemDiags, valDiags...)
   121  		if len(itemDiags) != 0 {
   122  			t.Errorf("wrong number of diags %d; want 0", len(itemDiags))
   123  			for _, diag := range itemDiags {
   124  				t.Logf("- %s", diag)
   125  			}
   126  			return
   127  		}
   128  		got[key] = val
   129  	}
   130  
   131  	for _, problem := range deep.Equal(got, want) {
   132  		t.Errorf(problem)
   133  	}
   134  }