From 5e6eafef6450aedf6160f68e5539e5b77679c97e Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Fri, 27 Sep 2024 10:40:18 -0600 Subject: [PATCH] Test Repeat success/failure --- parser_test.go | 18 ++++++++++++++++-- test/{readerat.go => helpers.go} | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) rename test/{readerat.go => helpers.go} (85%) diff --git a/parser_test.go b/parser_test.go index 08251e6..6e44cac 100644 --- a/parser_test.go +++ b/parser_test.go @@ -138,8 +138,22 @@ func TestEnd(t *testing.T) { } func TestRepeat(t *testing.T) { - t.Run("fails when number of successes is less than minCount", Todo) - t.Run("succeeds when number of successes is greater than minCount", Todo) + t.Run("succeeds iff number of successes ≥ minCount", rapid.MakeCheck(func(t *rapid.T) { + const good byte = 'o' + const bad byte = 'x' + const maxParses = 100 + + minCount := rapid.IntRange(0, maxParses).Draw(t, "minCount") + successes := rapid.IntRange(0, maxParses).Draw(t, "successes") + + input := append(ptest.SliceOfN(good, successes), bad) + p := gigaparsec.Repeat(minCount, gigaparsec.Match(good)) + result, err := p(gigaparsec.MakeState(bytes.NewReader(input))) + + must.NoError(t, err) + success, _, _ := result.Status() + test.EqOp(t, successes >= minCount, success, test.Sprint("expected successes ≥ minCount")) + })) t.Run("consumes iff at least one application consumes", Todo) t.Run("fails on error", Todo) t.Run("position is unchanged on failure", Todo) diff --git a/test/readerat.go b/test/helpers.go similarity index 85% rename from test/readerat.go rename to test/helpers.go index 0c57402..a47f22f 100644 --- a/test/readerat.go +++ b/test/helpers.go @@ -26,3 +26,11 @@ func ErrReaderAt(err error) io.ReaderAt { func StateIsAt[Input any](t test.T, s gigaparsec.State[Input], pos uint64) { test.EqOp(t, pos, s.Pos(), test.Sprintf("expected parser state to be at position %d, got %d", pos, s.Pos())) } + +func SliceOfN[T any](value T, n int) []T { + s := make([]T, n) + for i := range s { + s[i] = value + } + return s +}