go-parsekit/read/assertions_test.go

59 lines
1.5 KiB
Go

package read
// This file contains some tools that are used for writing tests.
import (
"fmt"
"runtime"
"testing"
)
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if expected != actual {
switch expected.(type) {
case rune:
t.Errorf(
"Unexpected value at %s:\nexpected: %q\nactual: %q",
callerFilepos(1), expected, actual)
default:
t.Errorf(
"Unexpected value at %s:\nexpected: %v\nactual: %v",
callerFilepos(1), expected, actual)
}
}
}
func callerFilepos(depth int) string {
// No error handling, because we call this method ourselves with safe depth values.
_, file, line, _ := runtime.Caller(depth + 1)
return fmt.Sprintf("%s:%d", file, line)
}
func assertPanic(t *testing.T, code func(), expected string) {
defer func() {
if r := recover(); r != nil {
if expected != r.(string) {
t.Errorf(
"Code did panic, but unexpected panic message received:\nexpected: %q\nactual: %q",
expected, r)
}
} else {
t.Errorf("Function did not panic (expected panic message: %s)", expected)
}
}()
code()
}
func assertBuffer(t *testing.T, name string, r *Buffer, code func(), capacity, start, length int) {
code()
if capacity != r.cap {
t.Errorf("[%s] Unexpected buffer cap (expected %d, got %d)", name, capacity, r.cap)
}
if start != r.start {
t.Errorf("[%s] Unexpected data starting point (expected %d, got %d)", name, start, r.start)
}
if length != r.len {
t.Errorf("[%s] Unexpected buffer len (expected %d, got %d)", name, length, r.len)
}
}