62 lines
1.7 KiB
Go
62 lines
1.7 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 assertCache(t *testing.T, name string, r *Buffer, code func(), storeLen, storeCap, bufLen, bufCap int) {
|
|
code()
|
|
if storeLen != len(r.store) {
|
|
t.Errorf("[%s] Unexpected store len (expected %d, got %d)", name, storeLen, len(r.store))
|
|
}
|
|
if storeCap != cap(r.store) {
|
|
t.Errorf("[%s] Unexpected store cap (expected %d, got %d)", name, storeCap, cap(r.store))
|
|
}
|
|
if bufLen != len(r.buffer) {
|
|
t.Errorf("[%s] Unexpected buffer len (expected %d, got %d)", name, bufLen, len(r.buffer))
|
|
}
|
|
if bufCap != cap(r.buffer) {
|
|
t.Errorf("[%s] Unexpected buffer cap (expected %d, got %d)", name, bufCap, cap(r.buffer))
|
|
}
|
|
}
|