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

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/davecgh/go-spew/spew"
     8  	"github.com/hashicorp/hcl/v2"
     9  	"github.com/hashicorp/hcl/v2/hcltest"
    10  	"github.com/hashicorp/terraform/internal/lang/marks"
    11  	"github.com/zclconf/go-cty/cty"
    12  )
    13  
    14  func TestEvaluateCountExpression(t *testing.T) {
    15  	tests := map[string]struct {
    16  		Expr  hcl.Expression
    17  		Count int
    18  	}{
    19  		"zero": {
    20  			hcltest.MockExprLiteral(cty.NumberIntVal(0)),
    21  			0,
    22  		},
    23  		"expression with marked value": {
    24  			hcltest.MockExprLiteral(cty.NumberIntVal(8).Mark(marks.Sensitive)),
    25  			8,
    26  		},
    27  	}
    28  	for name, test := range tests {
    29  		t.Run(name, func(t *testing.T) {
    30  			ctx := &MockEvalContext{}
    31  			ctx.installSimpleEval()
    32  			countVal, diags := evaluateCountExpression(test.Expr, ctx)
    33  
    34  			if len(diags) != 0 {
    35  				t.Errorf("unexpected diagnostics %s", spew.Sdump(diags))
    36  			}
    37  
    38  			if !reflect.DeepEqual(countVal, test.Count) {
    39  				t.Errorf(
    40  					"wrong map value\ngot:  %swant: %s",
    41  					spew.Sdump(countVal), spew.Sdump(test.Count),
    42  				)
    43  			}
    44  		})
    45  	}
    46  }