github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/eval_output_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform-plugin-sdk/internal/states"
     7  
     8  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     9  	"github.com/zclconf/go-cty/cty"
    10  )
    11  
    12  func TestEvalWriteMapOutput(t *testing.T) {
    13  	ctx := new(MockEvalContext)
    14  	ctx.StateState = states.NewState().SyncWrapper()
    15  
    16  	cases := []struct {
    17  		name string
    18  		val  cty.Value
    19  		err  bool
    20  	}{
    21  		{
    22  			// Eval should recognize a single map in a slice, and collapse it
    23  			// into the map value
    24  			"single-map",
    25  			cty.MapVal(map[string]cty.Value{
    26  				"a": cty.StringVal("b"),
    27  			}),
    28  			false,
    29  		},
    30  		{
    31  			// we can't apply a multi-valued map to a variable, so this should error
    32  			"multi-map",
    33  			cty.ListVal([]cty.Value{
    34  				cty.MapVal(map[string]cty.Value{
    35  					"a": cty.StringVal("b"),
    36  				}),
    37  				cty.MapVal(map[string]cty.Value{
    38  					"c": cty.StringVal("d"),
    39  				}),
    40  			}),
    41  			true,
    42  		},
    43  	}
    44  
    45  	for _, tc := range cases {
    46  		evalNode := &EvalWriteOutput{
    47  			Addr: addrs.OutputValue{Name: tc.name},
    48  		}
    49  		ctx.EvaluateExprResult = tc.val
    50  		t.Run(tc.name, func(t *testing.T) {
    51  			_, err := evalNode.Eval(ctx)
    52  			if err != nil && !tc.err {
    53  				t.Fatal(err)
    54  			}
    55  		})
    56  	}
    57  }