Test Slice with a failing reader

This commit is contained in:
Brandon Dyck 2024-09-13 10:38:47 -06:00
parent b040fd21c3
commit 0a149acf46
3 changed files with 29 additions and 1 deletions

View File

@ -1,2 +1,4 @@
Result should be Failed by default
Test RuneReader Test RuneReader
Test Regexp Test Regexp
Add SPDX tags

View File

@ -2,10 +2,12 @@ package gigaparsec_test
import ( import (
"bytes" "bytes"
"errors"
"testing" "testing"
"git.codemonkeysoftware.net/b/gigaparsec" "git.codemonkeysoftware.net/b/gigaparsec"
"git.codemonkeysoftware.net/b/gigaparsec/cursor" "git.codemonkeysoftware.net/b/gigaparsec/cursor"
ptest "git.codemonkeysoftware.net/b/gigaparsec/test"
"github.com/shoenig/test" "github.com/shoenig/test"
"github.com/shoenig/test/must" "github.com/shoenig/test/must"
"pgregory.net/rapid" "pgregory.net/rapid"
@ -49,7 +51,14 @@ func TestSlice(t *testing.T) {
input := s[:inputLen] input := s[:inputLen]
assertParseFails(t, input, gigaparsec.Slice(s)) assertParseFails(t, input, gigaparsec.Slice(s))
})) }))
t.Run("fails when read fails", Todo) t.Run("fails when read fails", rapid.MakeCheck(func(t *rapid.T) {
expectedErr := rapid.Map(rapid.StringN(0, 100, -1), errors.New).Draw(t, "err")
r := ptest.ErrReaderAt(expectedErr)
c := cursor.NewReaderAt(r)
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
_, err := gigaparsec.Slice(s)(gigaparsec.MakeState(c))
test.ErrorIs(t, err, expectedErr)
}))
t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) { t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) {
input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input") input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input")
sLen := rapid.IntRange(0, len(input)).Draw(t, "sLen") sLen := rapid.IntRange(0, len(input)).Draw(t, "sLen")

17
test/readerat.go Normal file
View File

@ -0,0 +1,17 @@
// Package test contains helpers for testing parsers.
package test
import "io"
type errReaderAt struct {
err error
}
func (r errReaderAt) ReadAt([]byte, int64) (int, error) {
return 0, r.err
}
// ErrReaderAt returns an [io.ReaderAt] with a ReadAt method that always returns err.
func ErrReaderAt(err error) io.ReaderAt {
return errReaderAt{err: err}
}