gigaparsec/test/readerat.go

53 lines
1.1 KiB
Go
Raw Normal View History

// SPDX-License-Identifier: Unlicense
2024-09-13 16:38:47 +00:00
// Package test contains helpers for testing parsers.
package test
2024-09-13 17:02:13 +00:00
import (
"io"
2024-09-24 17:53:32 +00:00
"git.codemonkeysoftware.net/b/gigaparsec"
2024-09-13 17:02:13 +00:00
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
2024-09-24 17:53:32 +00:00
"github.com/shoenig/test"
2024-09-13 17:02:13 +00:00
)
2024-09-13 16:38:47 +00:00
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}
}
2024-09-13 17:02:13 +00:00
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}
}
2024-09-24 17:53:32 +00:00
func StateIsAt[Input any](t test.T, s gigaparsec.State[Input], pos uint64) {
test.EqOp(t, pos, s.Pos(), test.Sprintf("expected parser state to be at position %d, got %d", pos, s.Pos()))
}