Test ReaderAtCursor with failing ReaderAt

This commit is contained in:
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)
}))
}