parser - byteparse_test.go
1 package parser_test
2
3 import (
4 "fmt"
5 "testing"
6
7 "vimagination.zapto.org/parser"
8 )
9
10 func testTokeniserAccept(t *testing.T, p parser.Tokeniser) {
11 p.Accept("ABCD")
12 if s := p.Get(); s != "A" {
13 t.Errorf("expecting \"A\", got %q", s)
14 return
15 }
16 p.Accept("ABCD")
17 if s := p.Get(); s != "B" {
18 t.Errorf("expecting \"B\", got %q", s)
19 return
20 }
21 p.Accept("ABCD")
22 if s := p.Get(); s != "C" {
23 t.Errorf("expecting \"C\", got %q", s)
24 return
25 }
26 p.Accept("ABCD")
27 if s := p.Get(); s != "" {
28 t.Errorf("expecting \"\", got %q", s)
29 return
30 }
31 p.Accept("£")
32 if s := p.Get(); s != "£" {
33 t.Errorf("expecting \"£\", got %q", s)
34 return
35 }
36 }
37
38 func TestByteAccept(t *testing.T) {
39 testTokeniserAccept(t, parser.NewByteTokeniser([]byte("ABC£")))
40 }
41
42 func testTokeniserAcceptRun(t *testing.T, p parser.Tokeniser) {
43 p.AcceptRun("0123456789")
44 if s := p.Get(); s != "123" {
45 t.Errorf("expecting \"123\", got %q", s)
46 return
47 }
48 p.AcceptRun("ABC")
49 if s := p.Get(); s != "ABC" {
50 t.Errorf("expecting \"ABC\", got %q", s)
51 return
52 }
53 p.AcceptRun("£$%^")
54 if s := p.Get(); s != "££$$%%^^" {
55 t.Errorf("expecting \"££$$%%^^\", got %q", s)
56 return
57 }
58 p.AcceptRun("\n")
59 if s := p.Get(); s != "\n" {
60 t.Errorf("expecting \"\\n\", got %q", s)
61 return
62 }
63 }
64
65 func TestByteAcceptRun(t *testing.T) {
66 testTokeniserAcceptRun(t, parser.NewByteTokeniser([]byte("123ABC££$$%%^^\n")))
67 }
68
69 func testTokeniserExcept(t *testing.T, p parser.Tokeniser) {
70 p.Except("1")
71 if s := p.Get(); s != "" {
72 t.Errorf("expecting \"\", got %q", s)
73 }
74 p.Except("2")
75 if s := p.Get(); s != "1" {
76 t.Errorf("expecting \"1\", got %q", s)
77 }
78 p.Except("2")
79 if s := p.Get(); s != "" {
80 t.Errorf("expecting \"\", got %q", s)
81 }
82 p.Except("!")
83 if s := p.Get(); s != "2" {
84 t.Errorf("expecting \"2\", got %q", s)
85 }
86 p.Except("!")
87 if s := p.Get(); s != "3" {
88 t.Errorf("expecting \"3\", got %q", s)
89 }
90 p.Except("!")
91 if s := p.Get(); s != "" {
92 t.Errorf("expecting \"\", got %q", s)
93 }
94 }
95
96 func TestByteExcept(t *testing.T) {
97 testTokeniserExcept(t, parser.NewByteTokeniser([]byte("123")))
98 }
99
100 func testTokeniserExceptRun(t *testing.T, p parser.Tokeniser) {
101 p.ExceptRun("\n")
102 if s := p.Get(); s != "12345ABC" {
103 t.Errorf("expecting \"12345ABC\", got %q", s)
104 return
105 }
106 p.Except("")
107 p.Get()
108 p.ExceptRun("\n")
109 if s := p.Get(); s != "67890DEF" {
110 t.Errorf("expecting \"67890DEF\", got %q", s)
111 return
112 }
113 p.Except("")
114 p.Get()
115 p.ExceptRun("")
116 if s := p.Get(); s != "OH MY!" {
117 t.Errorf("expecting \"OH MY!\", got %q", s)
118 return
119 }
120 }
121
122 func TestByteExceptRun(t *testing.T) {
123 testTokeniserExceptRun(t, parser.NewByteTokeniser([]byte("12345ABC\n67890DEF\nOH MY!")))
124 }
125
126 func ExampleNewByteTokeniser() {
127 p := parser.NewByteTokeniser([]byte("Hello, World!"))
128 alphaNum := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
129 p.AcceptRun(alphaNum)
130 word := p.Get()
131 fmt.Println("got word:", word)
132
133 p.ExceptRun(alphaNum)
134 p.Get()
135
136 p.AcceptRun(alphaNum)
137 word = p.Get()
138 fmt.Println("got word:", word)
139 // Output:
140 // got word: Hello
141 // got word: World
142 }