From 47612595673de657bdba2f0c862a53ad13fdb539 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Fri, 13 Sep 2024 11:02:13 -0600 Subject: [PATCH] Refine Slice read error test --- parser_test.go | 3 +-- test/readerat.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/parser_test.go b/parser_test.go index 94be345..ad6af6d 100644 --- a/parser_test.go +++ b/parser_test.go @@ -53,8 +53,7 @@ func TestSlice(t *testing.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") - r := ptest.ErrReaderAt(expectedErr) - c := cursor.NewReaderAt(r) + c := ptest.ErrCursor[byte](expectedErr) s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s") result, err := gigaparsec.Slice(s)(gigaparsec.MakeState(c)) failed, _, _ := result.Failed() diff --git a/test/readerat.go b/test/readerat.go index c4a0029..d61be8d 100644 --- a/test/readerat.go +++ b/test/readerat.go @@ -1,7 +1,11 @@ // Package test contains helpers for testing parsers. package test -import "io" +import ( + "io" + + "git.codemonkeysoftware.net/b/gigaparsec/cursor" +) type errReaderAt struct { err error @@ -15,3 +19,26 @@ func (r errReaderAt) ReadAt([]byte, int64) (int, error) { func ErrReaderAt(err error) io.ReaderAt { return errReaderAt{err: err} } + +type errCursor[T any] struct { + err error + pos uint64 +} + +func (c errCursor[T]) Read([]T) (uint64, cursor.Cursor[T], error) { + return 0, c, c.err +} + +func (c errCursor[T]) At(pos uint64) cursor.Cursor[T] { + c.pos = pos + return c +} + +func (c errCursor[T]) Pos() uint64 { + return c.pos +} + +// ErrCursor return a [cursor.Cursor] with a Read method that always returns err. +func ErrCursor[T any](err error) cursor.Cursor[T] { + return errCursor[T]{err: err} +}