Debian Codegraph @

assert/assert.go

raw
1 // Package assert provides C-like assertions for Go. For more
2 // information, see assert(3) (e.g. `man 3 assert`).
3 //
4 // The T variants operating on *testing.T values; instead of killing
5 // the program, they call the Fatal method.
6 //
7 // If GOTRACEBACK is set to enable coredumps, assertions will generate
8 // coredumps.
9 package assert
10
11 import (
12 "fmt"
13 "os"
14 "runtime"
15 "strings"
16 "testing"
17 )
18
19 // NoDebug, if set to true, will cause all asserts to be ignored.
20 var NoDebug bool
21
22 func die(what string, a ...string) {
23 _, file, line, ok := runtime.Caller(2)
24 if !ok {
25 panic(what)
26 }
27
28 if os.Getenv("GOTRACEBACK") == "crash" {
29 s := strings.Join(a, ", ")
30 if len(s) > 0 {
31 s = ": " + s
32 }
33 panic(what + s)
34 } else {
35 fmt.Fprintf(os.Stderr, "%s", what)
36 if len(a) > 0 {
37 s := strings.Join(a, ", ")
38 fmt.Fprintln(os.Stderr, ": "+s)
39 } else {
40 fmt.Fprintf(os.Stderr, "\n")
41 }
42
43 fmt.Fprintf(os.Stderr, "\t%s line %d\n", file, line)
44
45 os.Exit(1)
46 }
47 }
48
49 // Bool asserts that cond is false.
50 //
51 // For example, this would replace
52 // if x < 0 {
53 // log.Fatal("x is subzero")
54 // }
55 //
56 // The same assertion would be
57 // assert.Bool(x, "x is subzero")
58 func Bool(cond bool, s ...string) {
59 if NoDebug {
60 return
61 }
62
63 if !cond {
64 die("assert.Bool failed", s...)
65 }
66 }
67
68 // Error asserts that err is not nil, e.g. that an error has occurred.
69 //
70 // For example,
71 // if err == nil {
72 // log.Fatal("call to <something> should have failed")
73 // }
74 // // becomes
75 // assert.Error(err, "call to <something> should have failed")
76 func Error(err error, s ...string) {
77 if NoDebug {
78 return
79 } else if nil != err {
80 return
81 }
82
83 if len(s) == 0 {
84 die("error expected, but no error returned")
85 } else {
86 die(strings.Join(s, ", "))
87 }
88 }
89
90 // NoError asserts that err is nil, e.g. that no error has occurred.
91 func NoError(err error, s ...string) {
92 if NoDebug {
93 return
94 }
95
96 if nil != err {
97 die(err.Error())
98 }
99 }
100
101 // ErrorEq asserts that the actual error is the expected error.
102 func ErrorEq(expected, actual error) {
103 if NoDebug || (expected == actual) {
104 return
105 }
106
107 if expected == nil {
108 die(fmt.Sprintf("assert.ErrorEq: %s", actual.Error()))
109 }
110
111 var should string
112 if actual == nil {
113 should = "no error was returned"
114 } else {
115 should = fmt.Sprintf("have '%s'", actual)
116 }
117
118 die(fmt.Sprintf("assert.ErrorEq: expected '%s', but %s", expected, should))
119 }
120
121 // BoolT checks a boolean condition, calling Fatal on t if it is
122 // false.
123 func BoolT(t *testing.T, cond bool, s ...string) {
124 if !cond {
125 what := strings.Join(s, ", ")
126 if len(what) > 0 {
127 what = ": " + what
128 }
129 t.Fatalf("assert.Bool failed%s", what)
130 }
131 }
132
133 // ErrorT asserts that err is not nil, e.g. asserting that an error
134 // has occurred. See also NoErrorT.
135 func ErrorT(t *testing.T, err error, s ...string) {
136 if nil != err {
137 return
138 }
139
140 if len(s) == 0 {
141 t.Fatal("error expected, but no error returned")
142 } else {
143 t.Fatal(strings.Join(s, ", "))
144 }
145 }
146
147 // NoErrorT asserts that err is nil, e.g. asserting that no error has
148 // occurred. See also ErrorT.
149 func NoErrorT(t *testing.T, err error) {
150 if nil != err {
151 t.Fatalf("%s", err)
152 }
153 }
154
155 // ErrorEqT compares a pair of errors, calling Fatal on it if they
156 // don't match.
157 func ErrorEqT(t *testing.T, expected, actual error) {
158 if NoDebug || (expected == actual) {
159 return
160 }
161
162 if expected == nil {
163 die(fmt.Sprintf("assert.Error2: %s", actual.Error()))
164 }
165
166 var should string
167 if actual == nil {
168 should = "no error was returned"
169 } else {
170 should = fmt.Sprintf("have '%s'", actual)
171 }
172
173 die(fmt.Sprintf("assert.Error2: expected '%s', but %s", expected, should))
174 }