goparsify/state_test.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

2017-08-07 12:07:29 +02:00
package goparsify
2017-08-06 15:32:10 +02:00
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestState_Advance(t *testing.T) {
2017-08-09 13:18:14 +02:00
ps := NewState("fooo")
2017-08-06 15:32:10 +02:00
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) {
2017-08-09 13:18:14 +02:00
ps := NewState("fooo")
2017-08-06 15:32:10 +02:00
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) {
2017-08-09 13:18:14 +02:00
ps := NewState("fooo")
2017-08-06 15:32:10 +02:00
ps.ErrorHere("hello")
2017-08-09 13:18:14 +02:00
require.Equal(t, "offset 0: expected hello", ps.Error.Error())
2017-08-06 15:32:10 +02:00
require.Equal(t, 0, ps.Error.Pos())
require.True(t, ps.Errored())
2017-08-09 13:18:14 +02:00
ps.Recover()
2017-08-06 15:32:10 +02:00
require.False(t, ps.Errored())
ps.Advance(2)
ps.ErrorHere("hello2")
2017-08-09 13:18:14 +02:00
require.Equal(t, "offset 2: expected hello2", ps.Error.Error())
2017-08-06 15:32:10 +02:00
require.Equal(t, 2, ps.Error.Pos())
require.True(t, ps.Errored())
}
2017-08-10 13:58:14 +02:00
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))
}
2017-08-13 07:28:43 +02:00
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)
}