-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner_test.go
135 lines (128 loc) · 2.7 KB
/
scanner_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"io"
"strings"
"testing"
)
func TestScanner(t *testing.T) {
src := `
[section]
foo=bar
number=1234
phone_number=+1234
; this is a comment
[section2]
foo-dash=bar
`
s := NewScanner(strings.NewReader(src))
var tok *Token
var err error
for err == nil {
tok, err = s.Scan()
if err != nil {
if err.Error() != io.EOF.Error() {
t.Fatal(err)
}
}
if tok != nil {
//fmt.Println(tok.Line, ":", tok.Column, " ", tok.Text)
switch tok.Line {
case 1:
if tok.Column > 0 {
v := "[section]"
expect := string(v[tok.Column-1])
if tok.Text != expect {
t.Errorf("expected %s fot %s", expect, tok.Text)
}
}
case 2:
if tok.Column > 0 {
v := "foo=bar"
expect := string(v[tok.Column-1])
if tok.Text != expect {
t.Errorf("expected %s fot %s", expect, tok.Text)
}
}
case 3:
if tok.Column > 0 {
v := "number=1234"
expect := string(v[tok.Column-1])
if tok.Text != expect {
t.Errorf("expected %s fot %s", expect, tok.Text)
}
}
case 4:
if tok.Column > 0 {
v := "phone_number=+1234"
expect := string(v[tok.Column-1])
if tok.Text != expect {
t.Errorf("expected %s fot %s", expect, tok.Text)
}
}
case 5:
if tok.Column > 0 {
v := "[section]"
expect := string(v[tok.Column-1])
if tok.Text != expect {
t.Errorf("expected %s fot %s", expect, tok.Text)
}
}
case 6:
if s.column == 1 {
v := "; this is a comment"
if tok.Text != v {
t.Errorf("expected comment %s got %s", v, tok.Text)
}
}
case 7:
if tok.Column > 0 {
v := "[section]"
expect := string(v[tok.Column-1])
if tok.Text != expect {
t.Errorf("expected %s fot %s", expect, tok.Text)
}
}
case 8:
if tok.Column > 0 {
v := "[section2]"
expect := string(v[tok.Column-1])
if tok.Text != expect {
t.Errorf("expected %s fot %s", expect, tok.Text)
}
}
case 9:
if tok.Column > 0 {
v := "foo-dash=bar"
expect := string(v[tok.Column-1])
if tok.Text != expect {
t.Errorf("expected %s fot %s", expect, tok.Text)
}
}
}
}
}
}
func TestScanBlockComments(t *testing.T) {
src := `[section-name]
setting=true
;-- this is a block comment that begins on this line
and continues across multiple lines, until we
get to here --;`
s := NewScanner(strings.NewReader(src))
var tok *Token
var err error
for err == nil {
tok, err = s.Scan()
if err != nil {
if err.Error() != io.EOF.Error() {
t.Fatal(err)
}
}
if tok != nil {
txt := string(src[tok.Begin:tok.End])
if txt != tok.Text {
t.Errorf("expected %s got %s", tok.Text, txt)
}
}
}
}