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