67 lines
1.6 KiB
Go
67 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 (
|
|
"regexp"
|
|
"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()
|
|
expected string
|
|
}
|
|
|
|
func RunPanicTest(t *testing.T, p PanicTest) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
if !regexp.MustCompile(p.expected).MatchString(r.(string)) {
|
|
t.Errorf("Function did panic, but unexpected panic message received:\nexpected: %q\nactual: %q\n", p.expected, r)
|
|
}
|
|
} else {
|
|
t.Errorf("Function did not panic (expected panic message: %s)", p.expected)
|
|
}
|
|
}()
|
|
p.function()
|
|
}
|
|
|
|
func RunPanicTests(t *testing.T, testSet []PanicTest) {
|
|
for _, test := range testSet {
|
|
RunPanicTest(t, test)
|
|
}
|
|
}
|