-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
118 lines (108 loc) · 3.06 KB
/
reader_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
118
package tok
import (
"testing"
"unicode/utf8"
)
func TestReader(t *testing.T) {
cases := []struct {
inp string
reader Reader
tail string
}{
{
"var i = \n 456;",
Seq(Fold("VAR"), WS(), Lit("i ="), Many(WS()), Many(Between('0', '9')), Lit(";")),
"",
},
{
"var i = \n 456;",
Seq(Lit("var i ="), Many(WS()), Many(Digit()), Lit(";")),
"",
},
}
for i, c := range cases {
sca := NewScanner(c.inp)
err := sca.Use(c.reader)
if err != nil {
t.Errorf("%d unexpected error: %v", i, err)
}
if c.tail != sca.Tail() {
t.Errorf("%d unexpected tail value: %q != %q", i, c.tail, sca.Tail())
}
}
}
func TestReaderWhat(t *testing.T) {
cases := []struct {
r Reader
exp string
}{
{Any(Rune('!'), Lit("abc")), `[ '!' "abc" ]`},
{Between('!', '☃'), `<!☃>`},
{Between(0, utf8.MaxRune), `<\x00\U0010ffff>`},
{Between('a', 'a'), `'a'`},
{BetweenAny("a-zA-Z"), `[< az AZ >]`},
{BetweenAny("a-zA-Z_-_"), `[< az AZ __ >]`},
{Set("a-zA-Z", "_-+"), `[< az AZ "_-+" >]`},
{Fold("true"), `~"true"`},
{Holey('a', 'z', "ox"), `(<az> - "ox")`},
{Not(Rune('A')), `!'A'`},
{Seq(Rune('!'), Many(AnyRune(" +-")), Lit("abc")), `'!' +[" +-"] "abc"`},
{To(Bool("")), `->bool{""}`},
{Uint(16, 64), `uint{16,64}`},
{WS(), `[" \r\n\t"]`},
{Zom(WS()), `*[" \r\n\t"]`},
}
for i, c := range cases {
if c.r.What() != c.exp {
t.Errorf("%d unexpected what message: %s", i, c.r.What())
}
}
}
func TestJanus(t *testing.T) {
check := func(str, expStr string, e error, expE bool) {
if str != expStr {
t.Errorf("unexpected string: %q != %q", str, expStr)
}
if (expE && e == nil) && (!expE && e != nil) {
t.Errorf("unexpected error value: %v", e)
}
}
beg, end := Janus("i", Many(Between('a', 'z')))
str, err := NewScanner("two two").CaptureUse(Seq(beg, WS(), end))
check(str, "two two", err, false)
beg, end = Janus("c", Many(Rune('=')))
comBeg := Seq(Rune('['), beg, Rune('['))
comEnd := Seq(Rune(']'), end, Rune(']'))
str, err = NewScanner("[==[long lua string]==] ~=").CaptureUse(Seq(comBeg, To(comEnd)))
check(str, "[==[long lua string", err, false)
str, err = NewScanner("[==[long lua string]==] ~=").CaptureUse(Seq(comBeg, Past(comEnd)))
check(str, "[==[long lua string]==]", err, false)
}
func TestNot(t *testing.T) {
check := func(str, expStr string, e error, expE bool) {
if str != expStr {
t.Errorf("unexpected string: %q != %q", str, expStr)
}
if (expE && e == nil) && (!expE && e != nil) {
t.Errorf("unexpected error value: %v", e)
}
}
notA := Not(AnyRune("aA"))
str, err := NewScanner("XYZAa").CaptureUse(notA)
check(str, "X", err, false)
str, err = NewScanner("aAbB").CaptureUse(notA)
check(str, "", err, true)
str, err = NewScanner("XYZAa").CaptureUse(Many(notA))
check(str, "XYZ", err, false)
}
func TestInvalidReader(t *testing.T) {
var r Reader
r = Seq(To(Any(12)))
if !HasInvalidReader(r.What()) {
t.Errorf("expected %s in: %s", InvalidReaderMarker, r.What())
}
r = Seq(To(Any("s")))
if HasInvalidReader(r.What()) {
t.Errorf("unexpected %s in: %s", InvalidReaderMarker, r.What())
}
}