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
|
package parser
import (
"bufio"
"io"
"strings"
"testing"
)
func TestPeekRune(t *testing.T) {
s := "Ta’ Ħaġrat"
reader := reader{r: bufio.NewReader(strings.NewReader(s))}
for _, r := range s {
r1, err := reader.peekRune()
if err != nil && !(err == io.EOF && r == 't') {
t.Fatalf("reader.peekRune() failed with ‘%T’ at rune ‘%c’", err, r)
}
r2, _ := reader.readRune()
if r1 != r2 {
t.Fatalf("reader.peekRune() peaked ‘%c’ but the next read rune was ‘%c’", r1, r2)
}
}
}
func TestUnreadRune(t *testing.T) {
s := "Ta’ Ħaġrat"
reader := reader{r: bufio.NewReader(strings.NewReader(s))}
for range s {
r1, _ := reader.readRune()
if err := reader.unreadRune(); err != nil {
t.Fatalf("reader.unreadRune() failed with ‘%T’ when unreading rune ‘%c’", err, r1)
}
r2, _ := reader.readRune()
if r1 != r2 {
t.Fatalf("reader.readRune() returned ‘%c’ after unreading ‘%c’", r2, r1)
}
}
}
func TestUnreadRunePosTracking(t *testing.T) {
s := "Ta’ Ħaġrat\nIJsselmeer"
reader := reader{r: bufio.NewReader(strings.NewReader(s))}
for i := range []rune(s) {
r, _ := reader.readRune()
if err := reader.unreadRune(); err != nil {
t.Fatalf("reader.unreadRune() failed with ‘%T’ when unreading rune ‘%c’", err, r)
}
// Ensure we tracked the row correctly
switch {
case i < 11 && reader.pos.row != 0:
t.Fatalf("Expected reader to be on row 0 after unreading rune ‘%c’ but got row %d", r, reader.pos.row)
case i >= 11 && reader.pos.row != 1:
t.Fatalf("Expected reader to be on row 1 after unreading rune ‘%c’ but got row %d", r, reader.pos.row)
}
// Ensure we tracked the column correctly
switch {
case i < 11 && reader.pos.col != uint(i):
t.Fatalf("Expected reader to be on col %d after unreading rune ‘%c’ but got col %d", i, r, reader.pos.col)
case i == 11 && reader.pos.col != 0:
t.Fatalf("Expected reader to be on col 0 after unreading rune ‘\\n’ but got col %d", reader.pos.col)
case i > 11 && reader.pos.col != uint(i)-11:
t.Fatalf("Expected reader to be on col %d after unreading rune ‘%c’ but got col %d", i-10, r, reader.pos.col)
}
reader.readRune()
}
}
func TestReadRune(t *testing.T) {
s := "Ta’ Ħaġrat"
reader := reader{r: bufio.NewReader(strings.NewReader(s))}
for _, r1 := range s {
r2, err := reader.readRune()
if err != nil {
t.Fatalf("reader.readRune() failed with ‘%T’", err)
}
if r1 != r2 {
t.Fatalf("reader.readRune() read ‘%c’ but ‘%c’ was expected", r1, r2)
}
}
_, err := reader.readRune()
switch {
case err == nil:
t.Fatal("reader.readRune() expected to fail but didn’t")
case err != io.EOF:
t.Fatalf("reader.readRune() expected to fail with io.EOF but got ‘%T’", err)
}
}
func TestReadRunePosTracking(t *testing.T) {
s := "Ta’ Ħaġrat\nIJsselmeer"
line1 := true
reader := reader{r: bufio.NewReader(strings.NewReader(s))}
for i := range []rune(s) {
// Track the line we’re on
r, _ := reader.readRune()
if r == '\n' {
line1 = false
}
// Ensure we tracked the row correctly
switch {
case line1 && reader.pos.row != 0:
t.Fatalf("Expected reader to be on row 0 when reading rune ‘%c’ but got row %d", r, reader.pos.row)
case !line1 && reader.pos.row != 1:
t.Fatalf("Expected reader to be on row 1 when reading rune ‘%c’ but got row %d", r, reader.pos.row)
}
// Ensure we tracked the column correctly
switch {
case i < 10 && reader.pos.col != uint(i)+1:
t.Fatalf("Expected reader to be on col %d when reading rune ‘%c’ but got col %d", i, r, reader.pos.col)
case i == 10 && reader.pos.col != 0:
t.Fatalf("Expected reader to be on col 0 when reading rune ‘\\n’ but got col %d", reader.pos.col)
case i > 10 && reader.pos.col != uint(i)-10:
t.Fatalf("Expected reader to be on col %d when reading rune ‘%c’ but got col %d", i-10, r, reader.pos.col)
}
}
}
func TestReadNonSpaceRune(t *testing.T) {
s := "Ta’ Ħaġrat \t\n IJsselmeer"
reader := reader{r: bufio.NewReader(strings.NewReader(s))}
readUntil := func(target rune) {
for {
if r, _ := reader.readRune(); r == target {
break
}
}
}
assertNext := func(target rune) {
if r, err := reader.readNonSpaceRune(); err != nil {
t.Fatalf("reader.readNonSpaceRune() failed with ‘%T’", err)
} else if r != target {
t.Fatalf("reader.readNonSpaceRune() expected to return ‘%c’ but returned ‘%c’", target, r)
}
}
readUntil('’')
assertNext('Ħ')
readUntil('t')
assertNext('IJ')
}
func TestSkipSpaces(t *testing.T) {
s := "Ta’ Ħaġrat \t\n IJsselmeer"
reader := reader{r: bufio.NewReader(strings.NewReader(s))}
readUntil := func(target rune) {
for {
if r, _ := reader.readRune(); r == target {
break
}
}
}
assertNext := func(target rune) {
if err := reader.skipSpaces(); err != nil {
t.Fatalf("reader.skipSpaces() failed with ‘%T’", err)
}
if r, _ := reader.readRune(); r != target {
t.Fatalf("reader.readRune() expected to return ‘%c’ but returned ‘%c’", target, r)
}
}
readUntil('’')
assertNext('Ħ')
readUntil('t')
assertNext('IJ')
}
|