github.com/pulumi/terraform@v1.4.0/pkg/command/jsonformat/differ/attribute_path/matcher_test.go (about)

     1  package attribute_path
     2  
     3  import "testing"
     4  
     5  func TestPathMatcher_FollowsPath(t *testing.T) {
     6  	var matcher Matcher
     7  
     8  	matcher = &PathMatcher{
     9  		Paths: [][]interface{}{
    10  			{
    11  				float64(0),
    12  				"key",
    13  				float64(0),
    14  			},
    15  		},
    16  	}
    17  
    18  	if matcher.Matches() {
    19  		t.Errorf("should not have exact matched at base level")
    20  	}
    21  	if !matcher.MatchesPartial() {
    22  		t.Errorf("should have partial matched at base level")
    23  	}
    24  
    25  	matcher = matcher.GetChildWithIndex(0)
    26  
    27  	if matcher.Matches() {
    28  		t.Errorf("should not have exact matched at first level")
    29  	}
    30  	if !matcher.MatchesPartial() {
    31  		t.Errorf("should have partial matched at first level")
    32  	}
    33  
    34  	matcher = matcher.GetChildWithKey("key")
    35  
    36  	if matcher.Matches() {
    37  		t.Errorf("should not have exact matched at second level")
    38  	}
    39  	if !matcher.MatchesPartial() {
    40  		t.Errorf("should have partial matched at second level")
    41  	}
    42  
    43  	matcher = matcher.GetChildWithIndex(0)
    44  
    45  	if !matcher.Matches() {
    46  		t.Errorf("should have exact matched at leaf level")
    47  	}
    48  	if !matcher.MatchesPartial() {
    49  		t.Errorf("should have partial matched at leaf level")
    50  	}
    51  }
    52  func TestPathMatcher_Propagates(t *testing.T) {
    53  	var matcher Matcher
    54  
    55  	matcher = &PathMatcher{
    56  		Paths: [][]interface{}{
    57  			{
    58  				float64(0),
    59  				"key",
    60  			},
    61  		},
    62  		Propagate: true,
    63  	}
    64  
    65  	if matcher.Matches() {
    66  		t.Errorf("should not have exact matched at base level")
    67  	}
    68  	if !matcher.MatchesPartial() {
    69  		t.Errorf("should have partial matched at base level")
    70  	}
    71  
    72  	matcher = matcher.GetChildWithIndex(0)
    73  
    74  	if matcher.Matches() {
    75  		t.Errorf("should not have exact matched at first level")
    76  	}
    77  	if !matcher.MatchesPartial() {
    78  		t.Errorf("should have partial matched at first level")
    79  	}
    80  
    81  	matcher = matcher.GetChildWithKey("key")
    82  
    83  	if !matcher.Matches() {
    84  		t.Errorf("should have exact matched at second level")
    85  	}
    86  	if !matcher.MatchesPartial() {
    87  		t.Errorf("should have partial matched at second level")
    88  	}
    89  
    90  	matcher = matcher.GetChildWithIndex(0)
    91  
    92  	if !matcher.Matches() {
    93  		t.Errorf("should have exact matched at leaf level")
    94  	}
    95  	if !matcher.MatchesPartial() {
    96  		t.Errorf("should have partial matched at leaf level")
    97  	}
    98  }
    99  func TestPathMatcher_DoesNotPropagate(t *testing.T) {
   100  	var matcher Matcher
   101  
   102  	matcher = &PathMatcher{
   103  		Paths: [][]interface{}{
   104  			{
   105  				float64(0),
   106  				"key",
   107  			},
   108  		},
   109  	}
   110  
   111  	if matcher.Matches() {
   112  		t.Errorf("should not have exact matched at base level")
   113  	}
   114  	if !matcher.MatchesPartial() {
   115  		t.Errorf("should have partial matched at base level")
   116  	}
   117  
   118  	matcher = matcher.GetChildWithIndex(0)
   119  
   120  	if matcher.Matches() {
   121  		t.Errorf("should not have exact matched at first level")
   122  	}
   123  	if !matcher.MatchesPartial() {
   124  		t.Errorf("should have partial matched at first level")
   125  	}
   126  
   127  	matcher = matcher.GetChildWithKey("key")
   128  
   129  	if !matcher.Matches() {
   130  		t.Errorf("should have exact matched at second level")
   131  	}
   132  	if !matcher.MatchesPartial() {
   133  		t.Errorf("should have partial matched at second level")
   134  	}
   135  
   136  	matcher = matcher.GetChildWithIndex(0)
   137  
   138  	if matcher.Matches() {
   139  		t.Errorf("should not have exact matched at leaf level")
   140  	}
   141  	if matcher.MatchesPartial() {
   142  		t.Errorf("should not have partial matched at leaf level")
   143  	}
   144  }
   145  
   146  func TestPathMatcher_BreaksPath(t *testing.T) {
   147  	var matcher Matcher
   148  
   149  	matcher = &PathMatcher{
   150  		Paths: [][]interface{}{
   151  			{
   152  				float64(0),
   153  				"key",
   154  				float64(0),
   155  			},
   156  		},
   157  	}
   158  
   159  	if matcher.Matches() {
   160  		t.Errorf("should not have exact matched at base level")
   161  	}
   162  	if !matcher.MatchesPartial() {
   163  		t.Errorf("should have partial matched at base level")
   164  	}
   165  
   166  	matcher = matcher.GetChildWithIndex(0)
   167  
   168  	if matcher.Matches() {
   169  		t.Errorf("should not have exact matched at first level")
   170  	}
   171  	if !matcher.MatchesPartial() {
   172  		t.Errorf("should have partial matched at first level")
   173  	}
   174  
   175  	matcher = matcher.GetChildWithKey("invalid")
   176  
   177  	if matcher.Matches() {
   178  		t.Errorf("should not have exact matched at second level")
   179  	}
   180  	if matcher.MatchesPartial() {
   181  		t.Errorf("should not have partial matched at second level")
   182  
   183  	}
   184  }
   185  
   186  func TestPathMatcher_MultiplePaths(t *testing.T) {
   187  	var matcher Matcher
   188  
   189  	matcher = &PathMatcher{
   190  		Paths: [][]interface{}{
   191  			{
   192  				float64(0),
   193  				"key",
   194  				float64(0),
   195  			},
   196  			{
   197  				float64(0),
   198  				"key",
   199  				float64(1),
   200  			},
   201  		},
   202  	}
   203  
   204  	if matcher.Matches() {
   205  		t.Errorf("should not have exact matched at base level")
   206  	}
   207  	if !matcher.MatchesPartial() {
   208  		t.Errorf("should have partial matched at base level")
   209  	}
   210  
   211  	matcher = matcher.GetChildWithIndex(0)
   212  
   213  	if matcher.Matches() {
   214  		t.Errorf("should not have exact matched at first level")
   215  	}
   216  	if !matcher.MatchesPartial() {
   217  		t.Errorf("should have partial matched at first level")
   218  	}
   219  
   220  	matcher = matcher.GetChildWithKey("key")
   221  
   222  	if matcher.Matches() {
   223  		t.Errorf("should not have exact matched at second level")
   224  	}
   225  	if !matcher.MatchesPartial() {
   226  		t.Errorf("should have partial matched at second level")
   227  	}
   228  
   229  	validZero := matcher.GetChildWithIndex(0)
   230  	validOne := matcher.GetChildWithIndex(1)
   231  	invalid := matcher.GetChildWithIndex(2)
   232  
   233  	if !validZero.Matches() {
   234  		t.Errorf("should have exact matched at leaf level")
   235  	}
   236  	if !validZero.MatchesPartial() {
   237  		t.Errorf("should have partial matched at leaf level")
   238  	}
   239  
   240  	if !validOne.Matches() {
   241  		t.Errorf("should have exact matched at leaf level")
   242  	}
   243  	if !validOne.MatchesPartial() {
   244  		t.Errorf("should have partial matched at leaf level")
   245  	}
   246  
   247  	if invalid.Matches() {
   248  		t.Errorf("should not have exact matched at leaf level")
   249  	}
   250  	if invalid.MatchesPartial() {
   251  		t.Errorf("should not have partial matched at leaf level")
   252  	}
   253  }