Debian Codegraph @

helpers.go

raw symbols SCIP
1 package check
2
3 import (
4 "fmt"
5 "strings"
6 "time"
7 )
8
9 // TestName returns the current test name in the form "SuiteName.TestName"
10 func (c *C) TestName() string {
11 return c.testName
12 }
13
14 // -----------------------------------------------------------------------
15 // Basic succeeding/failing logic.
16
17 // Failed returns whether the currently running test has already failed.
18 func (c *C) Failed() bool {
19 return c.status() == failedSt
20 }
21
22 // Fail marks the currently running test as failed.
23 //
24 // Something ought to have been previously logged so the developer can tell
25 // what went wrong. The higher level helper functions will fail the test
26 // and do the logging properly.
27 func (c *C) Fail() {
28 c.setStatus(failedSt)
29 }
30
31 // FailNow marks the currently running test as failed and stops running it.
32 // Something ought to have been previously logged so the developer can tell
33 // what went wrong. The higher level helper functions will fail the test
34 // and do the logging properly.
35 func (c *C) FailNow() {
36 c.Fail()
37 c.stopNow()
38 }
39
40 // Succeed marks the currently running test as succeeded, undoing any
41 // previous failures.
42 func (c *C) Succeed() {
43 c.setStatus(succeededSt)
44 }
45
46 // SucceedNow marks the currently running test as succeeded, undoing any
47 // previous failures, and stops running the test.
48 func (c *C) SucceedNow() {
49 c.Succeed()
50 c.stopNow()
51 }
52
53 // ExpectFailure informs that the running test is knowingly broken for
54 // the provided reason. If the test does not fail, an error will be reported
55 // to raise attention to this fact. This method is useful to temporarily
56 // disable tests which cover well known problems until a better time to
57 // fix the problem is found, without forgetting about the fact that a
58 // failure still exists.
59 func (c *C) ExpectFailure(reason string) {
60 if reason == "" {
61 panic("Missing reason why the test is expected to fail")
62 }
63 c.mustFail = true
64 c.reason = reason
65 }
66
67 // Skip skips the running test for the provided reason. If run from within
68 // SetUpTest, the individual test being set up will be skipped, and if run
69 // from within SetUpSuite, the whole suite is skipped.
70 func (c *C) Skip(reason string) {
71 if reason == "" {
72 panic("Missing reason why the test is being skipped")
73 }
74 c.reason = reason
75 c.setStatus(skippedSt)
76 c.stopNow()
77 }
78
79 // Parallel will mark the test run parallel within a test suite.
80 func (c *C) Parallel() {
81 c.parallel <- c
82 }
83
84 // -----------------------------------------------------------------------
85 // Basic logging.
86
87 // GetTestLog returns the current test error output.
88 func (c *C) GetTestLog() string {
89 return c.logb.String()
90 }
91
92 // Log logs some information into the test error output.
93 // The provided arguments are assembled together into a string with fmt.Sprint.
94 func (c *C) Log(args ...interface{}) {
95 c.log(args...)
96 }
97
98 // Log logs some information into the test error output.
99 // The provided arguments are assembled together into a string with fmt.Sprintf.
100 func (c *C) Logf(format string, args ...interface{}) {
101 c.logf(format, args...)
102 }
103
104 // Output enables *C to be used as a logger in functions that require only
105 // the minimum interface of *log.Logger.
106 func (c *C) Output(calldepth int, s string) error {
107 d := time.Now().Sub(c.startTime)
108 msec := d / time.Millisecond
109 sec := d / time.Second
110 min := d / time.Minute
111
112 c.Logf("[LOG] %d:%02d.%03d %s", min, sec%60, msec%1000, s)
113 return nil
114 }
115
116 // Error logs an error into the test error output and marks the test as failed.
117 // The provided arguments are assembled together into a string with fmt.Sprint.
118 func (c *C) Error(args ...interface{}) {
119 c.logCaller(1)
120 c.logString(fmt.Sprint("Error: ", fmt.Sprint(args...)))
121 c.logNewLine()
122 c.Fail()
123 }
124
125 // Errorf logs an error into the test error output and marks the test as failed.
126 // The provided arguments are assembled together into a string with fmt.Sprintf.
127 func (c *C) Errorf(format string, args ...interface{}) {
128 c.logCaller(1)
129 c.logString(fmt.Sprintf("Error: "+format, args...))
130 c.logNewLine()
131 c.Fail()
132 }
133
134 // Fatal logs an error into the test error output, marks the test as failed, and
135 // stops the test execution. The provided arguments are assembled together into
136 // a string with fmt.Sprint.
137 func (c *C) Fatal(args ...interface{}) {
138 c.logCaller(1)
139 c.logString(fmt.Sprint("Error: ", fmt.Sprint(args...)))
140 c.logNewLine()
141 c.FailNow()
142 }
143
144 // Fatlaf logs an error into the test error output, marks the test as failed, and
145 // stops the test execution. The provided arguments are assembled together into
146 // a string with fmt.Sprintf.
147 func (c *C) Fatalf(format string, args ...interface{}) {
148 c.logCaller(1)
149 c.logString(fmt.Sprint("Error: ", fmt.Sprintf(format, args...)))
150 c.logNewLine()
151 c.FailNow()
152 }
153
154 // -----------------------------------------------------------------------
155 // Generic checks and assertions based on checkers.
156
157 // Check verifies if the first value matches the expected value according
158 // to the provided checker. If they do not match, an error is logged, the
159 // test is marked as failed, and the test execution continues.
160 //
161 // Some checkers may not need the expected argument (e.g. IsNil).
162 //
163 // Extra arguments provided to the function are logged next to the reported
164 // problem when the matching fails.
165 func (c *C) Check(obtained interface{}, checker Checker, args ...interface{}) bool {
166 return c.internalCheck("Check", obtained, checker, args...)
167 }
168
169 // Assert ensures that the first value matches the expected value according
170 // to the provided checker. If they do not match, an error is logged, the
171 // test is marked as failed, and the test execution stops.
172 //
173 // Some checkers may not need the expected argument (e.g. IsNil).
174 //
175 // Extra arguments provided to the function are logged next to the reported
176 // problem when the matching fails.
177 func (c *C) Assert(obtained interface{}, checker Checker, args ...interface{}) {
178 if !c.internalCheck("Assert", obtained, checker, args...) {
179 c.stopNow()
180 }
181 }
182
183 func (c *C) internalCheck(funcName string, obtained interface{}, checker Checker, args ...interface{}) bool {
184 if checker == nil {
185 c.logCaller(2)
186 c.logString(fmt.Sprintf("%s(obtained, nil!?, ...):", funcName))
187 c.logString("Oops.. you've provided a nil checker!")
188 c.logNewLine()
189 c.Fail()
190 return false
191 }
192
193 // If the last argument is a bug info, extract it out.
194 var comment CommentInterface
195 if len(args) > 0 {
196 if c, ok := args[len(args)-1].(CommentInterface); ok {
197 comment = c
198 args = args[:len(args)-1]
199 }
200 }
201
202 params := append([]interface{}{obtained}, args...)
203 info := checker.Info()
204
205 if len(params) != len(info.Params) {
206 names := append([]string{info.Params[0], info.Name}, info.Params[1:]...)
207 c.logCaller(2)
208 c.logString(fmt.Sprintf("%s(%s):", funcName, strings.Join(names, ", ")))
209 c.logString(fmt.Sprintf("Wrong number of parameters for %s: want %d, got %d", info.Name, len(names), len(params)+1))
210 c.logNewLine()
211 c.Fail()
212 return false
213 }
214
215 // Copy since it may be mutated by Check.
216 names := append([]string{}, info.Params...)
217
218 // Do the actual check.
219 result, error := checker.Check(params, names)
220 if !result || error != "" {
221 c.logCaller(2)
222 for i := 0; i != len(params); i++ {
223 c.logValue(names[i], params[i])
224 }
225 if comment != nil {
226 c.logString(comment.CheckCommentString())
227 }
228 if error != "" {
229 c.logString(error)
230 }
231 c.logNewLine()
232 c.Fail()
233 return false
234 }
235 return true
236 }