github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/stringlib/pattern/pattern_test.go (about) 1 package pattern 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func sameCaptures(exp, act []Capture) bool { 9 if len(exp) != len(act) { 10 return false 11 } 12 for i := range exp { 13 if exp[i] != act[i] { 14 return false 15 } 16 } 17 return true 18 } 19 20 func TestPattern(t *testing.T) { 21 tests := []struct { 22 ptn, s string 23 captures []Capture 24 invalid bool 25 }{ 26 { 27 ptn: "a*", 28 s: "aaabbb", 29 captures: []Capture{{0, 3}}, 30 }, 31 { 32 ptn: "a*aaab", 33 s: "aaaaaaaaabcd", 34 captures: []Capture{{0, 10}}, 35 }, 36 { 37 ptn: "%l+", 38 s: "xyzABC", 39 captures: []Capture{{0, 3}}, 40 }, 41 { 42 ptn: "(a+)bb", 43 s: "aaabbb", 44 captures: []Capture{{0, 5}, {0, 3}}, 45 }, 46 { 47 ptn: "x(%d+(%l+))(zzz)", 48 s: "x123abczzz", 49 captures: []Capture{{0, 10}, {1, 7}, {4, 7}, {7, 10}}, 50 }, 51 { 52 ptn: "..z", 53 s: "xyxyz", 54 captures: []Capture{{2, 5}}, 55 }, 56 { 57 ptn: "(..)-%1", 58 s: "ab-ba-ba", 59 captures: []Capture{{3, 8}, {3, 5}}, 60 }, 61 { 62 ptn: "x%b()y", 63 s: "x(y(y)(y)y)y()y", 64 captures: []Capture{{0, 12}}, 65 }, 66 { 67 ptn: "[abc]", 68 s: "uvwbzz", 69 captures: []Capture{{3, 4}}, 70 }, 71 { 72 ptn: "%f[abc].*%f[^abc]", 73 s: "1234baac4321", 74 captures: []Capture{{4, 8}}, 75 }, 76 { 77 ptn: "%d-123", 78 s: "456123123", 79 captures: []Capture{{0, 6}}, 80 }, 81 { 82 ptn: "%d*123", 83 s: "456123123", 84 captures: []Capture{{0, 9}}, 85 }, 86 { 87 ptn: "^abc", 88 s: "123abc", 89 captures: nil, 90 }, 91 { 92 ptn: "^a-$", 93 s: "aaaaa", 94 captures: []Capture{{0, 5}}, 95 }, 96 { 97 ptn: "(xx%1)", 98 invalid: true, 99 }, 100 { 101 ptn: "$", 102 s: "abc", 103 captures: []Capture{{3, 3}}, 104 }, 105 { 106 ptn: "%C+", 107 s: "abc123 xyz", 108 captures: []Capture{{0, 10}}, 109 }, 110 { 111 ptn: "[]%d]+x?", 112 s: "xxx]22x456", 113 captures: []Capture{{3, 7}}, 114 }, 115 { 116 ptn: "[0-][a-f]+$^", 117 s: "123--cafe$^ll", 118 captures: []Capture{{4, 11}}, 119 }, 120 { 121 ptn: "((((((((((a))))))))))", 122 invalid: true, 123 }, 124 { 125 ptn: "(", 126 invalid: true, 127 }, 128 { 129 ptn: "aa)", 130 invalid: true, 131 }, 132 { 133 ptn: "aaa%", 134 invalid: true, 135 }, 136 { 137 ptn: "%b", 138 invalid: true, 139 }, 140 { 141 ptn: "%b<", 142 invalid: true, 143 }, 144 { 145 ptn: "%0", 146 invalid: true, 147 }, 148 { 149 ptn: "%e", 150 invalid: true, 151 }, 152 { 153 ptn: "%[", 154 s: "[", 155 captures: []Capture{{0, 1}}, 156 }, 157 { 158 ptn: "[%", 159 invalid: true, 160 }, 161 { 162 ptn: "[%e]", 163 invalid: true, 164 }, 165 { 166 ptn: "[x", 167 invalid: true, 168 }, 169 { 170 ptn: "[x-", 171 invalid: true, 172 }, 173 } 174 for i, test := range tests { 175 t.Run(fmt.Sprintf("ptn_%d", i), func(t *testing.T) { 176 ptn, err := New(test.ptn) 177 if err != nil { 178 if test.invalid { 179 return 180 } 181 t.Fatal(err) 182 } 183 if test.invalid { 184 t.Fatal("Expected to be invalid") 185 } 186 captures, _ := ptn.MatchFromStart(test.s, 0, 0) 187 if !sameCaptures(test.captures, captures) { 188 t.Error("exp:", test.captures, "act:", captures) 189 t.Fail() 190 } 191 }) 192 } 193 }