| 1 | package helpers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/stretchr/testify/assert" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | lowercaseKeyRegex = `key_\d+` |
| 11 | lowercaseValueRegex = `value_\d+` |
| 12 | uppercaseValueRegex = `VALUE_\d+` |
| 13 | submatchesValueRegex = `(value)(_)(\d+)` |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | defaultKVRegex = NewDefaultKeyValueRegex(lowercaseValueRegex) |
| 18 | kvMultipleRegex = NewDefaultKeyValueRegex(lowercaseValueRegex, uppercaseValueRegex) |
| 19 | kvSubmatchesRegex = NewDefaultKeyValueRegex(submatchesValueRegex) |
| 20 | kvRegex = NewKeyValueRegex(lowercaseKeyRegex, lowercaseValueRegex) |
| 21 | ) |
| 22 | |
| 23 | type testCase struct { |
| 24 | name, input string |
| 25 | isExpected bool |
| 26 | expectedFullMatch, expectedKey, expectedDelimiter, expectedValue string |
| 27 | } |
| 28 | |
| 29 | var testCases = []testCase{ |
| 30 | {name: `value only`, input: "value_101", isExpected: true, expectedFullMatch: "value_101", expectedValue: "value_101"}, |
| 31 | {name: `value only - no match`, input: "VALUE_101", isExpected: false}, |
| 32 | {name: `value surrounded by whitespace`, input: " value_102 ", isExpected: true, expectedFullMatch: "value_102", expectedValue: "value_102"}, |
| 33 | {name: `value surrounded by whitespace - no match`, input: " VALUE_102 ", isExpected: false}, |
| 34 | {name: `"value"`, input: ` "value_103" `, isExpected: true, expectedFullMatch: `"value_103"`, expectedValue: "value_103"}, |
| 35 | {name: `"value" - no match`, input: ` "VALUE_103" `, isExpected: false}, |
| 36 | {name: `'value'`, input: `'value_104'`, isExpected: true, expectedFullMatch: `'value_104'`, expectedValue: "value_104"}, |
| 37 | {name: `'value' - no match`, input: `'VALUE_104'`, isExpected: false}, |
| 38 | {name: `[value]`, input: `[value_105]`, isExpected: true, expectedFullMatch: `[value_105]`, expectedValue: "value_105"}, |
| 39 | {name: `[value] - no match`, input: `[VALUE_105]`, isExpected: false}, |
| 40 | {name: `head_value`, input: `head_value_106`, isExpected: false}, |
| 41 | {name: `value_tail`, input: `value_107_tail`, isExpected: false}, |
| 42 | {name: `value space tail`, input: `value_108 tail`, isExpected: true, expectedFullMatch: "value_108", expectedValue: "value_108"}, |
| 43 | {name: `[ value ]`, input: `[ value_109 ]`, isExpected: true, expectedFullMatch: `[ value_109 ]`, expectedValue: "value_109"}, |
| 44 | {name: `[ value ] - no match`, input: `[ VALUE_109 ]`, isExpected: false}, |
| 45 | {name: `key value (key without delimiter not caught)`, input: `key_201 value_201`, isExpected: true, expectedFullMatch: `value_201`, expectedValue: "value_201"}, |
| 46 | {name: `key=value`, input: `key_202=value_202`, isExpected: true, expectedFullMatch: `key_202=value_202`, expectedKey: "key_202", expectedDelimiter: "=", expectedValue: "value_202"}, |
| 47 | {name: `key=value - no match`, input: `key_202=VALUE_202`, isExpected: false}, |
| 48 | {name: `key: value`, input: `key_203: value_203`, isExpected: true, expectedFullMatch: `key_203: value_203`, expectedKey: "key_203", expectedDelimiter: ":", expectedValue: "value_203"}, |
| 49 | {name: `key: value - no match`, input: `key_203: VALUE_203`, isExpected: false}, |
| 50 | {name: `key := value`, input: `key_204 := value_204`, isExpected: true, expectedFullMatch: `key_204 := value_204`, expectedKey: "key_204", expectedDelimiter: ":=", expectedValue: "value_204"}, |
| 51 | {name: `'key'=value`, input: `'key_205'=value_205`, isExpected: true, expectedFullMatch: `'key_205'=value_205`, expectedKey: "key_205", expectedDelimiter: "=", expectedValue: "value_205"}, |
| 52 | {name: `'key'=value - no match`, input: `'key_205'=VALUE_205`, isExpected: false}, |
| 53 | {name: `"key"="value"`, input: `"key_206"="value_206"`, isExpected: true, expectedFullMatch: `"key_206"="value_206"`, expectedKey: "key_206", expectedDelimiter: "=", expectedValue: "value_206"}, |
| 54 | {name: `"key"="value" - no match`, input: `"key_206"="VALUE_206"`, isExpected: false}, |
| 55 | {name: `"key" = 'value'`, input: `"key_207" = 'value_207'`, isExpected: true, expectedFullMatch: `"key_207" = 'value_207'`, expectedKey: "key_207", expectedDelimiter: "=", expectedValue: "value_207"}, |
| 56 | {name: `"key" = 'value' - no match`, input: `"key_207" = 'VALUE_207'`, isExpected: false}, |
| 57 | {name: `key = value`, input: `key_208 = value_208`, isExpected: true, expectedFullMatch: `key_208 = value_208`, expectedKey: "key_208", expectedDelimiter: "=", expectedValue: "value_208"}, |
| 58 | {name: `key = value - no match`, input: `key_208 = VALUE_208`, isExpected: false}, |
| 59 | {name: `key with space = value`, input: `key 209 = value_209`, isExpected: true, expectedFullMatch: `key 209 = value_209`, expectedKey: "key 209", expectedDelimiter: "=", expectedValue: "value_209"}, |
| 60 | {name: `key with space = value - no match`, input: `key 209 = VALUE_209`, isExpected: false}, |
| 61 | {name: `key/with/slash=value`, input: `key/210=value_210`, isExpected: true, expectedFullMatch: `key/210=value_210`, expectedKey: "key/210", expectedDelimiter: "=", expectedValue: "value_210"}, |
| 62 | {name: `key/with/slash=value`, input: `key/209=VALUE_209`, isExpected: false}, |
| 63 | {name: ` key with tab=value `, input: ` key 211=value_211 `, isExpected: true, expectedFullMatch: `key 211=value_211`, expectedKey: `key 211`, expectedDelimiter: "=", expectedValue: "value_211"}, |
| 64 | {name: ` key with tab=value - no match`, input: ` key 211=VALUE_211 `, isExpected: false}, |
| 65 | {name: `[key = value]`, input: `[key_212 = value_212]`, isExpected: true, expectedFullMatch: `[key_212 = value_212]`, expectedKey: "key_212", expectedDelimiter: "=", expectedValue: "value_212"}, |
| 66 | {name: `[key = value] - no match`, input: `[key_212 = VALUE_212]`, isExpected: false}, |
| 67 | {name: `key=value;`, input: `key_213=value_213;`, isExpected: true, expectedFullMatch: `key_213=value_213;`, expectedKey: "key_213", expectedDelimiter: "=", expectedValue: "value_213"}, |
| 68 | {name: `key=head_value`, input: `key_214=head_value_214`, isExpected: false}, |
| 69 | {name: `key=value_tail`, input: `key_215=value_215_tail`, isExpected: false}, |
| 70 | {name: `key=value space tail`, input: `key_216=value_216 tail`, isExpected: true, expectedFullMatch: `key_216=value_216`, expectedKey: "key_216", expectedDelimiter: "=", expectedValue: "value_216"}, |
| 71 | {name: `$key = value`, input: `$key_217 = value_217`, isExpected: true, expectedFullMatch: `$key_217 = value_217`, expectedKey: "$key_217", expectedDelimiter: "=", expectedValue: "value_217"}, |
| 72 | {name: `$key = value - no match`, input: `$key_217 = VALUE_217`, isExpected: false}, |
| 73 | {name: `export KEY=value`, input: `export KEY_218=value_218`, isExpected: true, expectedFullMatch: `export KEY_218=value_218`, expectedKey: "KEY_218", expectedDelimiter: "=", expectedValue: "value_218"}, |
| 74 | {name: `export KEY=value - no match`, input: `export KEY_218=VALUE_218`, isExpected: false}, |
| 75 | {name: `SET key=value`, input: `SET key_219=value_219`, isExpected: true, expectedFullMatch: `SET key_219=value_219`, expectedKey: "key_219", expectedDelimiter: "=", expectedValue: "value_219"}, |
| 76 | {name: `SET key=value - no match`, input: `SET key_219=VALUE_219`, isExpected: false}, |
| 77 | {name: `"key" = "value";`, input: `"key_220" = "value_220";`, isExpected: true, expectedFullMatch: `"key_220" = "value_220";`, expectedKey: "key_220", expectedDelimiter: "=", expectedValue: "value_220"}, |
| 78 | {name: `"key" = "value"; - no match`, input: `"key_220" = "VALUE_220";`, isExpected: false}, |
| 79 | {name: `[ "key" ] = [ "value" ]`, input: `[ "key_221" ] = [ "value_221" ]`, isExpected: true, expectedFullMatch: `[ "key_221" ] = [ "value_221" ]`, expectedKey: "key_221", expectedDelimiter: "=", expectedValue: "value_221"}, |
| 80 | {name: `[ "key" ] = [ "value" ] - no match`, input: `[ "key_221" ] = [ "VALUE_221" ]`, isExpected: false}, |
| 81 | {name: `[ key ]=[ value ]`, input: `[ key_222 ]=[ value_222 ]`, isExpected: true, expectedFullMatch: `[ key_222 ]=[ value_222 ]`, expectedKey: "key_222", expectedDelimiter: "=", expectedValue: "value_222"}, |
| 82 | {name: `[ key ]=[ value ] - no match`, input: `[ key_222 ]=[ VALUE_222 ]`, isExpected: false}, |
| 83 | {name: `[ "key" ] = [ "value" ] ;`, input: `[ "key_223" ] = [ "value_223" ] ;`, isExpected: true, expectedFullMatch: `[ "key_223" ] = [ "value_223" ] ;`, expectedKey: "key_223", expectedDelimiter: "=", expectedValue: "value_223"}, |
| 84 | {name: `[ "key" ] = [ "value" ] ; - no match`, input: `[ "key_223" ] = [ "VALUE_223" ] ;`, isExpected: false}, |
| 85 | {name: `empty input`, input: ``, isExpected: false}, |
| 86 | } |
| 87 | |
| 88 | var testCasesMultipleRegex = []testCase{ |
| 89 | {name: `value only`, input: "value_101", isExpected: true, expectedFullMatch: "value_101", expectedValue: "value_101"}, |
| 90 | {name: `VALUE only`, input: "VALUE_102", isExpected: true, expectedFullMatch: "VALUE_102", expectedValue: "VALUE_102"}, |
| 91 | {name: `key=value`, input: `key_103=value_103`, isExpected: true, expectedFullMatch: `key_103=value_103`, expectedKey: "key_103", expectedDelimiter: "=", expectedValue: "value_103"}, |
| 92 | {name: `key=VALUE`, input: `key_104=VALUE_104`, isExpected: true, expectedFullMatch: `key_104=VALUE_104`, expectedKey: "key_104", expectedDelimiter: "=", expectedValue: "VALUE_104"}, |
| 93 | {name: `key=value - no match`, input: `key_105=105`, isExpected: false}, |
| 94 | {name: `empty input`, input: ``, isExpected: false}, |
| 95 | } |
| 96 | |
| 97 | var testCasesKeyValue = []testCase{ |
| 98 | {name: `key = value`, input: ` key_101 = value_101 `, isExpected: true, expectedFullMatch: "key_101 = value_101", expectedKey: "key_101", expectedDelimiter: "=", expectedValue: "value_101"}, |
| 99 | {name: `"key": "value"`, input: `"key_102": "value_102"`, isExpected: true, expectedFullMatch: `"key_102": "value_102"`, expectedKey: "key_102", expectedDelimiter: ":", expectedValue: "value_102"}, |
| 100 | {name: `'key' := 'value'`, input: ` 'key_103' := 'value_103' `, isExpected: true, expectedFullMatch: `'key_103' := 'value_103'`, expectedKey: "key_103", expectedDelimiter: ":=", expectedValue: "value_103"}, |
| 101 | {name: `only key matched`, input: `key_201=201`, isExpected: false}, |
| 102 | {name: `only value matched`, input: `k_202=value_202`, isExpected: false}, |
| 103 | {name: `both not matched`, input: `k_203=v_203`, isExpected: false}, |
| 104 | {name: `key only`, input: `key_204`, isExpected: false}, |
| 105 | {name: `value only`, input: `value_205`, isExpected: false}, |
| 106 | {name: `empty input`, input: ``, isExpected: false}, |
| 107 | } |
| 108 | |
| 109 | func TestFindAll(t *testing.T) { |
| 110 | for _, test := range testCases { |
| 111 | t.Run(test.name, func(t *testing.T) { |
| 112 | actualMatches, err := defaultKVRegex.FindAll(test.input) |
| 113 | assert.NoError(t, err) |
| 114 | if test.isExpected { |
| 115 | if assert.Len(t, actualMatches, 1) { |
| 116 | assertMatch(t, test, actualMatches[0]) |
| 117 | } |
| 118 | } else { |
| 119 | assert.Len(t, actualMatches, 0) |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestFindAll_MultipleMatches(t *testing.T) { |
| 126 | t.Run("Test FindAll with multiple matches", func(t *testing.T) { |
| 127 | var input string |
| 128 | expectedMatches := make([]testCase, 0) |
| 129 | for _, test := range testCases { |
| 130 | input += fmt.Sprintf("%v\n", test.input) |
| 131 | if test.isExpected { |
| 132 | expectedMatches = append(expectedMatches, test) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | actualMatches, err := defaultKVRegex.FindAll(input) |
| 137 | assert.NoError(t, err) |
| 138 | assertMatches(t, expectedMatches, actualMatches) |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | func TestFindAll_MultipleRegex(t *testing.T) { |
| 143 | for _, test := range testCasesMultipleRegex { |
| 144 | t.Run(test.name, func(t *testing.T) { |
| 145 | actualMatches, err := kvMultipleRegex.FindAll(test.input) |
| 146 | assert.NoError(t, err) |
| 147 | if test.isExpected { |
| 148 | if assert.Len(t, actualMatches, 1) { |
| 149 | assertMatch(t, test, actualMatches[0]) |
| 150 | } |
| 151 | } else { |
| 152 | assert.Len(t, actualMatches, 0) |
| 153 | } |
| 154 | }) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestFindAll_MultipleRegexMultipleMatches(t *testing.T) { |
| 159 | t.Run("Test FindAll with multiple matches", func(t *testing.T) { |
| 160 | var input string |
| 161 | expectedMatches := make([]testCase, 0) |
| 162 | for _, test := range testCasesMultipleRegex { |
| 163 | input += fmt.Sprintf("%v\n", test.input) |
| 164 | if test.isExpected { |
| 165 | expectedMatches = append(expectedMatches, test) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | actualMatches, err := kvMultipleRegex.FindAll(input) |
| 170 | assert.NoError(t, err) |
| 171 | assertMatches(t, expectedMatches, actualMatches) |
| 172 | }) |
| 173 | } |
| 174 | |
| 175 | func TestFindAll_Submatches(t *testing.T) { |
| 176 | t.Run("Test FindAll with value submatches", func(t *testing.T) { |
| 177 | test := testCase{ |
| 178 | input: `key_101 = value_101`, |
| 179 | isExpected: true, |
| 180 | expectedFullMatch: `key_101 = value_101`, |
| 181 | expectedKey: "key_101", |
| 182 | expectedDelimiter: "=", |
| 183 | expectedValue: "value_101", |
| 184 | } |
| 185 | expectedValueSubmatches := []string{"value", "_", "101"} |
| 186 | |
| 187 | actualMatches, err := kvSubmatchesRegex.FindAll(test.input) |
| 188 | assert.NoError(t, err) |
| 189 | if assert.Len(t, actualMatches, 1) { |
| 190 | assertMatch(t, test, actualMatches[0]) |
| 191 | assert.Equal(t, expectedValueSubmatches, actualMatches[0].ValueSubmatches) |
| 192 | } |
| 193 | }) |
| 194 | } |
| 195 | |
| 196 | func TestFindAll_KeyValue(t *testing.T) { |
| 197 | for _, test := range testCasesKeyValue { |
| 198 | t.Run(test.name, func(t *testing.T) { |
| 199 | actualMatches, err := kvRegex.FindAll(test.input) |
| 200 | assert.NoError(t, err) |
| 201 | if test.isExpected { |
| 202 | if assert.Len(t, actualMatches, 1) { |
| 203 | assertMatch(t, test, actualMatches[0]) |
| 204 | } |
| 205 | } else { |
| 206 | assert.Len(t, actualMatches, 0) |
| 207 | } |
| 208 | }) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func TestFindAll_KeyValueMultipleMatches(t *testing.T) { |
| 213 | t.Run("Test FindAll with multiple matches", func(t *testing.T) { |
| 214 | var input string |
| 215 | expectedMatches := make([]testCase, 0) |
| 216 | for _, test := range testCasesKeyValue { |
| 217 | input += fmt.Sprintf("%v\n", test.input) |
| 218 | if test.isExpected { |
| 219 | expectedMatches = append(expectedMatches, test) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | actualMatches, err := kvRegex.FindAll(input) |
| 224 | assert.NoError(t, err) |
| 225 | assertMatches(t, expectedMatches, actualMatches) |
| 226 | }) |
| 227 | } |
| 228 | |
| 229 | func assertMatch(t *testing.T, expected testCase, actual MatchResult) { |
| 230 | t.Helper() |
| 231 | |
| 232 | assert.Equal(t, expected.expectedFullMatch, actual.FullMatch) |
| 233 | assert.Equal(t, expected.expectedKey, actual.Key) |
| 234 | assert.Equal(t, expected.expectedDelimiter, actual.Delimiter) |
| 235 | assert.Equal(t, expected.expectedValue, actual.Value) |
| 236 | } |
| 237 | |
| 238 | func assertMatches(t *testing.T, expectedMatches []testCase, actualMatches []MatchResult) { |
| 239 | t.Helper() |
| 240 | |
| 241 | if !assert.Len(t, actualMatches, len(expectedMatches)) { |
| 242 | return |
| 243 | } |
| 244 | |
| 245 | expectedMatchesMap := make(map[string]testCase, len(expectedMatches)) |
| 246 | for _, expected := range expectedMatches { |
| 247 | expectedMatchesMap[expected.expectedValue] = expected |
| 248 | } |
| 249 | |
| 250 | for _, actual := range actualMatches { |
| 251 | expected, found := expectedMatchesMap[actual.Value] |
| 252 | if assert.True(t, found) { |
| 253 | assertMatch(t, expected, actual) |
| 254 | } |
| 255 | } |
| 256 | } |