2016-04-28 18:33:21 +00:00
|
|
|
// Package assert provides C-like assertions for Go. For more
|
|
|
|
// information, see assert(3) (e.g. `man 3 assert`).
|
2016-04-28 19:23:50 +00:00
|
|
|
//
|
|
|
|
// The T variants operating on *testing.T values; instead of killing
|
|
|
|
// the program, they call the Fatal method.
|
2016-04-28 21:14:48 +00:00
|
|
|
//
|
|
|
|
// If GOTRACEBACK is set to enable coredumps, assertions will generate
|
|
|
|
// coredumps.
|
2016-04-28 18:33:21 +00:00
|
|
|
package assert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2016-04-28 19:23:50 +00:00
|
|
|
"strings"
|
2016-04-28 18:33:21 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NoDebug, if set to true, will cause all asserts to be ignored.
|
|
|
|
var NoDebug bool
|
|
|
|
|
2016-04-28 19:23:50 +00:00
|
|
|
func die(what string, a ...string) {
|
2016-04-28 18:33:21 +00:00
|
|
|
_, file, line, ok := runtime.Caller(2)
|
|
|
|
if !ok {
|
|
|
|
panic(what)
|
2016-04-28 21:14:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("GOTRACEBACK") == "crash" {
|
|
|
|
s := strings.Join(a, ", ")
|
|
|
|
if len(s) > 0 {
|
|
|
|
s = ": " + s
|
|
|
|
}
|
|
|
|
panic(what + s)
|
2016-04-28 18:33:21 +00:00
|
|
|
} else {
|
2016-04-28 19:23:50 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%s", what)
|
|
|
|
if len(a) > 0 {
|
|
|
|
s := strings.Join(a, ", ")
|
2016-04-28 21:14:48 +00:00
|
|
|
fmt.Fprintln(os.Stderr, ": "+s)
|
2016-04-28 19:23:50 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "\n")
|
|
|
|
}
|
2016-04-28 21:14:48 +00:00
|
|
|
|
2016-04-28 18:33:21 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "\t%s line %d\n", file, line)
|
2016-04-28 21:14:48 +00:00
|
|
|
|
2016-04-28 18:33:21 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bool asserts that cond is false.
|
2016-04-28 19:23:50 +00:00
|
|
|
//
|
|
|
|
// For example, this would replace
|
|
|
|
// if x < 0 {
|
|
|
|
// log.Fatal("x is subzero")
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The same assertion would be
|
|
|
|
// assert.Bool(x, "x is subzero")
|
|
|
|
func Bool(cond bool, s ...string) {
|
2016-04-28 18:33:21 +00:00
|
|
|
if NoDebug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cond {
|
2016-04-28 19:23:50 +00:00
|
|
|
die("assert.Bool failed", s...)
|
2016-04-28 18:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:23:50 +00:00
|
|
|
// Error asserts that err is not nil, e.g. that an error has occurred.
|
|
|
|
//
|
|
|
|
// For example,
|
|
|
|
// if err == nil {
|
|
|
|
// log.Fatal("call to <something> should have failed")
|
|
|
|
// }
|
|
|
|
// // becomes
|
|
|
|
// assert.Error(err, "call to <something> should have failed")
|
|
|
|
func Error(err error, s ...string) {
|
|
|
|
if NoDebug {
|
|
|
|
return
|
|
|
|
} else if nil != err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s) == 0 {
|
|
|
|
die("error expected, but no error returned")
|
|
|
|
} else {
|
|
|
|
die(strings.Join(s, ", "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoError asserts that err is nil, e.g. that no error has occurred.
|
|
|
|
func NoError(err error, s ...string) {
|
2016-04-28 18:33:21 +00:00
|
|
|
if NoDebug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
die(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:23:50 +00:00
|
|
|
// ErrorEq asserts that the actual error is the expected error.
|
|
|
|
func ErrorEq(expected, actual error) {
|
2016-04-28 18:33:21 +00:00
|
|
|
if NoDebug || (expected == actual) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected == nil {
|
2016-04-28 19:23:50 +00:00
|
|
|
die(fmt.Sprintf("assert.ErrorEq: %s", actual.Error()))
|
2016-04-28 18:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var should string
|
|
|
|
if actual == nil {
|
|
|
|
should = "no error was returned"
|
|
|
|
} else {
|
|
|
|
should = fmt.Sprintf("have '%s'", actual)
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:23:50 +00:00
|
|
|
die(fmt.Sprintf("assert.ErrorEq: expected '%s', but %s", expected, should))
|
2016-04-28 18:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BoolT checks a boolean condition, calling Fatal on t if it is
|
|
|
|
// false.
|
2016-04-28 19:23:50 +00:00
|
|
|
func BoolT(t *testing.T, cond bool, s ...string) {
|
2016-04-28 18:33:21 +00:00
|
|
|
if !cond {
|
2016-04-28 19:23:50 +00:00
|
|
|
what := strings.Join(s, ", ")
|
|
|
|
if len(what) > 0 {
|
|
|
|
what = ": " + what
|
|
|
|
}
|
|
|
|
t.Fatalf("assert.Bool failed%s", what)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorT asserts that err is not nil, e.g. asserting that an error
|
|
|
|
// has occurred. See also NoErrorT.
|
|
|
|
func ErrorT(t *testing.T, err error, s ...string) {
|
|
|
|
if nil != err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s) == 0 {
|
|
|
|
t.Fatal("error expected, but no error returned")
|
|
|
|
} else {
|
|
|
|
t.Fatal(strings.Join(s, ", "))
|
2016-04-28 18:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:23:50 +00:00
|
|
|
// NoErrorT asserts that err is nil, e.g. asserting that no error has
|
|
|
|
// occurred. See also ErrorT.
|
|
|
|
func NoErrorT(t *testing.T, err error) {
|
2016-04-28 18:33:21 +00:00
|
|
|
if nil != err {
|
|
|
|
t.Fatalf("%s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:23:50 +00:00
|
|
|
// ErrorEqT compares a pair of errors, calling Fatal on it if they
|
2016-04-28 18:33:21 +00:00
|
|
|
// don't match.
|
2016-04-28 19:23:50 +00:00
|
|
|
func ErrorEqT(t *testing.T, expected, actual error) {
|
2016-04-28 18:33:21 +00:00
|
|
|
if NoDebug || (expected == actual) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected == nil {
|
|
|
|
die(fmt.Sprintf("assert.Error2: %s", actual.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
var should string
|
|
|
|
if actual == nil {
|
|
|
|
should = "no error was returned"
|
|
|
|
} else {
|
|
|
|
should = fmt.Sprintf("have '%s'", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
die(fmt.Sprintf("assert.Error2: expected '%s', but %s", expected, should))
|
|
|
|
}
|