Test Repeat success/failure

This commit is contained in:
Brandon Dyck 2024-09-27 10:40:18 -06:00
parent 2d6f091e0b
commit 5e6eafef64
2 changed files with 24 additions and 2 deletions

View File

@ -138,8 +138,22 @@ func TestEnd(t *testing.T) {
} }
func TestRepeat(t *testing.T) { func TestRepeat(t *testing.T) {
t.Run("fails when number of successes is less than minCount", Todo) t.Run("succeeds iff number of successes ≥ minCount", rapid.MakeCheck(func(t *rapid.T) {
t.Run("succeeds when number of successes is greater than minCount", Todo) 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("consumes iff at least one application consumes", Todo)
t.Run("fails on error", Todo) t.Run("fails on error", Todo)
t.Run("position is unchanged on failure", Todo) t.Run("position is unchanged on failure", Todo)

View File

@ -26,3 +26,11 @@ func ErrReaderAt(err error) io.ReaderAt {
func StateIsAt[Input any](t test.T, s gigaparsec.State[Input], pos uint64) { 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())) 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
}