From c22246b7dedb702ececd03b2ed0b9bb187824ed1 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 16 Oct 2024 13:32:25 -0600 Subject: [PATCH] Added some Regexp tests --- bytes/regexp_test.go | 46 +++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/bytes/regexp_test.go b/bytes/regexp_test.go index 11f3953..6c77b92 100644 --- a/bytes/regexp_test.go +++ b/bytes/regexp_test.go @@ -4,6 +4,7 @@ package bytes_test import ( "bytes" + "errors" "strings" "testing" @@ -21,20 +22,43 @@ func Todo(t *testing.T) { } func TestRegexp(t *testing.T) { - t.Run("only searches the beginning of input", Todo) - t.Run("position is correct after match", Todo) - t.Run("fails on unexpected error", Todo) - t.Run("returns a useful Got value", rapid.MakeCheck(func(t *rapid.T) { + alpha := rapid.SliceOfBytesMatching(`[A-Za-z]{1,100}`) + t.Run("position and value are correct after match", rapid.MakeCheck(func(t *rapid.T) { + needle := alpha.Draw(t, "needle") + input := rapid.Map(alpha, func(suffix []byte) []byte { return append(needle, suffix...) }). + Draw(t, "input") + + p := pbytes.Regexp(string(needle)) + result, err := p(gigaparsec.MakeState(bytes.NewReader(input))) + succeeded, val, next := result.Status() - })) - t.Run("basically works", func(t *testing.T) { - result, err := pbytes.Regexp("a")(gigaparsec.MakeState(strings.NewReader("a"))) must.NoError(t, err) - success, value, _ := result.Status() - test.True(t, success, test.Sprint(result.Message())) - test.EqOp(t, "a", value) - test.True(t, result.Consumed()) + test.True(t, succeeded) + test.EqOp(t, string(needle), val) + ptest.StateIsAt(t, next, uint64(len(needle))) + })) + t.Run("only searches the beginning of input", rapid.MakeCheck(func(t *rapid.T) { + needle := alpha.Draw(t, "needle") + input := rapid.Map(alpha, func(prefix []byte) []byte { return append(prefix, needle...) }). + Filter(func(b []byte) bool { return !bytes.HasPrefix(b, needle) }). + Draw(t, "input") + + p := pbytes.Regexp(string(needle)) + result, err := p(gigaparsec.MakeState(bytes.NewReader(input))) + succeeded, _, _ := result.Status() + + must.NoError(t, err) + test.False(t, succeeded) + })) + t.Run("fails on unexpected error", func(t *testing.T) { + expectedErr := errors.New("it broke") + p := pbytes.Regexp("nope") + result, err := p(gigaparsec.MakeState(ptest.ErrReaderAt(expectedErr))) + succeeded, _, _ := result.Status() + test.ErrorIs(t, err, expectedErr) + test.False(t, succeeded) }) + t.Run("returns a useful Got value", Todo) } func TestRuneReader(t *testing.T) {