From 2759894ed838b7a30749827b63aa06c0ff98c391 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 9 Sep 2024 15:13:37 -0600 Subject: [PATCH] Factored out Slice failure assertions --- parser_test.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/parser_test.go b/parser_test.go index babbe67..986279e 100644 --- a/parser_test.go +++ b/parser_test.go @@ -23,27 +23,30 @@ func hasPrefix(prefix []byte) func([]byte) bool { } func TestSlice(t *testing.T) { + 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 + } + 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") - start := gigaparsec.MakeState(cursor.NewSlice(input)) - - consumed, result, err := gigaparsec.Slice(s)(start) - test.ErrorAs(t, err, &gigaparsec.ParseError{}) - test.False(t, consumed, test.Sprint("expected consumed to be false")) - test.SliceEmpty(t, result.Value) + assertParseFails(t, input, gigaparsec.Slice(s)) })) 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] - start := gigaparsec.MakeState(cursor.NewSlice(input)) - - consumed, result, err := gigaparsec.Slice(s)(start) - test.ErrorAs(t, err, &gigaparsec.ParseError{}) - test.False(t, consumed, test.Sprint("expected consumed to be false")) - test.SliceEmpty(t, result.Value) + assertParseFails(t, input, gigaparsec.Slice(s)) })) t.Run("fails when read fails", Todo) t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) {