~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/check.v1/helpers.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// -----------------------------------------------------------------------
 
80
// Basic logging.
 
81
 
 
82
// GetTestLog returns the current test error output.
 
83
func (c *C) GetTestLog() string {
 
84
        return c.logb.String()
 
85
}
 
86
 
 
87
// Log logs some information into the test error output.
 
88
// The provided arguments are assembled together into a string with fmt.Sprint.
 
89
func (c *C) Log(args ...interface{}) {
 
90
        c.log(args...)
 
91
}
 
92
 
 
93
// Log logs some information into the test error output.
 
94
// The provided arguments are assembled together into a string with fmt.Sprintf.
 
95
func (c *C) Logf(format string, args ...interface{}) {
 
96
        c.logf(format, args...)
 
97
}
 
98
 
 
99
// Output enables *C to be used as a logger in functions that require only
 
100
// the minimum interface of *log.Logger.
 
101
func (c *C) Output(calldepth int, s string) error {
 
102
        d := time.Now().Sub(c.startTime)
 
103
        msec := d / time.Millisecond
 
104
        sec := d / time.Second
 
105
        min := d / time.Minute
 
106
 
 
107
        c.Logf("[LOG] %d:%02d.%03d %s", min, sec%60, msec%1000, s)
 
108
        return nil
 
109
}
 
110
 
 
111
// Error logs an error into the test error output and marks the test as failed.
 
112
// The provided arguments are assembled together into a string with fmt.Sprint.
 
113
func (c *C) Error(args ...interface{}) {
 
114
        c.logCaller(1)
 
115
        c.logString(fmt.Sprint("Error: ", fmt.Sprint(args...)))
 
116
        c.logNewLine()
 
117
        c.Fail()
 
118
}
 
119
 
 
120
// Errorf logs an error into the test error output and marks the test as failed.
 
121
// The provided arguments are assembled together into a string with fmt.Sprintf.
 
122
func (c *C) Errorf(format string, args ...interface{}) {
 
123
        c.logCaller(1)
 
124
        c.logString(fmt.Sprintf("Error: "+format, args...))
 
125
        c.logNewLine()
 
126
        c.Fail()
 
127
}
 
128
 
 
129
// Fatal logs an error into the test error output, marks the test as failed, and
 
130
// stops the test execution. The provided arguments are assembled together into
 
131
// a string with fmt.Sprint.
 
132
func (c *C) Fatal(args ...interface{}) {
 
133
        c.logCaller(1)
 
134
        c.logString(fmt.Sprint("Error: ", fmt.Sprint(args...)))
 
135
        c.logNewLine()
 
136
        c.FailNow()
 
137
}
 
138
 
 
139
// Fatlaf logs an error into the test error output, marks the test as failed, and
 
140
// stops the test execution. The provided arguments are assembled together into
 
141
// a string with fmt.Sprintf.
 
142
func (c *C) Fatalf(format string, args ...interface{}) {
 
143
        c.logCaller(1)
 
144
        c.logString(fmt.Sprint("Error: ", fmt.Sprintf(format, args...)))
 
145
        c.logNewLine()
 
146
        c.FailNow()
 
147
}
 
148
 
 
149
// -----------------------------------------------------------------------
 
150
// Generic checks and assertions based on checkers.
 
151
 
 
152
// Check verifies if the first value matches the expected value according
 
153
// to the provided checker. If they do not match, an error is logged, the
 
154
// test is marked as failed, and the test execution continues.
 
155
//
 
156
// Some checkers may not need the expected argument (e.g. IsNil).
 
157
//
 
158
// Extra arguments provided to the function are logged next to the reported
 
159
// problem when the matching fails.
 
160
func (c *C) Check(obtained interface{}, checker Checker, args ...interface{}) bool {
 
161
        return c.internalCheck("Check", obtained, checker, args...)
 
162
}
 
163
 
 
164
// Assert ensures that the first value matches the expected value according
 
165
// to the provided checker. If they do not match, an error is logged, the
 
166
// test is marked as failed, and the test execution stops.
 
167
//
 
168
// Some checkers may not need the expected argument (e.g. IsNil).
 
169
//
 
170
// Extra arguments provided to the function are logged next to the reported
 
171
// problem when the matching fails.
 
172
func (c *C) Assert(obtained interface{}, checker Checker, args ...interface{}) {
 
173
        if !c.internalCheck("Assert", obtained, checker, args...) {
 
174
                c.stopNow()
 
175
        }
 
176
}
 
177
 
 
178
func (c *C) internalCheck(funcName string, obtained interface{}, checker Checker, args ...interface{}) bool {
 
179
        if checker == nil {
 
180
                c.logCaller(2)
 
181
                c.logString(fmt.Sprintf("%s(obtained, nil!?, ...):", funcName))
 
182
                c.logString("Oops.. you've provided a nil checker!")
 
183
                c.logNewLine()
 
184
                c.Fail()
 
185
                return false
 
186
        }
 
187
 
 
188
        // If the last argument is a bug info, extract it out.
 
189
        var comment CommentInterface
 
190
        if len(args) > 0 {
 
191
                if c, ok := args[len(args)-1].(CommentInterface); ok {
 
192
                        comment = c
 
193
                        args = args[:len(args)-1]
 
194
                }
 
195
        }
 
196
 
 
197
        params := append([]interface{}{obtained}, args...)
 
198
        info := checker.Info()
 
199
 
 
200
        if len(params) != len(info.Params) {
 
201
                names := append([]string{info.Params[0], info.Name}, info.Params[1:]...)
 
202
                c.logCaller(2)
 
203
                c.logString(fmt.Sprintf("%s(%s):", funcName, strings.Join(names, ", ")))
 
204
                c.logString(fmt.Sprintf("Wrong number of parameters for %s: want %d, got %d", info.Name, len(names), len(params)+1))
 
205
                c.logNewLine()
 
206
                c.Fail()
 
207
                return false
 
208
        }
 
209
 
 
210
        // Copy since it may be mutated by Check.
 
211
        names := append([]string{}, info.Params...)
 
212
 
 
213
        // Do the actual check.
 
214
        result, error := checker.Check(params, names)
 
215
        if !result || error != "" {
 
216
                c.logCaller(2)
 
217
                for i := 0; i != len(params); i++ {
 
218
                        c.logValue(names[i], params[i])
 
219
                }
 
220
                if comment != nil {
 
221
                        c.logString(comment.CheckCommentString())
 
222
                }
 
223
                if error != "" {
 
224
                        c.logString(error)
 
225
                }
 
226
                c.logNewLine()
 
227
                c.Fail()
 
228
                return false
 
229
        }
 
230
        return true
 
231
}