go-parsekit/parsekit_test.go

66 lines
1.6 KiB
Go

package parsekit_test
// This file only provides building blocks for writing tests.
// No actual tests belong in this file.
import (
"testing"
"git.makaay.nl/mauricem/go-parsekit"
)
// Easy access to the parsekit definitions.
var c, a, m = parsekit.C, parsekit.A, parsekit.M
type TokenHandlerTest struct {
Input string
TokenHandler parsekit.TokenHandler
MustMatch bool
Expected string
}
func RunTokenHandlerTests(t *testing.T, testSet []TokenHandlerTest) {
for _, test := range testSet {
RunTokenHandlerTest(t, test)
}
}
func RunTokenHandlerTest(t *testing.T, test TokenHandlerTest) {
output, err := parsekit.NewMatcher(test.TokenHandler, "a match").Execute(test.Input)
if test.MustMatch {
if err != nil {
t.Errorf("Test %q failed with error: %s", test.Input, err)
} else if output != test.Expected {
t.Errorf("Test %q failed: not expected output:\nexpected: %q\nactual: %q\n", test.Input, test.Expected, output)
}
} else {
if err == nil {
t.Errorf("Test %q failed: should not match, but it did", test.Input)
}
}
}
type PanicTest struct {
function func()
epxected string
}
func RunPanicTest(t *testing.T, p PanicTest) {
defer func() {
if r := recover(); r != nil {
if r != p.epxected {
t.Errorf("Function did panic, but unexpected panic message received:\nexpected: %q\nactual: %q\n", p.epxected, r)
}
} else {
t.Errorf("Function did not panic (expected panic message: %s)", p.epxected)
}
}()
p.function()
}
func RunPanicTests(t *testing.T, testSet []PanicTest) {
for _, test := range testSet {
RunPanicTest(t, test)
}
}