|
| 1 | +package dyn_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/databricks/cli/libs/dyn" |
| 7 | + assert "github.com/databricks/cli/libs/dyn/dynassert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestPatternTrie_SearchPath(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + pattern string |
| 14 | + mustMatch []string |
| 15 | + mustNotMatch []string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "empty pattern", |
| 19 | + pattern: "", |
| 20 | + mustMatch: []string{""}, |
| 21 | + mustNotMatch: []string{"foo"}, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "simple key pattern", |
| 25 | + pattern: "foo", |
| 26 | + mustMatch: []string{"foo"}, |
| 27 | + mustNotMatch: []string{"foo.bar", "foo[0]", "bar"}, |
| 28 | + }, |
| 29 | + |
| 30 | + { |
| 31 | + name: "nested key pattern", |
| 32 | + pattern: "foo.bar", |
| 33 | + mustMatch: []string{"foo.bar"}, |
| 34 | + mustNotMatch: []string{"foo", "foo[0]", "bar.foo", "foo.baz"}, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "root wildcard", |
| 38 | + pattern: "*", |
| 39 | + mustMatch: []string{"foo", "bar"}, |
| 40 | + mustNotMatch: []string{"", "bar.foo", "foo.baz"}, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "wildcard * after foo", |
| 44 | + pattern: "foo.*", |
| 45 | + mustMatch: []string{"foo.bar", "foo.baz"}, |
| 46 | + mustNotMatch: []string{"foo", "bar", "foo.bar.baz"}, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "wildcard [*] after foo", |
| 50 | + pattern: "foo[*]", |
| 51 | + mustMatch: []string{"foo[0]", "foo[1]", "foo[2025]"}, |
| 52 | + mustNotMatch: []string{"foo", "bar", "foo[0].bar"}, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "key after * wildcard", |
| 56 | + pattern: "foo.*.bar", |
| 57 | + mustMatch: []string{"foo.abc.bar", "foo.def.bar"}, |
| 58 | + mustNotMatch: []string{"foo", "bar", "foo.bar.baz"}, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "key after [*] wildcard", |
| 62 | + pattern: "foo[*].bar", |
| 63 | + mustMatch: []string{"foo[0].bar", "foo[1].bar", "foo[2025].bar"}, |
| 64 | + mustNotMatch: []string{"foo", "bar", "foo[0].baz"}, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "multiple * wildcards", |
| 68 | + pattern: "*.*.*", |
| 69 | + mustMatch: []string{"foo.bar.baz", "foo.bar.qux"}, |
| 70 | + mustNotMatch: []string{"foo", "bar", "foo.bar", "foo.bar.baz.qux"}, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "multiple [*] wildcards", |
| 74 | + pattern: "foo[*][*]", |
| 75 | + mustMatch: []string{"foo[0][0]", "foo[1][1]", "foo[2025][2025]"}, |
| 76 | + mustNotMatch: []string{"foo", "bar", "foo[0][0][0]"}, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "[*] after * wildcard", |
| 80 | + pattern: "*[*]", |
| 81 | + mustMatch: []string{"foo[0]", "foo[1]", "foo[2025]"}, |
| 82 | + mustNotMatch: []string{"foo", "bar", "foo[0].bar", "[0].foo"}, |
| 83 | + }, |
| 84 | + } |
| 85 | + for _, tt := range tests { |
| 86 | + t.Run(tt.name, func(t *testing.T) { |
| 87 | + trie := &dyn.TrieNode{} |
| 88 | + pattern := dyn.MustPatternFromString(tt.pattern) |
| 89 | + |
| 90 | + // None of the expected paths should match yet. |
| 91 | + for _, path := range tt.mustMatch { |
| 92 | + _, ok := trie.SearchPath(dyn.MustPathFromString(path)) |
| 93 | + assert.False(t, ok) |
| 94 | + } |
| 95 | + for _, path := range tt.mustNotMatch { |
| 96 | + _, ok := trie.SearchPath(dyn.MustPathFromString(path)) |
| 97 | + assert.False(t, ok) |
| 98 | + } |
| 99 | + |
| 100 | + err := trie.Insert(pattern) |
| 101 | + assert.NoError(t, err) |
| 102 | + |
| 103 | + // Now all the expected paths should match. |
| 104 | + for _, path := range tt.mustMatch { |
| 105 | + pattern, ok := trie.SearchPath(dyn.MustPathFromString(path)) |
| 106 | + assert.True(t, ok) |
| 107 | + assert.Equal(t, dyn.MustPatternFromString(tt.pattern), pattern) |
| 108 | + } |
| 109 | + for _, path := range tt.mustNotMatch { |
| 110 | + _, ok := trie.SearchPath(dyn.MustPathFromString(path)) |
| 111 | + assert.False(t, ok) |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestPatternTrie_MultiplePatterns(t *testing.T) { |
| 118 | + trie := &dyn.TrieNode{} |
| 119 | + |
| 120 | + patterns := []string{ |
| 121 | + "foo.bar", |
| 122 | + "foo.*.baz", |
| 123 | + "def[*]", |
| 124 | + } |
| 125 | + |
| 126 | + mustMatch := map[string]string{ |
| 127 | + "foo.bar": "foo.bar", |
| 128 | + "foo.abc.baz": "foo.*.baz", |
| 129 | + "foo.def.baz": "foo.*.baz", |
| 130 | + "def[0]": "def[*]", |
| 131 | + "def[1]": "def[*]", |
| 132 | + } |
| 133 | + |
| 134 | + mustNotMatch := []string{ |
| 135 | + "foo", |
| 136 | + "abc[0]", |
| 137 | + "abc[1]", |
| 138 | + "def[2].x", |
| 139 | + "foo.y", |
| 140 | + "foo.bar.baz.qux", |
| 141 | + } |
| 142 | + |
| 143 | + for _, pattern := range patterns { |
| 144 | + err := trie.Insert(dyn.MustPatternFromString(pattern)) |
| 145 | + assert.NoError(t, err) |
| 146 | + } |
| 147 | + |
| 148 | + for path, expectedPattern := range mustMatch { |
| 149 | + pattern, ok := trie.SearchPath(dyn.MustPathFromString(path)) |
| 150 | + assert.True(t, ok) |
| 151 | + assert.Equal(t, dyn.MustPatternFromString(expectedPattern), pattern) |
| 152 | + } |
| 153 | + |
| 154 | + for _, path := range mustNotMatch { |
| 155 | + _, ok := trie.SearchPath(dyn.MustPathFromString(path)) |
| 156 | + assert.False(t, ok) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func TestPatternTrie_OverlappingPatterns(t *testing.T) { |
| 161 | + trie := &dyn.TrieNode{} |
| 162 | + |
| 163 | + // Insert overlapping patterns |
| 164 | + patterns := []string{ |
| 165 | + "foo.bar", |
| 166 | + "foo.*", |
| 167 | + "*.bar", |
| 168 | + "*.*", |
| 169 | + } |
| 170 | + |
| 171 | + for _, pattern := range patterns { |
| 172 | + err := trie.Insert(dyn.MustPatternFromString(pattern)) |
| 173 | + assert.NoError(t, err) |
| 174 | + } |
| 175 | + |
| 176 | + for _, path := range []string{ |
| 177 | + "foo.bar", |
| 178 | + "foo.baz", |
| 179 | + "baz.bar", |
| 180 | + "baz.qux", |
| 181 | + } { |
| 182 | + _, ok := trie.SearchPath(dyn.MustPathFromString(path)) |
| 183 | + assert.True(t, ok) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func TestPatternTrie_FixedIndexPatterns(t *testing.T) { |
| 188 | + trie := &dyn.TrieNode{} |
| 189 | + |
| 190 | + err := trie.Insert(dyn.MustPatternFromString("foo[0]")) |
| 191 | + assert.EqualError(t, err, "fixed index patterns are not supported: dyn.Pattern{dyn.pathComponent{key:\"foo\", index:0}, dyn.pathComponent{key:\"\", index:0}}") |
| 192 | + |
| 193 | + err = trie.Insert(dyn.MustPatternFromString("foo[2]")) |
| 194 | + assert.EqualError(t, err, "fixed index patterns are not supported: dyn.Pattern{dyn.pathComponent{key:\"foo\", index:0}, dyn.pathComponent{key:\"\", index:2}}") |
| 195 | +} |
0 commit comments