gigaparsec/test/helpers.go

37 lines
777 B
Go

// SPDX-License-Identifier: Unlicense
// Package test contains helpers for testing parsers.
package test
import (
"io"
"git.codemonkeysoftware.net/b/gigaparsec"
"github.com/shoenig/test"
)
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}
}
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()))
}
func SliceOfN[T any](value T, n int) []T {
s := make([]T, n)
for i := range s {
s[i] = value
}
return s
}