-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunereader_test.go
112 lines (94 loc) · 2.6 KB
/
runereader_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
/*
* This file is part of the Peek-A-Buf package.
*
* (c) Philip Lehmann-Böhm <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package peekabuf
import (
"bytes"
"testing"
)
func TestNewRuneReader(t *testing.T) {
b := NewRuneReader(bytes.NewBufferString("peekabuf"))
if b == nil {
t.Error("expected a RuneReader pointer but got nil")
}
}
func TestRead(t *testing.T) {
r := NewRuneReader(bytes.NewBufferString("peekabuf"))
expected := []rune{'p', 'e', 'e', 'k', 'a', 'b', 'u', 'f', EOF, EOF}
for _, expectedRune := range expected {
actualRune := r.Read()
if actualRune != expectedRune {
t.Errorf("expected rune to be %s but got %s", string(expectedRune), string(actualRune))
}
}
}
func TestUnread(t *testing.T) {
r := NewRuneReader(bytes.NewBufferString("pab"))
r.Unread()
checkRune(t, r, 'p')
r.Unread()
checkRune(t, r, 'p')
r.Unread()
r.Unread()
checkRune(t, r, 'p')
expected := []rune{'a', 'b', EOF, EOF}
for _, toCheck := range expected {
r.Read()
r.Unread()
checkRune(t, r, toCheck)
}
}
func TestPeek(t *testing.T) {
r := NewRuneReader(bytes.NewBufferString("pab"))
peeked, err := r.Peek(2)
if err != nil {
t.Errorf("expected no error but got %s", err)
}
if len(peeked) != 2 {
t.Errorf("expected 2 peeked runes but got %d", len(peeked))
}
if peeked[0] != 'p' || peeked[1] != 'a' {
t.Errorf("expected 'p' and 'a' but got '%s' and '%s'", string(peeked[0]), string(peeked[1]))
}
checkRune(t, r, 'p')
checkRune(t, r, 'a')
checkRune(t, r, 'b')
checkRune(t, r, EOF)
peeked, err = r.Peek(2)
if err != nil {
t.Errorf("expected no error but got %s", err)
}
if len(peeked) != 1 {
t.Errorf("expected 1 peeked rune but got %d", len(peeked))
}
if peeked[0] != EOF {
t.Errorf("expected EOL but got '%s'", string(peeked[0]))
}
r = NewRuneReader(bytes.NewBufferString("pab"))
peeked, err = r.Peek(4)
if err != nil {
t.Errorf("expected no error but got %s", err)
}
if len(peeked) != 4 {
t.Errorf("expected 4 peeked runes but got %d", len(peeked))
}
if peeked[0] != 'p' || peeked[1] != 'a' || peeked[2] != 'b' || peeked[3] != EOF {
t.Errorf("expected 'p', 'a', 'b' and EOL but got '%s', '%s', '%s' and '%s'",
string(peeked[0]), string(peeked[1]), string(peeked[2]), string(peeked[3]))
}
checkRune(t, r, 'p')
checkRune(t, r, 'a')
checkRune(t, r, 'b')
checkRune(t, r, EOF)
}
func checkRune(t *testing.T, r *RuneReader, expected rune) {
read := r.Read()
if read != expected {
t.Errorf("expected rune to be %s but got %s", string(expected), string(read))
}
}