-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_stream_test.go
52 lines (48 loc) · 988 Bytes
/
token_stream_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
package goply_test
import (
"testing"
"github.com/nayas360/goply"
)
func TestTokenStream_PosDelta(t *testing.T) {
l := makeLexer()
tokens, err := l.GetTokenStream("123 456")
if err != nil {
t.Error(err)
}
if tokens.Pos() != 0 {
t.Error("pos should start from 0")
}
tokens.PosDelta(1)
if tokens.Pos() != 1 {
t.Error("pos should be at 1")
}
if tokens.PosDelta(1).Pos() != 2 {
t.Error("pos should be at 2")
}
if !tokens.EOS() {
t.Error("should be end of stream now")
}
tokens.ResetPos()
if tokens.Pos() != 0 {
t.Error("should be 0")
}
if tokens.PosDelta(2).ResetPos().Pos() != 0 {
t.Error("should be 0")
}
if !tokens.PosDelta(2).EOS() {
t.Error("should be end of stream now")
}
tokens.PosDelta(-1)
if tokens.Pos() != 1 {
t.Error("pos should be at 1")
}
if tokens.PosDelta(-1).Pos() != 0 {
t.Error("pos should be at 0")
}
}
func makeLexer() *goply.Lexer {
l := goply.NewLexer(true)
l.AddRule("num", "[0-9]+")
l.Ignore("\\s+")
return l
}