1 // Copyright 2017 Frank Schroeder. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package assert provides helper functions for testing.
18 // skip defines the default call depth
21 // Equal asserts that got and want are equal as defined by
22 // reflect.DeepEqual. The test fails with msg if they are not equal.
23 func Equal(t *testing.T, got, want interface{}, msg ...string) {
24 if x := equal(2, got, want, msg...); x != "" {
30 func equal(skip int, got, want interface{}, msg ...string) string {
31 if !reflect.DeepEqual(got, want) {
32 return fail(skip, "got %v want %v %s", got, want, strings.Join(msg, " "))
37 // Panic asserts that function fn() panics.
38 // It assumes that recover() either returns a string or
39 // an error and fails if the message does not match
40 // the regular expression in 'matches'.
41 func Panic(t *testing.T, fn func(), matches string) {
42 if x := doesPanic(2, fn, matches); x != "" {
48 func doesPanic(skip int, fn func(), expr string) (err string) {
52 err = fail(skip, "did not panic")
62 err = matches(skip, v, expr)
68 // Matches asserts that a value matches a given regular expression.
69 func Matches(t *testing.T, value, expr string) {
70 if x := matches(2, value, expr); x != "" {
76 func matches(skip int, value, expr string) string {
77 ok, err := regexp.MatchString(expr, value)
79 return fail(skip, "invalid pattern %q. %s", expr, err)
82 return fail(skip, "got %s which does not match %s", value, expr)
87 func fail(skip int, format string, args ...interface{}) string {
88 _, file, line, _ := runtime.Caller(skip)
89 return fmt.Sprintf("\t%s:%d: %s\n", filepath.Base(file), line, fmt.Sprintf(format, args...))