| 1 | package gocache |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestMatchPattern(t *testing.T) { |
| 6 | scenarios := []struct { |
| 7 | pattern string |
| 8 | key string |
| 9 | expectedToMatch bool |
| 10 | }{ |
| 11 | { |
| 12 | pattern: "*", |
| 13 | key: "livingroom_123", |
| 14 | expectedToMatch: true, |
| 15 | }, |
| 16 | { |
| 17 | pattern: "*", |
| 18 | key: "livingroom_123", |
| 19 | expectedToMatch: true, |
| 20 | }, |
| 21 | { |
| 22 | pattern: "**", |
| 23 | key: "livingroom_123", |
| 24 | expectedToMatch: true, |
| 25 | }, |
| 26 | { |
| 27 | pattern: "living*", |
| 28 | key: "livingroom_123", |
| 29 | expectedToMatch: true, |
| 30 | }, |
| 31 | { |
| 32 | pattern: "*living*", |
| 33 | key: "livingroom_123", |
| 34 | expectedToMatch: true, |
| 35 | }, |
| 36 | { |
| 37 | pattern: "*123", |
| 38 | key: "livingroom_123", |
| 39 | expectedToMatch: true, |
| 40 | }, |
| 41 | { |
| 42 | pattern: "*_*", |
| 43 | key: "livingroom_123", |
| 44 | expectedToMatch: true, |
| 45 | }, |
| 46 | { |
| 47 | pattern: "living*_*3", |
| 48 | key: "livingroom_123", |
| 49 | expectedToMatch: true, |
| 50 | }, |
| 51 | { |
| 52 | pattern: "living*room_*3", |
| 53 | key: "livingroom_123", |
| 54 | expectedToMatch: true, |
| 55 | }, |
| 56 | { |
| 57 | pattern: "living*room_*3", |
| 58 | key: "livingroom_123", |
| 59 | expectedToMatch: true, |
| 60 | }, |
| 61 | { |
| 62 | pattern: "*vin*om*2*", |
| 63 | key: "livingroom_123", |
| 64 | expectedToMatch: true, |
| 65 | }, |
| 66 | { |
| 67 | pattern: "livingroom_123", |
| 68 | key: "livingroom_123", |
| 69 | expectedToMatch: true, |
| 70 | }, |
| 71 | { |
| 72 | pattern: "*livingroom_123*", |
| 73 | key: "livingroom_123", |
| 74 | expectedToMatch: true, |
| 75 | }, |
| 76 | { |
| 77 | pattern: "livingroom", |
| 78 | key: "livingroom_123", |
| 79 | expectedToMatch: false, |
| 80 | }, |
| 81 | { |
| 82 | pattern: "livingroom123", |
| 83 | key: "livingroom_123", |
| 84 | expectedToMatch: false, |
| 85 | }, |
| 86 | { |
| 87 | pattern: "what", |
| 88 | key: "livingroom_123", |
| 89 | expectedToMatch: false, |
| 90 | }, |
| 91 | { |
| 92 | pattern: "*what*", |
| 93 | key: "livingroom_123", |
| 94 | expectedToMatch: false, |
| 95 | }, |
| 96 | { |
| 97 | pattern: "*.*", |
| 98 | key: "livingroom_123", |
| 99 | expectedToMatch: false, |
| 100 | }, |
| 101 | { |
| 102 | pattern: "room*123", |
| 103 | key: "livingroom_123", |
| 104 | expectedToMatch: false, |
| 105 | }, |
| 106 | } |
| 107 | for _, scenario := range scenarios { |
| 108 | t.Run(scenario.pattern+"---"+scenario.key, func(t *testing.T) { |
| 109 | matched := MatchPattern(scenario.pattern, scenario.key) |
| 110 | if scenario.expectedToMatch { |
| 111 | if !matched { |
| 112 | t.Errorf("%s should've matched pattern '%s'", scenario.key, scenario.pattern) |
| 113 | } |
| 114 | } else { |
| 115 | if matched { |
| 116 | t.Errorf("%s shouldn't have matched pattern '%s'", scenario.key, scenario.pattern) |
| 117 | } |
| 118 | } |
| 119 | }) |
| 120 | } |
| 121 | } |