47 lines
906 B
Go
47 lines
906 B
Go
// SPDX-License-Identifier: Unlicense
|
|
|
|
// Package test contains helpers for testing parsers.
|
|
package test
|
|
|
|
import (
|
|
"io"
|
|
|
|
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
|
|
)
|
|
|
|
type errReaderAt struct {
|
|
err error
|
|
}
|
|
|
|
func (r errReaderAt) ReadAt([]byte, int64) (int, error) {
|
|
return 0, r.err
|
|
}
|
|
|
|
// ErrReaderAt returns an [io.ReaderAt] with a ReadAt method that always returns err.
|
|
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}
|
|
}
|