forked from benbjohnson/ego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
181 lines (159 loc) · 5.99 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
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
179
180
181
package ego_test
import (
"bytes"
"io"
"testing"
. "github.com/benbjohnson/ego"
"github.com/stretchr/testify/assert"
)
// Ensure that a text block can be scanned.
func TestScannerTextBlock(t *testing.T) {
s := NewScanner(bytes.NewBufferString("hello world"), "tmpl.ego")
b, err := s.Scan()
assert.NoError(t, err)
if b, ok := b.(*TextBlock); assert.True(t, ok) {
assert.Equal(t, b.Content, "hello world")
assert.Equal(t, b.Pos, Pos{Path: "tmpl.ego", LineNo: 1})
}
}
// Ensure that a text block with a single "<" returns.
func TestScannerTextBlockSingleLT(t *testing.T) {
s := NewScanner(bytes.NewBufferString("<"), "tmpl.ego")
b, err := s.Scan()
assert.NoError(t, err)
if b, ok := b.(*TextBlock); assert.True(t, ok) {
assert.Equal(t, b.Content, "<")
}
}
// Ensure that a text block starting with a "<" returns.
func TestScannerTextBlockStartingLT(t *testing.T) {
s := NewScanner(bytes.NewBufferString("<html>"), "tmpl.ego")
b, err := s.Scan()
assert.NoError(t, err)
if b, ok := b.(*TextBlock); assert.True(t, ok) {
assert.Equal(t, b.Content, "<html>")
}
}
// Ensure that a code block can be scanned.
func TestScannerCodeBlock(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<% x := 1 %>`), "tmpl.ego")
b, err := s.Scan()
assert.NoError(t, err)
if b, ok := b.(*CodeBlock); assert.True(t, ok) {
assert.Equal(t, b.Content, ` x := 1 `)
assert.Equal(t, b.Pos, Pos{Path: "tmpl.ego", LineNo: 1})
}
}
// Ensure that a code block that ends unexpectedly returns an error.
func TestScannerCodeBlockUnexpectedEOF_1(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%`), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a code block that ends unexpectedly returns an error.
func TestScannerCodeBlockUnexpectedEOF_2(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<% x = 2`), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a code block that ends unexpectedly returns an error.
func TestScannerCodeBlockUnexpectedEOF_3(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<% x = 2 %`), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a code block that ends unexpectedly returns an error.
func TestScannerCodeBlockUnexpectedEOF_4(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<% x = 2 % `), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a header block can be scanned.
func TestScannerHeaderBlock(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%% import "foo" %%>`), "tmpl.ego")
b, err := s.Scan()
assert.NoError(t, err)
if b, ok := b.(*HeaderBlock); assert.True(t, ok) {
assert.Equal(t, b.Content, ` import "foo" `)
assert.Equal(t, b.Pos, Pos{Path: "tmpl.ego", LineNo: 1})
}
}
// Ensure that a header block that ends unexpectedly returns an error.
func TestScannerHeaderBlockUnexpectedEOF_1(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%% import "foo" `), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a header block that ends unexpectedly returns an error.
func TestScannerHeaderBlockUnexpectedEOF_2(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%% import "foo" %`), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a header block that ends unexpectedly returns an error.
func TestScannerHeaderBlockUnexpectedEOF_3(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%% import "foo" % `), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a header block that ends unexpectedly returns an error.
func TestScannerHeaderBlockUnexpectedEOF_4(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%% import "foo" %%`), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a header block that ends unexpectedly returns an error.
func TestScannerHeaderBlockUnexpectedEOF_5(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%% import "foo" %% `), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a print block can be scanned.
func TestScannerPrintBlock(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%= myNum %>`), "tmpl.ego")
b, err := s.Scan()
assert.NoError(t, err)
if b, ok := b.(*PrintBlock); assert.True(t, ok) {
assert.Equal(t, b.Content, ` myNum `)
assert.Equal(t, b.Pos, Pos{Path: "tmpl.ego", LineNo: 1})
}
}
// Ensure that a print block that ends unexpectedly returns an error.
func TestScannerPrintBlockUnexpectedEOF(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%= `), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that a declaration block can be scanned.
func TestScannerDeclarationBlock(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%! MyTemplate(w io.Writer) error %>`), "tmpl.ego")
b, err := s.Scan()
assert.NoError(t, err)
if b, ok := b.(*DeclarationBlock); assert.True(t, ok) {
assert.Equal(t, b.Content, ` MyTemplate(w io.Writer) error `)
assert.Equal(t, b.Pos, Pos{Path: "tmpl.ego", LineNo: 1})
}
}
// Ensure that a declaration block that ends unexpectedly returns an error.
func TestScannerDeclarationBlockUnexpectedEOF(t *testing.T) {
s := NewScanner(bytes.NewBufferString(`<%! `), "tmpl.ego")
_, err := s.Scan()
assert.Equal(t, err, io.ErrUnexpectedEOF)
}
// Ensure that line numbers are tracked correctly.
func TestScannerMultiline(t *testing.T) {
s := NewScanner(bytes.NewBufferString("hello\nworld<%= x \n\n %>goodbye"), "tmpl.ego")
b, _ := s.Scan()
assert.Equal(t, b.(*TextBlock).Pos, Pos{Path: "tmpl.ego", LineNo: 1})
b, _ = s.Scan()
assert.Equal(t, b.(*PrintBlock).Pos, Pos{Path: "tmpl.ego", LineNo: 2})
b, _ = s.Scan()
assert.Equal(t, b.(*TextBlock).Pos, Pos{Path: "tmpl.ego", LineNo: 4})
}
// Ensure that EOF returns an error.
func TestScannerEOF(t *testing.T) {
s := NewScanner(bytes.NewBuffer(nil), "tmpl.ego")
b, err := s.Scan()
assert.Equal(t, err, io.EOF)
assert.Nil(t, b)
}