39 lines
930 B
Go
39 lines
930 B
Go
package read_test
|
|
|
|
// 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 {
|
|
t.Errorf(
|
|
"Unexpected value at %s:\nexpected: %q\nactual: %q",
|
|
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()
|
|
}
|