Fix prefix filter

This commit is contained in:
Brandon Dyck 2024-09-10 09:30:54 -06:00
parent 87c54ca12b
commit 14d2418d2c

View File

@ -14,18 +14,14 @@ func Todo(t *testing.T) {
t.Errorf("TODO") t.Errorf("TODO")
} }
func curry[A, B, C any](f func(A, B) C) func(A) func(B) C {
return func(a A) func(B) C {
return func(b B) C {
return f(a, b)
}
}
}
func not[T any](pred func(T) bool) func(T) bool { func not[T any](pred func(T) bool) func(T) bool {
return func(x T) bool { return !pred(x) } 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) { func TestSlice(t *testing.T) {
assertParseFails := func(t rapid.TB, input []byte, p gigaparsec.Parser[byte, []byte]) (parseErr gigaparsec.ParseError) { assertParseFails := func(t rapid.TB, input []byte, p gigaparsec.Parser[byte, []byte]) (parseErr gigaparsec.ParseError) {
t.Helper() t.Helper()
@ -43,7 +39,7 @@ func TestSlice(t *testing.T) {
t.Run("fails with wrong contents", rapid.MakeCheck(func(t *rapid.T) { t.Run("fails with wrong contents", rapid.MakeCheck(func(t *rapid.T) {
s := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "s") s := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "s")
input := rapid.SliceOfN(rapid.Byte(), len(s), -1). input := rapid.SliceOfN(rapid.Byte(), len(s), -1).
Filter(not(curry(bytes.HasPrefix)(s))).Draw(t, "input") Filter(not(hasPrefix(s))).Draw(t, "input")
assertParseFails(t, input, gigaparsec.Slice(s)) assertParseFails(t, input, gigaparsec.Slice(s))
})) }))
t.Run("fails at end of input", rapid.MakeCheck(func(t *rapid.T) { t.Run("fails at end of input", rapid.MakeCheck(func(t *rapid.T) {