github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/tflint/module_variable_test.go (about)

     1  package tflint
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/google/go-cmp/cmp/cmpopts"
     8  	hcl "github.com/hashicorp/hcl/v2"
     9  )
    10  
    11  func Test_roots(t *testing.T) {
    12  	cases := []struct {
    13  		Name     string
    14  		Var      *moduleVariable
    15  		Expected []*moduleVariable
    16  	}{
    17  		{
    18  			Name: "root only",
    19  			Var: &moduleVariable{
    20  				Root:      true,
    21  				Parents:   []*moduleVariable{},
    22  				DeclRange: hcl.Range{Filename: "foo.tf"},
    23  			},
    24  			Expected: []*moduleVariable{
    25  				{
    26  					Root:      true,
    27  					Parents:   []*moduleVariable{},
    28  					DeclRange: hcl.Range{Filename: "foo.tf"},
    29  				},
    30  			},
    31  		},
    32  		{
    33  			Name: "all roots",
    34  			Var: &moduleVariable{
    35  				Root: false,
    36  				Parents: []*moduleVariable{
    37  					{
    38  						Root:      true,
    39  						Parents:   []*moduleVariable{},
    40  						DeclRange: hcl.Range{Filename: "bar.tf"},
    41  					},
    42  					{
    43  						Root:      false,
    44  						Parents:   []*moduleVariable{},
    45  						DeclRange: hcl.Range{Filename: "bar.tf"},
    46  					},
    47  					{
    48  						Root: false,
    49  						Parents: []*moduleVariable{
    50  							{
    51  								Root:      true,
    52  								Parents:   []*moduleVariable{},
    53  								DeclRange: hcl.Range{Filename: "baz.tf"},
    54  							},
    55  						},
    56  						DeclRange: hcl.Range{Filename: "bar.tf"},
    57  					},
    58  				},
    59  				DeclRange: hcl.Range{Filename: "foo.tf"},
    60  			},
    61  			Expected: []*moduleVariable{
    62  				{
    63  					Root:      true,
    64  					Parents:   []*moduleVariable{},
    65  					DeclRange: hcl.Range{Filename: "bar.tf"},
    66  				},
    67  				{
    68  					Root:      true,
    69  					Parents:   []*moduleVariable{},
    70  					DeclRange: hcl.Range{Filename: "baz.tf"},
    71  				},
    72  			},
    73  		},
    74  	}
    75  
    76  	for _, tc := range cases {
    77  		ret := tc.Var.roots()
    78  		opts := []cmp.Option{cmpopts.IgnoreFields(moduleVariable{}, "Callers")}
    79  		if !cmp.Equal(ret, tc.Expected, opts...) {
    80  			t.Fatalf("Failed `%s` test: diff=%s", tc.Name, cmp.Diff(ret, tc.Expected, opts...))
    81  		}
    82  	}
    83  }
    84  
    85  func Test_callers(t *testing.T) {
    86  	cases := []struct {
    87  		Name     string
    88  		Var      *moduleVariable
    89  		Expected []hcl.Range
    90  	}{
    91  		{
    92  			Name: "root only",
    93  			Var: &moduleVariable{
    94  				Root:      true,
    95  				Parents:   []*moduleVariable{},
    96  				DeclRange: hcl.Range{Filename: "foo.tf"},
    97  			},
    98  			Expected: []hcl.Range{
    99  				{Filename: "foo.tf"},
   100  			},
   101  		},
   102  		{
   103  			Name: "all roots",
   104  			Var: &moduleVariable{
   105  				Root: false,
   106  				Parents: []*moduleVariable{
   107  					{
   108  						Root: false,
   109  						Parents: []*moduleVariable{
   110  							{
   111  								Root:      true,
   112  								Parents:   []*moduleVariable{},
   113  								DeclRange: hcl.Range{Filename: "baz.tf"},
   114  							},
   115  						},
   116  						DeclRange: hcl.Range{Filename: "bar.tf"},
   117  					},
   118  				},
   119  				DeclRange: hcl.Range{Filename: "foo.tf"},
   120  			},
   121  			Expected: []hcl.Range{
   122  				{Filename: "baz.tf"},
   123  				{Filename: "bar.tf"},
   124  				{Filename: "foo.tf"},
   125  			},
   126  		},
   127  	}
   128  
   129  	for _, tc := range cases {
   130  		roots := tc.Var.roots()
   131  		if len(roots) != 1 {
   132  			t.Fatalf("Failed `%s` test: expected 1 root, but get `%d` roots", tc.Name, len(roots))
   133  		}
   134  
   135  		ret := roots[0].callers()
   136  		if !cmp.Equal(ret, tc.Expected) {
   137  			t.Fatalf("Failed `%s` test: diff=%s", tc.Name, cmp.Diff(ret, tc.Expected))
   138  		}
   139  	}
   140  }