github.com/terraform-linters/tflint-plugin-sdk@v0.22.0/hclext/schema_test.go (about)

     1  package hclext
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  )
     8  
     9  func TestImpliedBodySchema(t *testing.T) {
    10  	tests := []struct {
    11  		Name string
    12  		Val  interface{}
    13  		Want *BodySchema
    14  	}{
    15  		{
    16  			Name: "empty struct",
    17  			Val:  struct{}{},
    18  			Want: &BodySchema{},
    19  		},
    20  		{
    21  			Name: "struct without tags",
    22  			Val: struct {
    23  				Ignored bool
    24  			}{},
    25  			Want: &BodySchema{},
    26  		},
    27  		{
    28  			Name: "attribute tags",
    29  			Val: struct {
    30  				Attr1 bool `hclext:"attr1"`
    31  				Attr2 bool `hclext:"attr2"`
    32  			}{},
    33  			Want: &BodySchema{
    34  				Attributes: []AttributeSchema{
    35  					{
    36  						Name:     "attr1",
    37  						Required: true,
    38  					},
    39  					{
    40  						Name:     "attr2",
    41  						Required: true,
    42  					},
    43  				},
    44  			},
    45  		},
    46  		{
    47  			Name: "pointer attribute tags",
    48  			Val: struct {
    49  				Attr *bool `hclext:"attr,attr"`
    50  			}{},
    51  			Want: &BodySchema{
    52  				Attributes: []AttributeSchema{
    53  					{
    54  						Name:     "attr",
    55  						Required: false,
    56  					},
    57  				},
    58  			},
    59  		},
    60  		{
    61  			Name: "optional attribute tags",
    62  			Val: struct {
    63  				Attr bool `hclext:"attr,optional"`
    64  			}{},
    65  			Want: &BodySchema{
    66  				Attributes: []AttributeSchema{
    67  					{
    68  						Name:     "attr",
    69  						Required: false,
    70  					},
    71  				},
    72  			},
    73  		},
    74  		{
    75  			Name: "block tags",
    76  			Val: struct {
    77  				Thing struct{} `hclext:"thing,block"`
    78  			}{},
    79  			Want: &BodySchema{
    80  				Blocks: []BlockSchema{
    81  					{
    82  						Type: "thing",
    83  						Body: &BodySchema{},
    84  					},
    85  				},
    86  			},
    87  		},
    88  		{
    89  			Name: "block tags with labels",
    90  			Val: struct {
    91  				Thing struct {
    92  					Type string `hclext:"type,label"`
    93  					Name string `hclext:"name,label"`
    94  				} `hclext:"thing,block"`
    95  			}{},
    96  			Want: &BodySchema{
    97  				Blocks: []BlockSchema{
    98  					{
    99  						Type:       "thing",
   100  						LabelNames: []string{"type", "name"},
   101  						Body:       &BodySchema{},
   102  					},
   103  				},
   104  			},
   105  		},
   106  		{
   107  			Name: "multiple block tags with labels",
   108  			Val: struct {
   109  				Thing []struct {
   110  					Type string `hclext:"type,label"`
   111  					Name string `hclext:"name,label"`
   112  				} `hclext:"thing,block"`
   113  			}{},
   114  			Want: &BodySchema{
   115  				Blocks: []BlockSchema{
   116  					{
   117  						Type:       "thing",
   118  						LabelNames: []string{"type", "name"},
   119  						Body:       &BodySchema{},
   120  					},
   121  				},
   122  			},
   123  		},
   124  		{
   125  			Name: "pointer block tags with labels",
   126  			Val: struct {
   127  				Thing *struct {
   128  					Type string `hclext:"type,label"`
   129  					Name string `hclext:"name,label"`
   130  				} `hclext:"thing,block"`
   131  			}{},
   132  			Want: &BodySchema{
   133  				Blocks: []BlockSchema{
   134  					{
   135  						Type:       "thing",
   136  						LabelNames: []string{"type", "name"},
   137  						Body:       &BodySchema{},
   138  					},
   139  				},
   140  			},
   141  		},
   142  		{
   143  			Name: "nested block tags with labels",
   144  			Val: struct {
   145  				Thing struct {
   146  					Name      string `hclext:"name,label"`
   147  					Something string `hclext:"something"`
   148  				} `hclext:"thing,block"`
   149  			}{},
   150  			Want: &BodySchema{
   151  				Blocks: []BlockSchema{
   152  					{
   153  						Type:       "thing",
   154  						LabelNames: []string{"name"},
   155  						Body: &BodySchema{
   156  							Attributes: []AttributeSchema{
   157  								{
   158  									Name:     "something",
   159  									Required: true,
   160  								},
   161  							},
   162  						},
   163  					},
   164  				},
   165  			},
   166  		},
   167  		{
   168  			Name: "attribute/block tags with labels",
   169  			Val: struct {
   170  				Doodad string `hclext:"doodad"`
   171  				Thing  struct {
   172  					Name string `hclext:"name,label"`
   173  				} `hclext:"thing,block"`
   174  			}{},
   175  			Want: &BodySchema{
   176  				Attributes: []AttributeSchema{
   177  					{
   178  						Name:     "doodad",
   179  						Required: true,
   180  					},
   181  				},
   182  				Blocks: []BlockSchema{
   183  					{
   184  						Type:       "thing",
   185  						LabelNames: []string{"name"},
   186  						Body:       &BodySchema{},
   187  					},
   188  				},
   189  			},
   190  		},
   191  	}
   192  
   193  	for _, test := range tests {
   194  		t.Run(test.Name, func(t *testing.T) {
   195  			got := ImpliedBodySchema(test.Val)
   196  			if diff := cmp.Diff(test.Want, got); diff != "" {
   197  				t.Errorf("wrong schema\ndiff:  %s", diff)
   198  			}
   199  		})
   200  	}
   201  }