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