57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package parse
|
|
|
|
// This file contains some tools that are used for writing tests.
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
func AssertEqual(t *testing.T, expected interface{}, actual interface{}, forWhat string) {
|
|
if expected != actual {
|
|
t.Errorf(
|
|
"Unexpected value for %s:\nexpected: %q\nactual: %q",
|
|
forWhat, expected, actual)
|
|
}
|
|
}
|
|
|
|
func AssertTrue(t *testing.T, b bool, assertion string) {
|
|
if !b {
|
|
t.Errorf("Assertion %s is false", assertion)
|
|
}
|
|
}
|
|
|
|
type PanicT struct {
|
|
Function func()
|
|
Regexp bool
|
|
Expect string
|
|
}
|
|
|
|
func AssertPanics(t *testing.T, testSet []PanicT) {
|
|
for _, test := range testSet {
|
|
AssertPanic(t, test)
|
|
}
|
|
}
|
|
|
|
func AssertPanic(t *testing.T, p PanicT) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
mismatch := false
|
|
if p.Regexp && !regexp.MustCompile(p.Expect).MatchString(r.(string)) {
|
|
mismatch = true
|
|
}
|
|
if !p.Regexp && p.Expect != r.(string) {
|
|
mismatch = true
|
|
}
|
|
if mismatch {
|
|
t.Errorf(
|
|
"Code did panic, but unexpected panic message received:\nexpected: %q\nactual: %q",
|
|
p.Expect, r)
|
|
}
|
|
} else {
|
|
t.Errorf("Function did not panic (expected panic message: %s)", p.Expect)
|
|
}
|
|
}()
|
|
p.Function()
|
|
}
|