goutils/assert/assert.go

175 lines
3.5 KiB
Go
Raw Normal View History

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`).
//
// 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"
"strings"
2016-04-28 18:33:21 +00:00
"testing"
)
2017-11-16 16:32:05 +00:00
// NoDebug can be set to true to cause all asserts to be ignored.
2016-04-28 18:33:21 +00:00
var NoDebug bool
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 {
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)
} 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.
//
// 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 {
die("assert.Bool failed", s...)
2016-04-28 18:33:21 +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 {
2023-05-05 06:21:07 +00:00
die(err.Error(), s...)
2016-04-28 18:33:21 +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 {
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)
}
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.
func BoolT(t *testing.T, cond bool, s ...string) {
2016-04-28 18:33:21 +00:00
if !cond {
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
}
}
// 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)
}
}
// ErrorEqT compares a pair of errors, calling Fatal on it if they
2016-04-28 18:33:21 +00:00
// don't match.
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)
}
2023-05-05 06:21:07 +00:00
t.Fatalf("assert.Error2: expected '%s', but %s", expected, should)
2016-04-28 18:33:21 +00:00
}