gigaparsec/parser_test.go

53 lines
1.6 KiB
Go

package gigaparsec_test
import (
"bytes"
"testing"
"git.codemonkeysoftware.net/b/gigaparsec"
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
"github.com/shoenig/test"
"pgregory.net/rapid"
)
func Todo(t *testing.T) {
t.Errorf("TODO")
}
func notP[T any](pred func(T) bool) func(T) bool {
return func(x T) bool { return !pred(x) }
}
func hasPrefix(prefix []byte) func([]byte) bool {
return func(b []byte) bool { return bytes.HasPrefix(b, prefix) }
}
func TestSlice(t *testing.T) {
t.Run("fails with wrong contents", rapid.MakeCheck(func(t *rapid.T) {
s := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "s")
input := rapid.SliceOfN(rapid.Byte(), len(s), -1).
Filter(notP(hasPrefix(s))).Draw(t, "input")
out, err := gigaparsec.Parse(gigaparsec.Slice(s), cursor.NewSlice(input))
test.ErrorAs(t, err, &gigaparsec.ParseError{})
test.SliceEmpty(t, out)
}))
t.Run("fails at end of input", rapid.MakeCheck(func(t *rapid.T) {
s := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "s")
inputLen := rapid.IntRange(0, len(s)-1).Draw(t, "inputLen")
input := s[:inputLen]
out, err := gigaparsec.Parse(gigaparsec.Slice(s), cursor.NewSlice(input))
test.ErrorAs(t, err, &gigaparsec.ParseError{})
test.SliceEmpty(t, out)
}))
t.Run("fails when read fails", Todo)
t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) {
input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input")
sLen := rapid.IntRange(0, len(input)).Draw(t, "sLen")
s := input[:sLen]
out, err := gigaparsec.Parse(gigaparsec.Slice(s), cursor.NewSlice(input))
test.NoError(t, err)
test.SliceEqOp(t, s, out)
}))
t.Run("next state has correct position", Todo)
}