2016-06-16 17:55:50 -04:00
|
|
|
// Package assert contains functions for making assertions in unit tests
|
|
|
|
package assert
|
|
|
|
|
|
|
|
import (
|
2016-07-05 14:43:28 -04:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2016-09-13 03:01:31 -04:00
|
|
|
"reflect"
|
2016-07-05 14:43:28 -04:00
|
|
|
"runtime"
|
2016-06-16 17:55:50 -04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestingT is an interface which defines the methods of testing.T that are
|
|
|
|
// required by this package
|
|
|
|
type TestingT interface {
|
|
|
|
Fatalf(string, ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal compare the actual value to the expected value and fails the test if
|
|
|
|
// they are not equal.
|
|
|
|
func Equal(t TestingT, actual, expected interface{}) {
|
|
|
|
if expected != actual {
|
2016-06-22 16:51:12 -04:00
|
|
|
fatal(t, "Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual)
|
2016-06-16 17:55:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 12:38:23 -04:00
|
|
|
//EqualStringSlice compares two slices and fails the test if they do not contain
|
|
|
|
// the same items.
|
|
|
|
func EqualStringSlice(t TestingT, actual, expected []string) {
|
|
|
|
if len(actual) != len(expected) {
|
2016-06-22 16:51:12 -04:00
|
|
|
fatal(t, "Expected (length %d): %q\nActual (length %d): %q",
|
2016-06-29 12:38:23 -04:00
|
|
|
len(expected), expected, len(actual), actual)
|
|
|
|
}
|
|
|
|
for i, item := range actual {
|
|
|
|
if item != expected[i] {
|
2016-06-22 16:51:12 -04:00
|
|
|
fatal(t, "Slices differ at element %d, expected %q got %q",
|
2016-06-29 12:38:23 -04:00
|
|
|
i, expected[i], item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 17:55:50 -04:00
|
|
|
// NilError asserts that the error is nil, otherwise it fails the test.
|
|
|
|
func NilError(t TestingT, err error) {
|
|
|
|
if err != nil {
|
2016-06-22 16:51:12 -04:00
|
|
|
fatal(t, "Expected no error, got: %s", err.Error())
|
2016-06-16 17:55:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 03:01:31 -04:00
|
|
|
// DeepEqual compare the actual value to the expected value and fails the test if
|
|
|
|
// they are not "deeply equal".
|
|
|
|
func DeepEqual(t TestingT, actual, expected interface{}) {
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
fatal(t, "Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 17:55:50 -04:00
|
|
|
// Error asserts that error is not nil, and contains the expected text,
|
|
|
|
// otherwise it fails the test.
|
|
|
|
func Error(t TestingT, err error, contains string) {
|
|
|
|
if err == nil {
|
2016-07-05 14:43:28 -04:00
|
|
|
fatal(t, "Expected an error, but error was nil")
|
2016-06-16 17:55:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), contains) {
|
2016-06-22 16:51:12 -04:00
|
|
|
fatal(t, "Expected error to contain '%s', got '%s'", contains, err.Error())
|
2016-06-16 17:55:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains asserts that the string contains a substring, otherwise it fails the
|
|
|
|
// test.
|
|
|
|
func Contains(t TestingT, actual, contains string) {
|
|
|
|
if !strings.Contains(actual, contains) {
|
2016-06-22 16:51:12 -04:00
|
|
|
fatal(t, "Expected '%s' to contain '%s'", actual, contains)
|
2016-06-16 17:55:50 -04:00
|
|
|
}
|
|
|
|
}
|
2016-07-05 14:43:28 -04:00
|
|
|
|
2016-06-22 16:51:12 -04:00
|
|
|
// NotNil fails the test if the object is nil
|
|
|
|
func NotNil(t TestingT, obj interface{}) {
|
|
|
|
if obj == nil {
|
|
|
|
fatal(t, "Expected non-nil value.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fatal(t TestingT, format string, args ...interface{}) {
|
|
|
|
t.Fatalf(errorSource()+format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See testing.decorate()
|
|
|
|
func errorSource() string {
|
|
|
|
_, filename, line, ok := runtime.Caller(3)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%d: ", filepath.Base(filename), line)
|
2016-07-05 14:43:28 -04:00
|
|
|
}
|