2024-09-09 16:27:54 +00:00
|
|
|
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) {
|
2024-09-09 21:13:37 +00:00
|
|
|
assertParseFails := func(t rapid.TB, input []byte, p gigaparsec.Parser[byte, []byte]) (parseErr gigaparsec.ParseError) {
|
|
|
|
t.Helper()
|
|
|
|
start := gigaparsec.MakeState(cursor.NewSlice(input))
|
|
|
|
consumed, result, err := p(start)
|
|
|
|
test.ErrorAs(t, err, &parseErr, test.Sprint("expected ParseError"))
|
|
|
|
test.False(t, consumed, test.Sprint("expected consumed to be false"))
|
|
|
|
test.SliceEmpty(t, result.Value, test.Sprint("expected result value to be empty"))
|
|
|
|
if t.Failed() {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
return parseErr
|
|
|
|
}
|
|
|
|
|
2024-09-09 16:27:54 +00:00
|
|
|
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")
|
2024-09-09 21:13:37 +00:00
|
|
|
assertParseFails(t, input, gigaparsec.Slice(s))
|
2024-09-09 16:27:54 +00:00
|
|
|
}))
|
|
|
|
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]
|
2024-09-09 21:13:37 +00:00
|
|
|
assertParseFails(t, input, gigaparsec.Slice(s))
|
2024-09-09 16:27:54 +00:00
|
|
|
}))
|
|
|
|
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]
|
2024-09-09 21:00:17 +00:00
|
|
|
start := gigaparsec.MakeState(cursor.NewSlice(input))
|
|
|
|
|
|
|
|
consumed, result, err := gigaparsec.Slice(s)(start)
|
2024-09-09 16:27:54 +00:00
|
|
|
test.NoError(t, err)
|
2024-09-09 21:00:17 +00:00
|
|
|
test.True(t, consumed, test.Sprint("expected consumed to be true"))
|
|
|
|
test.SliceEqOp(t, s, result.Value)
|
|
|
|
test.EqOp(t, uint64(len(s)), result.State.Pos())
|
2024-09-09 16:27:54 +00:00
|
|
|
}))
|
|
|
|
}
|