forked from timtadh/lex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex_test.go
60 lines (52 loc) · 1.66 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
package lex
import "testing"
type testToken struct {
name string
attribute Attribute
}
type testAttr string
func (self *testToken) Name() string {
return self.name
}
func (self *testToken) Attribute() Attribute {
return self.attribute
}
func (self testAttr) String() string {
return string(self)
}
func TestLexerMatch(t *testing.T) {
var a Attribute = testAttr("hello")
t.Log(a)
var c Token = &testToken{name:"test", attribute:testAttr("hello")}
t.Log(c)
var b ProcessMatch = func(match []byte) (bool, Token) {
return false, nil
}
t.Log(b)
tokens := Lex([]*Pattern{
&Pattern{"x.yz", func(match []byte) (bool, Token) {
return true, &testToken{"xyz", testAttr(string(match))}
}},
&Pattern{"ab|cd", func(match []byte) (bool, Token) {
return true, &testToken{"ab-or-cd", testAttr(string(match))}
}},
&Pattern{"b*", func(match []byte) (bool, Token) {
return true, &testToken{"bstar", testAttr(string(match))}
}}}, []byte("xqyzabbbab"))
expected := []*testToken{
&testToken{"xyz", testAttr("xqyz")},
&testToken{"ab-or-cd", testAttr("ab")},
&testToken{"bstar", testAttr("bb")},
&testToken{"ab-or-cd", testAttr("ab")}}
i := 0
for token := range tokens {
t.Log(i, "found", token)
if expected[i].Name() != token.Name() {
t.Fatalf("Expected %v == %v", expected[i].Name(), token.Name())
}
if expected[i].Attribute().String() != token.Attribute().String() {
t.Fatalf("Expected %v == %v", expected[i].Attribute(), token.Attribute())
}
i++
}
}