github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/command/jsonprovider/block_test.go (about)

     1  package jsonprovider
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/zclconf/go-cty/cty"
     9  
    10  	"github.com/hashicorp/terraform/internal/configs/configschema"
    11  )
    12  
    13  func TestMarshalBlock(t *testing.T) {
    14  	tests := []struct {
    15  		Input *configschema.Block
    16  		Want  *block
    17  	}{
    18  		{
    19  			nil,
    20  			&block{},
    21  		},
    22  		{
    23  			Input: &configschema.Block{
    24  				Attributes: map[string]*configschema.Attribute{
    25  					"id":  {Type: cty.String, Optional: true, Computed: true},
    26  					"ami": {Type: cty.String, Optional: true},
    27  				},
    28  				BlockTypes: map[string]*configschema.NestedBlock{
    29  					"network_interface": {
    30  						Nesting: configschema.NestingList,
    31  						Block: configschema.Block{
    32  							Attributes: map[string]*configschema.Attribute{
    33  								"device_index": {Type: cty.String, Optional: true},
    34  								"description":  {Type: cty.String, Optional: true},
    35  							},
    36  						},
    37  					},
    38  				},
    39  			},
    40  			Want: &block{
    41  				Attributes: map[string]*attribute{
    42  					"ami": {AttributeType: json.RawMessage(`"string"`), Optional: true, DescriptionKind: "plain"},
    43  					"id":  {AttributeType: json.RawMessage(`"string"`), Optional: true, Computed: true, DescriptionKind: "plain"},
    44  				},
    45  				BlockTypes: map[string]*blockType{
    46  					"network_interface": {
    47  						NestingMode: "list",
    48  						Block: &block{
    49  							Attributes: map[string]*attribute{
    50  								"description":  {AttributeType: json.RawMessage(`"string"`), Optional: true, DescriptionKind: "plain"},
    51  								"device_index": {AttributeType: json.RawMessage(`"string"`), Optional: true, DescriptionKind: "plain"},
    52  							},
    53  							DescriptionKind: "plain",
    54  						},
    55  					},
    56  				},
    57  				DescriptionKind: "plain",
    58  			},
    59  		},
    60  	}
    61  
    62  	for _, test := range tests {
    63  		got := marshalBlock(test.Input)
    64  		if !cmp.Equal(got, test.Want) {
    65  			t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, test.Want))
    66  		}
    67  	}
    68  }