forked from vektah/goparsify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate_test.go
61 lines (49 loc) · 1.41 KB
/
state_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
package goparsify
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestState_Advance(t *testing.T) {
ps := NewState("fooo")
require.Equal(t, 0, ps.Pos)
ps.Advance(2)
require.Equal(t, 2, ps.Pos)
ps.Advance(1)
require.Equal(t, 3, ps.Pos)
}
func TestState_Get(t *testing.T) {
ps := NewState("fooo")
require.Equal(t, "fooo", ps.Get())
ps.Advance(1)
require.Equal(t, "ooo", ps.Get())
ps.Advance(4)
require.Equal(t, "", ps.Get())
ps.Advance(10)
require.Equal(t, "", ps.Get())
}
func TestState_Errors(t *testing.T) {
ps := NewState("fooo")
ps.ErrorHere("hello")
require.Equal(t, "offset 0: expected hello", ps.Error.Error())
require.Equal(t, 0, ps.Error.Pos())
require.True(t, ps.Errored())
ps.Recover()
require.False(t, ps.Errored())
ps.Advance(2)
ps.ErrorHere("hello2")
require.Equal(t, "offset 2: expected hello2", ps.Error.Error())
require.Equal(t, 2, ps.Error.Pos())
require.True(t, ps.Errored())
}
func TestState_Preview(t *testing.T) {
require.Equal(t, "", NewState("").Preview(10))
require.Equal(t, "asdf", NewState("asdf").Preview(10))
require.Equal(t, "asdfasdfas", NewState("asdfasdfasdf").Preview(10))
}
func TestWhitespaces(t *testing.T) {
p := Many(Any("hello", "world", "!"))
_, err := Run(p, "hello world\u2005!", ASCIIWhitespace)
require.Equal(t, "left unparsed: \u2005!", err.Error())
_, err = Run(p, "hello world\u2005!", UnicodeWhitespace)
require.NoError(t, err)
}