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