-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex_test.go
69 lines (62 loc) · 1.45 KB
/
lex_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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hu
import (
"reflect"
"strings"
"testing"
)
type lexTest struct {
name string
input string
items []item
}
var (
tEOF = item{itemEOF, ""}
tQuote = item{itemString, `"abc \n\t\" "`}
)
var lexTests = []lexTest{
{"empty", "", []item{tEOF}},
{"words", "Red lentil soup",
[]item{
{itemWord, "Red"}, {itemSpace, " "},
{itemWord, "lentil"}, {itemSpace, " "},
{itemWord, "soup"},
tEOF}},
{"number and word", "1 onion",
[]item{
{itemNumber, "1"}, {itemSpace, " "},
{itemWord, "onion"},
tEOF}},
{"colon", "Photo: apple",
[]item{
{itemWord, "Photo"}, {itemPunctuation, ":"}, {itemSpace, " "},
{itemWord, "apple"},
tEOF}},
{"punctuation", "onion, chopped",
[]item{
{itemWord, "onion"}, {itemPunctuation, ","}, {itemSpace, " "},
{itemWord, "chopped"},
tEOF}},
}
// collect gathers the emitted items into a slice.
func collect(t *lexTest) (items []item) {
l := newReader(t.name, strings.NewReader(t.input))
for {
item := l.nextItem()
items = append(items, item)
if item.typ == itemEOF || item.typ == itemError {
break
}
}
return
}
func TestLex(t *testing.T) {
for _, test := range lexTests {
items := collect(&test)
if !reflect.DeepEqual(items, test.items) {
t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
}
}
}