diff --git a/cursor/cursor_test.go b/cursor/cursor_test.go index 3fa3e8e..6072438 100644 --- a/cursor/cursor_test.go +++ b/cursor/cursor_test.go @@ -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) + })) } diff --git a/parser_test.go b/parser_test.go index ad6af6d..1096b8c 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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)) diff --git a/test/generator/gen.go b/test/generator/gen.go new file mode 100644 index 0000000..6a63231 --- /dev/null +++ b/test/generator/gen.go @@ -0,0 +1,11 @@ +package generator + +import ( + "errors" + + "pgregory.net/rapid" +) + +func Error() *rapid.Generator[error] { + return rapid.Map(rapid.String(), errors.New) +}