Test ReaderAtCursor with failing ReaderAt

This commit is contained in:
Brandon Dyck 2024-09-13 11:17:32 -06:00
parent 4761259567
commit c9ee9916eb
3 changed files with 26 additions and 2 deletions

View File

@ -6,6 +6,8 @@ import (
"testing"
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
ptest "git.codemonkeysoftware.net/b/gigaparsec/test"
"git.codemonkeysoftware.net/b/gigaparsec/test/generator"
"github.com/shoenig/test"
"github.com/shoenig/test/must"
"pgregory.net/rapid"
@ -15,6 +17,7 @@ func Todo(t *testing.T) {
t.Errorf("TODO")
}
// TODO move this to generator
func SliceOfNZero[T any](minLen, maxLen int) *rapid.Generator[[]T] {
return rapid.Map(rapid.IntRange(minLen, maxLen), func(n int) []T {
return make([]T, n)
@ -110,4 +113,14 @@ func TestReaderAtCursor(t *testing.T) {
testCursor(t, func(b []byte) cursor.ReaderAtCursor {
return cursor.NewReaderAt(bytes.NewReader(b))
})
t.Run("Read returns an error if the ReaderAt fails", rapid.MakeCheck(func(t *rapid.T) {
expectedErr := generator.Error().Draw(t, "expectedErr")
startPos := rapid.Uint64().Draw(t, "startPos")
dst := SliceOfNZero[byte](0, 100).Draw(t, "dst")
c := cursor.NewReaderAt(ptest.ErrReaderAt(expectedErr)).At(startPos)
n, next, err := c.Read(dst)
test.ErrorIs(t, err, expectedErr)
test.EqOp(t, startPos, next.Pos())
test.Zero(t, n)
}))
}

View File

@ -2,12 +2,12 @@ package gigaparsec_test
import (
"bytes"
"errors"
"testing"
"git.codemonkeysoftware.net/b/gigaparsec"
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
ptest "git.codemonkeysoftware.net/b/gigaparsec/test"
"git.codemonkeysoftware.net/b/gigaparsec/test/generator"
"github.com/shoenig/test"
"github.com/shoenig/test/must"
"pgregory.net/rapid"
@ -52,7 +52,7 @@ func TestSlice(t *testing.T) {
assertParseFails(t, input, gigaparsec.Slice(s))
}))
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")
expectedErr := generator.Error().Draw(t, "expectedErr")
c := ptest.ErrCursor[byte](expectedErr)
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
result, err := gigaparsec.Slice(s)(gigaparsec.MakeState(c))

11
test/generator/gen.go Normal file
View File

@ -0,0 +1,11 @@
package generator
import (
"errors"
"pgregory.net/rapid"
)
func Error() *rapid.Generator[error] {
return rapid.Map(rapid.String(), errors.New)
}