-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathregexp_parse_test.go
117 lines (111 loc) · 2.28 KB
/
regexp_parse_test.go
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
package quamina
import (
"errors"
"testing"
"unicode/utf8"
)
func TestBasicRunelist(t *testing.T) {
bytes := []byte("foo")
r := newRxParseState(bytes)
for i, b := range bytes {
next, err := r.nextRune()
if err != nil {
t.Errorf("err at %d", i)
}
if next != rune(b) {
t.Errorf("mismatch at %d", i)
}
}
_, err := r.nextRune()
if !errors.Is(err, errRegexpEOF) {
t.Error("missed EOF")
}
if !r.isEmpty() {
t.Error("missed empty")
}
}
func TestBadUTF8(t *testing.T) {
bad := []byte{0xF8}
ps := newRxParseState(bad)
_, err := ps.nextRune()
if err == nil {
t.Error("bad UTF8")
}
}
func TestVariablePlaneRunelist(t *testing.T) {
runes := []rune{'&', 0x416, 0x4E2D, 0x10346}
lengths := []int{1, 2, 3, 4}
list := newRxParseState([]byte(string(runes)))
read := 0
for i := range runes {
r, err := list.nextRune()
read += utf8.RuneLen(r)
if err != nil {
t.Errorf("err at %d", i)
}
if r != runes[i] {
t.Errorf("mismatch at %d", i)
}
if utf8.RuneLen(r) != lengths[i] {
t.Errorf("length mismatch at %d", i)
}
if read != list.offset() {
t.Errorf("wrong length at %d", i)
}
}
if !list.isEmpty() {
t.Error("Missed empty")
}
for i := 3; i >= 0; i-- {
list.backup1(runes[i])
read -= utf8.RuneLen(runes[i])
if list.offset() != read {
t.Errorf("wrong offset at %d", i)
}
}
if list.offset() != 0 {
t.Error("offset not 0")
}
}
func TestRuneListRequire(t *testing.T) {
r := newRxParseState([]byte("foo"))
err := r.require('f')
if err != nil {
t.Error("require mode 1")
}
r = newRxParseState([]byte("foo"))
err = r.require('É')
if err == nil {
t.Error("require mode 2")
}
r = newRxParseState([]byte("Éé"))
err = r.require('É')
if err != nil {
t.Error("require mode 3")
}
r = newRxParseState([]byte("Éé"))
err = r.require('é')
if err == nil {
t.Error("require mode 4")
}
}
func TestRuneListBypass(t *testing.T) {
r := newRxParseState([]byte("Éé"))
_, err := r.bypassOptional('é')
if err != nil {
t.Error("bypass mode 1")
}
next, err := r.nextRune()
if err != nil || next != 'É' {
t.Error("bypass mode 2")
}
r = newRxParseState([]byte("Éé"))
_, err = r.bypassOptional('x')
if err != nil {
t.Error("bypass mode 3")
}
next, err = r.nextRune()
if err != nil || next != 'É' {
t.Error("bypass mode 4")
}
}