2024-09-18 22:28:53 +00:00
|
|
|
// 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 (
|
2024-10-17 14:57:45 +00:00
|
|
|
"errors"
|
2024-09-13 17:02:13 +00:00
|
|
|
"io"
|
|
|
|
|
2024-09-24 17:53:32 +00:00
|
|
|
"git.codemonkeysoftware.net/b/gigaparsec"
|
|
|
|
"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
|
|
|
|
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()))
|
|
|
|
}
|
2024-09-27 16:40:18 +00:00
|
|
|
|
|
|
|
func SliceOfN[T any](value T, n int) []T {
|
|
|
|
s := make([]T, n)
|
|
|
|
for i := range s {
|
|
|
|
s[i] = value
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2024-10-17 14:57:45 +00:00
|
|
|
|
|
|
|
type ForcedResult struct{ Succeed, Consume bool }
|
|
|
|
|
|
|
|
func ForceResult(state gigaparsec.State[ForcedResult]) (gigaparsec.Result[ForcedResult, struct{}], error) {
|
|
|
|
buf := make([]ForcedResult, 1)
|
|
|
|
_, next, err := state.Read(buf)
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return gigaparsec.Fail[ForcedResult, struct{}](false, gigaparsec.MessageEnd(state.Pos())), nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return gigaparsec.Result[ForcedResult, struct{}]{}, err
|
|
|
|
}
|
|
|
|
tok := buf[0]
|
|
|
|
if tok.Succeed {
|
|
|
|
return gigaparsec.Succeed(tok.Consume, struct{}{}, next, gigaparsec.MessageOK(state.Pos())), nil
|
|
|
|
} else {
|
|
|
|
return gigaparsec.Fail[ForcedResult, struct{}](tok.Consume, gigaparsec.MakeMessage(state.Pos(), "Succeed=false", "Succeed=true")), nil
|
|
|
|
}
|
|
|
|
}
|