1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
package scan
import (
"bufio"
"strings"
"testing"
)
func TestPeek(t *testing.T) {
tcases := []struct {
name string
in string
peeked byte
}{{
"empty", "", 0,
}, {
"123", "123", '1',
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
peeked := b.Peek()
if string(b) != tc.in {
t.Errorf("left: got: %s, want: %s", string(b), tc.in)
}
if peeked != tc.peeked {
t.Errorf("peeked: got: %c, want: %c", peeked, tc.peeked)
}
})
}
}
func TestPop(t *testing.T) {
tcases := []struct {
name string
in string
popped byte
left string
}{{
"empty", "", 0, "",
}, {
"123", "123", '1', "23",
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
popped := b.Pop()
if string(b) != tc.left {
t.Errorf("left: got: %s, want: %s", string(b), tc.left)
}
if popped != tc.popped {
t.Errorf("popped: got: %c, want: %c", popped, tc.popped)
}
})
}
}
func TestPopN(t *testing.T) {
tcases := []struct {
name string
in string
n int
popped string
left string
}{{
"empty", "", 0, "", "",
}, {
"1,0", "1", 0, "", "1",
}, {
"12,0", "12", 0, "", "12",
}, {
"1,1", "1", 1, "1", "",
}, {
"12,1", "12", 1, "1", "2",
}, {
"123,1", "123", 1, "1", "23",
}, {
"123,2", "123", 2, "12", "3",
}, {
"123,3", "123", 3, "123", "",
}, {
"123,4", "123", 4, "", "123",
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
popped := b.PopN(tc.n)
if string(b) != tc.left {
t.Errorf("left: got: %s, want: %s", string(b), tc.left)
}
if string(popped) != tc.popped {
t.Errorf("popped: got: %s, want: %s", string(popped), tc.popped)
}
})
}
}
func TestTrim(t *testing.T) {
tcases := []struct {
name string
in string
left string
right string
}{{
"empty", "", "", "",
}, {
"one space", " ", "", "",
}, {
"all spaces", " \r\n\t\x0c", "", "",
}, {
"one char and spaces", " \r\n\t\x0ca \r\n\t\x0c", "a \r\n\t\x0c", " \r\n\t\x0ca",
}, {
"one char", "a", "a", "a",
}, {
// Unicode Ogham space mark
"unicode space ogham", " ", " ", " ",
}, {
// Unicode Em space mark
"unicode em space", "\u2003", "\u2003", "\u2003",
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
b.TrimLWS()
if string(b) != tc.left {
t.Errorf("left: got: %s, want: %s", string(b), tc.left)
}
b = Bytes(tc.in)
b.TrimRWS()
if string(b) != tc.right {
t.Errorf("right: got: %s, want: %s", string(b), tc.right)
}
})
}
}
func TestAdvance(t *testing.T) {
tcases := []struct {
name string
in string
advance int
want string
shouldDo bool
}{{
"empty 0", "", 0, "", true,
}, {
"empty 1", "", 1, "", false,
}, {
"empty -1", "", -1, "", false,
}, {
"123 0", "123", 0, "123", true,
}, {
"123 -1", "123", -1, "123", false,
}, {
"123 1", "123", 1, "23", true,
}, {
"123 4", "123", 4, "123", false,
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
did := b.Advance(tc.advance)
if did != tc.shouldDo {
t.Errorf("got: %t, want: %t", did, tc.shouldDo)
}
if string(b) != tc.want {
t.Errorf("got: %s, want: %s", string(b), tc.want)
}
})
}
}
func TestLine(t *testing.T) {
tcases := []struct {
name string
in string
line string
leftover string
}{{
"empty", "", "", "",
}, {
"one line", "abc", "abc", "",
}, {
"just a \\n", "\n", "", "",
}, {
"just two \\n", "\n\n", "", "\n",
}, {
"one line with \\n", "abc\n", "abc", "",
}, {
"two lines", "abc\ndef", "abc", "def",
}, {
"two lines with \\n", "abc\ndef\n", "abc", "def\n",
}, {
"drops final cr", "abc\r", "abc", "",
}, {
"cr inside line", "abc\rdef", "abc\rdef", "",
}, {
"nl and cr", "\n\r", "", "\r",
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
line := b.Line()
if string(line) != tc.line {
t.Errorf("line: got: %s, want: %s", line, []byte(tc.line))
}
if string(b) != tc.leftover {
t.Errorf("leftover: got: %s, want: %s", b, []byte(tc.leftover))
}
// Test if it behaves like bufio.Scanner as well.
s := bufio.NewScanner(strings.NewReader(tc.in))
s.Scan()
if string(line) != s.Text() {
t.Errorf("Bytes.Line not like bufio.Scanner")
}
})
}
}
func TestPopUntil(t *testing.T) {
tcases := []struct {
name string
in string
untilAny string
popped string
leftover string
}{{
"empty", "", "", "", "",
}, {
"empty with until", "", "123", "", "",
}, {
"until empty", "123", "", "123", "",
}, {
"until 1", "123", "1", "", "123",
}, {
"until 2", "123", "2", "1", "23",
}, {
"until 3", "123", "3", "12", "3",
}, {
"until 4", "123", "4", "123", "",
}, {
"multiple untilAny", "123", "32", "1", "23",
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
popped := b.PopUntil([]byte(tc.untilAny)...)
if string(popped) != tc.popped {
t.Errorf("popped: got: %s, want: %s", popped, []byte(tc.popped))
}
if string(b) != tc.leftover {
t.Errorf("leftover: got: %s, want: %s", b, []byte(tc.leftover))
}
})
}
}
func TestReadSlice(t *testing.T) {
tcases := []struct {
name string
in string
stopAt byte
popped string
leftover string
}{{
"both empty", "", 0, "", "",
}, {
"stop at not found", "abc", 'd', "abc", "",
}, {
"stop at the end", "abc", 'c', "abc", "",
}, {
"stop at in the middle", "abcdef", 'c', "abc", "def",
}, {
"stop at the beginning", "abcdef", 'a', "a", "bcdef",
}, {
"just one char", "a", 'a', "a", "",
}, {
"same char twice", "aa", 'a', "a", "a",
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
got := b.ReadSlice(tc.stopAt)
if tc.popped != string(got) {
t.Errorf("popped got: %s, want: %s", got, tc.popped)
}
if tc.leftover != string(b) {
t.Errorf("leftover got: %s, want: %s", string(b), tc.leftover)
}
})
}
}
func TestUint16(t *testing.T) {
tcases := []struct {
name string
in []byte
res uint16
ok bool
}{{
"empty", nil, 0, false,
}, {
"too short", []byte{0}, 0, false,
}, {
"just enough", []byte{1, 0}, 1, true,
}, {
"longer", []byte{1, 0, 2}, 1, true,
}}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.in)
res, ok := b.Uint16()
if res != tc.res {
t.Errorf("got: %d, want: %d", res, tc.res)
}
if ok != tc.ok {
t.Errorf("ok: got: %t, want: %t", ok, tc.ok)
}
})
}
}
var searchTestcases = []struct {
name string
haystack string
needle string
flags int
expect int
}{{
"empty", "", "", 0, 0,
}, {
"empty compact ws", "", "", CompactWS, 0,
}, {
"empty ignore case", "", "", IgnoreCase, 0,
}, {
"simple", "abc", "abc", 0, 0,
}, {
"simple compact ws", "abc", "abc", CompactWS, 0,
}, {
"simple ignore case", "abc", "abc", IgnoreCase, 0,
}, {
"ignore case 1 upper", "aBc", "ABC", IgnoreCase, 0,
}, {
"ignore case prefixed", "aaBcß", "ABC", IgnoreCase, 1,
}, {
"ignore case prefixed utf8", "ßaBcß", "ABC", IgnoreCase, 2, // 2 because ß is 2 bytes long
}, {
"simple compact ws and ignore case", " a", " A", CompactWS | IgnoreCase, 0,
}, {
"simple compact ws and ignore prefix", "a a", " A", CompactWS | IgnoreCase, 1,
}}
func TestSearch(t *testing.T) {
for _, tc := range searchTestcases {
t.Run(tc.name, func(t *testing.T) {
b := Bytes(tc.haystack)
i := b.Search([]byte(tc.needle), tc.flags)
if i != tc.expect {
t.Errorf("got: %d, want: %d", i, tc.expect)
}
})
}
}
func FuzzSearch(f *testing.F) {
for _, tc := range searchTestcases {
f.Add([]byte(tc.haystack), []byte(tc.needle), tc.flags)
}
f.Fuzz(func(t *testing.T, haystack, needle []byte, flags int) {
b := Bytes(haystack)
b.Search(needle, flags%CompactWS|IgnoreCase)
})
}
|