|
| 1 | +package wildcard |
| 2 | + |
| 3 | +/* |
| 4 | + * copy and modified from https://github.com/IGLOU-EU/go-wildcard/tree/ce22b7af48e487517a492d3727d9386492043e21 |
| 5 | + * |
| 6 | + * Copyright (c) 2023 Iglou.eu <[email protected]> |
| 7 | + * Copyright (c) 2023 Adrien Kara <[email protected]> |
| 8 | + * |
| 9 | + * Licensed under the BSD 3-Clause License, |
| 10 | + */ |
| 11 | + |
| 12 | +import ( |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +// TestMatch validates the logic of wild card matching, |
| 17 | +// it need to support '*', '?' and only validate for byte comparison |
| 18 | +// over string, not rune or grapheme cluster |
| 19 | +func TestMatch(t *testing.T) { |
| 20 | + cases := []struct { |
| 21 | + s string |
| 22 | + pattern string |
| 23 | + result bool |
| 24 | + }{ |
| 25 | + {"", "", true}, |
| 26 | + {"", "*", true}, |
| 27 | + {"", "**", true}, |
| 28 | + {"", "?", true}, |
| 29 | + {"", "??", true}, |
| 30 | + {"", "?*", true}, |
| 31 | + {"", "*?", true}, |
| 32 | + {"", ".", false}, |
| 33 | + {"", ".?", false}, |
| 34 | + {"", "?.", false}, |
| 35 | + {"", ".*", false}, |
| 36 | + {"", "*.", false}, |
| 37 | + {"", "*.?", false}, |
| 38 | + {"", "?.*", false}, |
| 39 | + |
| 40 | + {"a", "", false}, |
| 41 | + {"a", "a", true}, |
| 42 | + {"a", "*", true}, |
| 43 | + {"a", "**", true}, |
| 44 | + {"a", "?", true}, |
| 45 | + {"a", "??", true}, |
| 46 | + {"a", ".", false}, |
| 47 | + {"a", ".?", false}, |
| 48 | + {"a", "?.", false}, |
| 49 | + {"a", ".*", false}, |
| 50 | + {"a", "*.", false}, |
| 51 | + {"a", "*.?", false}, |
| 52 | + {"a", "?.*", false}, |
| 53 | + |
| 54 | + {"match the exact string", "match the exact string", true}, |
| 55 | + {"do not match a different string", "this is a different string", false}, |
| 56 | + {"Match The Exact String WITH DIFFERENT CASE", "Match The Exact String WITH DIFFERENT CASE", true}, |
| 57 | + {"do not match a different string WITH DIFFERENT CASE", "this is a different string WITH DIFFERENT CASE", false}, |
| 58 | + {"Do Not Match The Exact String With Different Case", "do not match the exact string with different case", false}, |
| 59 | + {"match an emoji 😃", "match an emoji 😃", true}, |
| 60 | + {"do not match because of different emoji 😃", "do not match because of different emoji 😄", false}, |
| 61 | + {"🌅☕️📰👨💼👩💼🏢🖥️💼💻📊📈📉👨👩👧👦🍝🕰️💪🏋️♂️🏋️♀️🏋️♂️💼🚴♂️🚴♀️🚴♂️🛀💤🌃", "🌅☕️📰👨💼👩💼🏢🖥️💼💻📊📈📉👨👩👧👦🍝🕰️💪🏋️♂️🏋️♀️🏋️♂️💼🚴♂️🚴♀️🚴♂️🛀💤🌃", true}, |
| 62 | + {"🌅☕️📰👨💼👩💼🏢🖥️💼💻📊📈📉👨👩👧👦🍝🕰️💪🏋️♂️🏋️♀️🏋️♂️💼🚴♂️🚴♀️🚴♂️🛀💤🌃", "🦌🐇🦡🐿️🌲🌳🏰🌳🌲🌞🌧️❄️🌬️⛈️🔥🎄🎅🎁🎉🎊🥳👨👩👧👦💏👪💖👩💼🛀", false}, |
| 63 | + |
| 64 | + {"match a string with a *", "match a string *", true}, |
| 65 | + {"match a string with a * at the beginning", "* at the beginning", true}, |
| 66 | + {"match a string with two *", "match * with *", true}, |
| 67 | + {"do not match a string with extra and a *", "do not match a string * with more", false}, |
| 68 | + |
| 69 | + {"match a string with a ?", "match ? string with a ?", true}, |
| 70 | + {"match a string with a ? at the beginning", "?atch a string with a ? at the beginning", true}, |
| 71 | + {"match a string with two ?", "match a string with two ??", true}, |
| 72 | + {"match a optional char with a ?", "match a optional? char with a ?", true}, |
| 73 | + {"match a optional char with a ?", "match a optional? char with a ?", true}, |
| 74 | + {"do not match a string with extra and a ?", "do not match ? string with extra and a ? like this", false}, |
| 75 | + |
| 76 | + {"do not match a string with a .", "do not match . string with a .", false}, |
| 77 | + {"do not match a string with a . at the beginning", "do not .atch a string with a . at the beginning", false}, |
| 78 | + {"do not match a string with two .", "do not match a ..ring with two .", false}, |
| 79 | + {"do not match a string with extra .", "do not match a string with extra ..", false}, |
| 80 | + |
| 81 | + {"A big brown fox jumps over the lazy dog, with all there wildcards friends", ". big?brown fox jumps over * wildcard. friend??", false}, |
| 82 | + {"A big brown fox fails to jump over the lazy dog, with all there wildcards friends", ". big?brown fox jumps over * wildcard. friend??", false}, |
| 83 | + |
| 84 | + {"domain a.b.c", "domain a.b.c", true}, |
| 85 | + {"domain adb.c", "domain a.b.c", false}, |
| 86 | + {"aaaa", "a*a", true}, |
| 87 | + } |
| 88 | + |
| 89 | + for i, c := range cases { |
| 90 | + t.Run(c.s, func(t *testing.T) { |
| 91 | + result := Match(c.pattern, c.s) |
| 92 | + if c.result != result { |
| 93 | + t.Errorf("Test %d: Expected `%v`, found `%v`; With Pattern: `%s` and String: `%s`", i+1, c.result, result, c.pattern, c.s) |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func FuzzMatch(f *testing.F) { |
| 100 | + f.Fuzz(func(t *testing.T, s string) { |
| 101 | + if !Match(string(s), string(s)) { |
| 102 | + t.Fatalf("%s does not match %s", s, s) |
| 103 | + } |
| 104 | + }) |
| 105 | +} |
0 commit comments