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

     1  package configs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"github.com/zclconf/go-cty/cty"
     8  )
     9  
    10  func TestSynthBodyContent(t *testing.T) {
    11  	tests := map[string]struct {
    12  		Values    map[string]cty.Value
    13  		Schema    *hcl.BodySchema
    14  		DiagCount int
    15  	}{
    16  		"empty": {
    17  			Values:    map[string]cty.Value{},
    18  			Schema:    &hcl.BodySchema{},
    19  			DiagCount: 0,
    20  		},
    21  		"missing required attribute": {
    22  			Values: map[string]cty.Value{},
    23  			Schema: &hcl.BodySchema{
    24  				Attributes: []hcl.AttributeSchema{
    25  					{
    26  						Name:     "nonexist",
    27  						Required: true,
    28  					},
    29  				},
    30  			},
    31  			DiagCount: 1, // missing required attribute
    32  		},
    33  		"missing optional attribute": {
    34  			Values: map[string]cty.Value{},
    35  			Schema: &hcl.BodySchema{
    36  				Attributes: []hcl.AttributeSchema{
    37  					{
    38  						Name: "nonexist",
    39  					},
    40  				},
    41  			},
    42  			DiagCount: 0,
    43  		},
    44  		"extraneous attribute": {
    45  			Values: map[string]cty.Value{
    46  				"foo": cty.StringVal("unwanted"),
    47  			},
    48  			Schema:    &hcl.BodySchema{},
    49  			DiagCount: 1, // unsupported attribute
    50  		},
    51  	}
    52  
    53  	for name, test := range tests {
    54  		t.Run(name, func(t *testing.T) {
    55  			body := SynthBody("synth", test.Values)
    56  			_, diags := body.Content(test.Schema)
    57  			if got, want := len(diags), test.DiagCount; got != want {
    58  				t.Errorf("wrong number of diagnostics %d; want %d", got, want)
    59  				for _, diag := range diags {
    60  					t.Logf("- %s", diag)
    61  				}
    62  			}
    63  		})
    64  	}
    65  }