Test ReaderAtCursor with failing ReaderAt
This commit is contained in:
parent
4761259567
commit
c9ee9916eb
@ -6,6 +6,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
|
"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"
|
||||||
"github.com/shoenig/test/must"
|
"github.com/shoenig/test/must"
|
||||||
"pgregory.net/rapid"
|
"pgregory.net/rapid"
|
||||||
@ -15,6 +17,7 @@ func Todo(t *testing.T) {
|
|||||||
t.Errorf("TODO")
|
t.Errorf("TODO")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO move this to generator
|
||||||
func SliceOfNZero[T any](minLen, maxLen int) *rapid.Generator[[]T] {
|
func SliceOfNZero[T any](minLen, maxLen int) *rapid.Generator[[]T] {
|
||||||
return rapid.Map(rapid.IntRange(minLen, maxLen), func(n int) []T {
|
return rapid.Map(rapid.IntRange(minLen, maxLen), func(n int) []T {
|
||||||
return make([]T, n)
|
return make([]T, n)
|
||||||
@ -110,4 +113,14 @@ func TestReaderAtCursor(t *testing.T) {
|
|||||||
testCursor(t, func(b []byte) cursor.ReaderAtCursor {
|
testCursor(t, func(b []byte) cursor.ReaderAtCursor {
|
||||||
return cursor.NewReaderAt(bytes.NewReader(b))
|
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)
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
@ -2,12 +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"
|
ptest "git.codemonkeysoftware.net/b/gigaparsec/test"
|
||||||
|
"git.codemonkeysoftware.net/b/gigaparsec/test/generator"
|
||||||
"github.com/shoenig/test"
|
"github.com/shoenig/test"
|
||||||
"github.com/shoenig/test/must"
|
"github.com/shoenig/test/must"
|
||||||
"pgregory.net/rapid"
|
"pgregory.net/rapid"
|
||||||
@ -52,7 +52,7 @@ func TestSlice(t *testing.T) {
|
|||||||
assertParseFails(t, input, gigaparsec.Slice(s))
|
assertParseFails(t, input, gigaparsec.Slice(s))
|
||||||
}))
|
}))
|
||||||
t.Run("fails when read fails", rapid.MakeCheck(func(t *rapid.T) {
|
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)
|
c := ptest.ErrCursor[byte](expectedErr)
|
||||||
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
|
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
|
||||||
result, err := gigaparsec.Slice(s)(gigaparsec.MakeState(c))
|
result, err := gigaparsec.Slice(s)(gigaparsec.MakeState(c))
|
||||||
|
11
test/generator/gen.go
Normal file
11
test/generator/gen.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package generator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"pgregory.net/rapid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Error() *rapid.Generator[error] {
|
||||||
|
return rapid.Map(rapid.String(), errors.New)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user