Test Repeat success and next state

This commit is contained in:
Brandon Dyck 2024-10-16 09:06:25 -06:00
parent 59903ba151
commit bfc9a9ae58
2 changed files with 19 additions and 17 deletions

View File

@ -379,25 +379,26 @@ func Pipe[In, Ignore, Through any](p Parser[In, Ignore]) func(Through) Parser[In
// It succeeds if and only if p succeeds at least minCount times. // It succeeds if and only if p succeeds at least minCount times.
// It consumes if and only if at least one of the applications of p consumes. // It consumes if and only if at least one of the applications of p consumes.
func Repeat[In, Out any](minCount int, p Parser[In, Out]) Parser[In, []Out] { func Repeat[In, Out any](minCount int, p Parser[In, Out]) Parser[In, []Out] {
return func(s State[In]) (Result[In, []Out], error) { return func(state State[In]) (Result[In, []Out], error) {
var values []Out var values []Out
var consumed bool var consumed bool
next := s currState := state
for { for {
result, err := p(next) result, err := p(currState)
if err != nil { if err != nil {
return Result[In, []Out]{}, fmt.Errorf("AtLeastN: %w", err) return Result[In, []Out]{}, fmt.Errorf("AtLeastN: %w", err)
} }
consumed = consumed || result.Consumed() consumed = consumed || result.Consumed()
var value Out var value Out
var success bool var success bool
success, value, next = result.Status() success, value, nextState := result.Status()
if !success { if !success {
if len(values) >= minCount { if len(values) >= minCount {
return Succeed(consumed, values, next, MessageOK(s.Pos())), nil return Succeed(consumed, values, currState, MessageOK(state.Pos())), nil
} }
return Fail[In, []Out](consumed, result.Message()), nil return Fail[In, []Out](consumed, result.Message()), nil
} }
currState = nextState
values = append(values, value) values = append(values, value)
} }
} }

View File

@ -138,24 +138,25 @@ func TestEnd(t *testing.T) {
} }
func TestRepeat(t *testing.T) { func TestRepeat(t *testing.T) {
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 const maxParses = 100
t.Run("succeeds iff number of successes ≥ minCount", rapid.MakeCheck(func(t *rapid.T) {
minCount := rapid.IntRange(0, maxParses).Draw(t, "minCount") minCount := rapid.IntRange(0, maxParses).Draw(t, "minCount")
successes := rapid.IntRange(0, maxParses).Draw(t, "successes") successes := rapid.IntRange(0, maxParses).Draw(t, "successes")
shouldSucceed := successes >= minCount
input := append(ptest.SliceOfN(good, successes), bad) input := append(ptest.SliceOfN(true, successes), false)
p := gigaparsec.Repeat(minCount, gigaparsec.Match(good)) p := gigaparsec.Repeat(minCount, gigaparsec.Match(true))
result, err := p(gigaparsec.MakeState(bytes.NewReader(input))) result, err := p(gigaparsec.MakeState(gigaparsec.SliceReaderAt[bool](input)))
must.NoError(t, err) must.NoError(t, err)
success, _, _ := result.Status() success, _, next := result.Status()
test.EqOp(t, successes >= minCount, success, test.Sprint("expected successes ≥ minCount")) test.EqOp(t, shouldSucceed, success)
})) if success {
t.Run("consumes iff at least one application consumes", Todo) test.EqOp(t, uint64(successes), next.Pos())
t.Run("fails on error", Todo) }
t.Run("position is unchanged on failure", Todo) }))
t.Run("position follows last success on overall success", Todo) t.Run("consumes iff at least one application consumes", rapid.MakeCheck(func(t *rapid.T) {
}))
t.Run("fails on error", Todo)
} }