learnlytics-go/templ/parser/v2/allocs_test.go

27 lines
554 B
Go
Raw Normal View History

2025-03-20 12:35:13 +01:00
package parser
import (
"testing"
"github.com/a-h/parse"
)
func RunParserAllocTest[T any](t *testing.T, p parse.Parser[T], expectOK bool, maxAllocs int64, input string) {
pi := parse.NewInput(input)
actual := testing.AllocsPerRun(4, func() {
pi.Seek(0)
_, ok, err := p.Parse(pi)
if err != nil {
t.Fatalf("error parsing %T: %v", p, err)
}
if ok != expectOK {
t.Fatalf("failed to parse %T", p)
}
})
// Run the benchmark.
if int64(actual) > maxAllocs {
t.Fatalf("Expected allocs <= %d, got %d", maxAllocs, int64(actual))
}
}