github.com/hdt3213/godis@v1.2.9/lib/wildcard/wildcard_test.go (about) 1 package wildcard 2 3 import "testing" 4 5 func TestWildCard(t *testing.T) { 6 p, err := CompilePattern("") 7 if err != nil { 8 t.Error(err) 9 return 10 } 11 if !p.IsMatch("") { 12 t.Error("expect true actually false") 13 } 14 p, err = CompilePattern("a") 15 if err != nil { 16 t.Error(err) 17 return 18 } 19 if !p.IsMatch("a") { 20 t.Error("expect true actually false") 21 } 22 if p.IsMatch("b") { 23 t.Error("expect false actually true") 24 } 25 26 // test '?' 27 p, err = CompilePattern("a?") 28 if err != nil { 29 t.Error(err) 30 return 31 } 32 if !p.IsMatch("ab") { 33 t.Error("expect true actually false") 34 } 35 if p.IsMatch("a") { 36 t.Error("expect false actually true") 37 } 38 if p.IsMatch("abb") { 39 t.Error("expect false actually true") 40 } 41 if p.IsMatch("bb") { 42 t.Error("expect false actually true") 43 } 44 45 // test * 46 p, err = CompilePattern("a*") 47 if err != nil { 48 t.Error(err) 49 return 50 } 51 if !p.IsMatch("ab") { 52 t.Error("expect true actually false") 53 } 54 if !p.IsMatch("a") { 55 t.Error("expect true actually false") 56 } 57 if !p.IsMatch("abb") { 58 t.Error("expect true actually false") 59 } 60 if p.IsMatch("bb") { 61 t.Error("expect false actually true") 62 } 63 64 // test [] 65 p, err = CompilePattern("a[ab[]") 66 if err != nil { 67 t.Error(err) 68 return 69 } 70 if !p.IsMatch("ab") { 71 t.Error("expect true actually false") 72 } 73 if !p.IsMatch("aa") { 74 t.Error("expect true actually false") 75 } 76 if !p.IsMatch("a[") { 77 t.Error("expect true actually false") 78 } 79 if p.IsMatch("abb") { 80 t.Error("expect false actually true") 81 } 82 if p.IsMatch("bb") { 83 t.Error("expect false actually true") 84 } 85 86 // test [a-c] 87 p, err = CompilePattern("h[a-c]llo") 88 if err != nil { 89 t.Error(err) 90 return 91 } 92 if !p.IsMatch("hallo") { 93 t.Error("expect true actually false") 94 } 95 if !p.IsMatch("hbllo") { 96 t.Error("expect true actually false") 97 } 98 if !p.IsMatch("hcllo") { 99 t.Error("expect true actually false") 100 } 101 if p.IsMatch("hdllo") { 102 t.Error("expect false actually true") 103 } 104 if p.IsMatch("hello") { 105 t.Error("expect false actually true") 106 } 107 108 //test [^] 109 p, err = CompilePattern("h[^ab]llo") 110 if err != nil { 111 t.Error(err) 112 return 113 } 114 if p.IsMatch("hallo") { 115 t.Error("expect false actually true") 116 } 117 if p.IsMatch("hbllo") { 118 t.Error("expect false actually true") 119 } 120 if !p.IsMatch("hcllo") { 121 t.Error("expect true actually false") 122 } 123 124 p, err = CompilePattern("[^ab]c") 125 if err != nil { 126 t.Error(err) 127 return 128 } 129 if p.IsMatch("abc") { 130 t.Error("expect false actually true") 131 } 132 if !p.IsMatch("1c") { 133 t.Error("expect true actually false") 134 } 135 136 p, err = CompilePattern("1^2") 137 if err != nil { 138 t.Error(err) 139 return 140 } 141 if !p.IsMatch("1^2") { 142 t.Error("expect true actually false") 143 } 144 145 p, err = CompilePattern(`\[^1]2`) 146 if err != nil { 147 t.Error(err) 148 return 149 } 150 if !p.IsMatch("[^1]2") { 151 t.Error("expect true actually false") 152 } 153 154 p, err = CompilePattern(`^1`) 155 if err != nil { 156 t.Error(err) 157 return 158 } 159 if !p.IsMatch("^1") { 160 t.Error("expect true actually false") 161 } 162 163 // test escape 164 p, err = CompilePattern(`\\\\`) 165 if err != nil { 166 t.Error(err) 167 return 168 } 169 if !p.IsMatch(`\\`) { 170 t.Error("expect true actually false") 171 } 172 173 p, err = CompilePattern("\\*") 174 if err != nil { 175 t.Error(err) 176 return 177 } 178 if !p.IsMatch("*") { 179 t.Error("expect true actually false") 180 } 181 if p.IsMatch("a") { 182 t.Error("expect false actually true") 183 } 184 185 p, err = CompilePattern(`\`) 186 if err == nil || err.Error() != errEndWithEscape { 187 t.Error(err) 188 return 189 } 190 }