1
// These tests check that the foundations of gocheck are working properly.
2
// They already assume that fundamental failing is working already, though,
3
// since this was tested in bootstrap_test.go. Even then, some care may
4
// still have to be taken when using external functions, since they should
5
// of course not rely on functionality tested here.
11
"launchpad.net/gocheck"
18
// -----------------------------------------------------------------------
19
// Foundation test suite.
21
type FoundationS struct{}
23
var foundationS = gocheck.Suite(&FoundationS{})
25
func (s *FoundationS) TestCountSuite(c *gocheck.C) {
29
func (s *FoundationS) TestErrorf(c *gocheck.C) {
30
// Do not use checkState() here. It depends on Errorf() working.
31
expectedLog := fmt.Sprintf("foundation_test.go:%d:\n"+
32
" c.Errorf(\"Error %%v!\", \"message\")\n"+
33
"... Error: Error message!\n\n",
35
c.Errorf("Error %v!", "message")
38
if log := c.GetTestLog(); log != expectedLog {
39
c.Logf("Errorf() logged %#v rather than %#v", log, expectedLog)
43
c.Logf("Errorf() didn't put the test in a failed state")
48
func (s *FoundationS) TestError(c *gocheck.C) {
49
expectedLog := fmt.Sprintf("foundation_test.go:%d:\n"+
50
" c\\.Error\\(\"Error \", \"message!\"\\)\n"+
51
"\\.\\.\\. Error: Error message!\n\n",
53
c.Error("Error ", "message!")
56
name: "Error(`Error `, `message!`)",
62
func (s *FoundationS) TestFailNow(c *gocheck.C) {
65
c.Error("FailNow() didn't fail the test")
68
if c.GetTestLog() != "" {
69
c.Error("Something got logged:\n" + c.GetTestLog())
75
c.Log("FailNow() didn't stop the test")
78
func (s *FoundationS) TestSucceedNow(c *gocheck.C) {
81
c.Error("SucceedNow() didn't succeed the test")
83
if c.GetTestLog() != "" {
84
c.Error("Something got logged:\n" + c.GetTestLog())
90
c.Log("SucceedNow() didn't stop the test")
93
func (s *FoundationS) TestFailureHeader(c *gocheck.C) {
95
failHelper := FailHelper{}
96
gocheck.Run(&failHelper, &gocheck.RunConf{Output: &output})
97
header := fmt.Sprintf(""+
98
"\n-----------------------------------"+
99
"-----------------------------------\n"+
100
"FAIL: gocheck_test.go:%d: FailHelper.TestLogAndFail\n",
102
if strings.Index(output.value, header) == -1 {
104
"Failure didn't print a proper header.\n"+
105
"... Got:\n%s... Expected something with:\n%s",
106
output.value, header)
110
func (s *FoundationS) TestFatal(c *gocheck.C) {
114
c.Error("Fatal() didn't fail the test")
117
expected := fmt.Sprintf("foundation_test.go:%d:\n"+
118
" c.Fatal(\"Die \", \"now!\")\n"+
119
"... Error: Die now!\n\n",
121
if c.GetTestLog() != expected {
122
c.Error("Incorrect log:", c.GetTestLog())
127
line = getMyLine() + 1
128
c.Fatal("Die ", "now!")
129
c.Log("Fatal() didn't stop the test")
132
func (s *FoundationS) TestFatalf(c *gocheck.C) {
136
c.Error("Fatalf() didn't fail the test")
139
expected := fmt.Sprintf("foundation_test.go:%d:\n"+
140
" c.Fatalf(\"Die %%s!\", \"now\")\n"+
141
"... Error: Die now!\n\n",
143
if c.GetTestLog() != expected {
144
c.Error("Incorrect log:", c.GetTestLog())
149
line = getMyLine() + 1
150
c.Fatalf("Die %s!", "now")
151
c.Log("Fatalf() didn't stop the test")
154
func (s *FoundationS) TestCallerLoggingInsideTest(c *gocheck.C) {
155
log := fmt.Sprintf(""+
156
"foundation_test.go:%d:\n"+
157
" result := c.Check\\(10, gocheck.Equals, 20\\)\n"+
158
"\\.\\.\\. obtained int = 10\n"+
159
"\\.\\.\\. expected int = 20\n\n",
161
result := c.Check(10, gocheck.Equals, 20)
162
checkState(c, result,
164
name: "Check(10, Equals, 20)",
171
func (s *FoundationS) TestCallerLoggingInDifferentFile(c *gocheck.C) {
172
result, line := checkEqualWrapper(c, 10, 20)
173
testLine := getMyLine() - 1
174
log := fmt.Sprintf(""+
175
"foundation_test.go:%d:\n"+
176
" result, line := checkEqualWrapper\\(c, 10, 20\\)\n"+
177
"gocheck_test.go:%d:\n"+
178
" return c.Check\\(obtained, gocheck.Equals, expected\\), getMyLine\\(\\)\n"+
179
"\\.\\.\\. obtained int = 10\n"+
180
"\\.\\.\\. expected int = 20\n\n",
182
checkState(c, result,
184
name: "Check(10, Equals, 20)",
191
// -----------------------------------------------------------------------
192
// ExpectFailure() inverts the logic of failure.
194
type ExpectFailureSucceedHelper struct{}
196
func (s *ExpectFailureSucceedHelper) TestSucceed(c *gocheck.C) {
197
c.ExpectFailure("It booms!")
201
type ExpectFailureFailHelper struct{}
203
func (s *ExpectFailureFailHelper) TestFail(c *gocheck.C) {
204
c.ExpectFailure("Bug #XYZ")
207
func (s *FoundationS) TestExpectFailureFail(c *gocheck.C) {
208
helper := ExpectFailureFailHelper{}
210
result := gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
214
"FAIL: foundation_test\\.go:[0-9]+:" +
215
" ExpectFailureFailHelper\\.TestFail\n\n" +
216
"\\.\\.\\. Error: Test succeeded, but was expected to fail\n" +
217
"\\.\\.\\. Reason: Bug #XYZ\n$"
219
matched, err := regexp.MatchString(expected, output.value)
221
c.Error("Bad expression: ", expected)
223
c.Error("ExpectFailure() didn't log properly:\n", output.value)
226
c.Assert(result.ExpectedFailures, gocheck.Equals, 0)
229
func (s *FoundationS) TestExpectFailureSucceed(c *gocheck.C) {
230
helper := ExpectFailureSucceedHelper{}
232
result := gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
234
c.Assert(output.value, gocheck.Equals, "")
235
c.Assert(result.ExpectedFailures, gocheck.Equals, 1)
238
func (s *FoundationS) TestExpectFailureSucceedVerbose(c *gocheck.C) {
239
helper := ExpectFailureSucceedHelper{}
241
result := gocheck.Run(&helper, &gocheck.RunConf{Output: &output, Verbose: true})
244
"FAIL EXPECTED: foundation_test\\.go:[0-9]+:" +
245
" ExpectFailureSucceedHelper\\.TestSucceed \\(It booms!\\)\t *[.0-9]+s\n"
247
matched, err := regexp.MatchString(expected, output.value)
249
c.Error("Bad expression: ", expected)
251
c.Error("ExpectFailure() didn't log properly:\n", output.value)
254
c.Assert(result.ExpectedFailures, gocheck.Equals, 1)
257
// -----------------------------------------------------------------------
258
// Skip() allows stopping a test without positive/negative results.
260
type SkipTestHelper struct{}
262
func (s *SkipTestHelper) TestFail(c *gocheck.C) {
263
c.Skip("Wrong platform or whatever")
267
func (s *FoundationS) TestSkip(c *gocheck.C) {
268
helper := SkipTestHelper{}
270
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
272
if output.value != "" {
273
c.Error("Skip() logged something:\n", output.value)
277
func (s *FoundationS) TestSkipVerbose(c *gocheck.C) {
278
helper := SkipTestHelper{}
280
gocheck.Run(&helper, &gocheck.RunConf{Output: &output, Verbose: true})
282
expected := "SKIP: foundation_test\\.go:[0-9]+: SkipTestHelper\\.TestFail" +
283
" \\(Wrong platform or whatever\\)"
284
matched, err := regexp.MatchString(expected, output.value)
286
c.Error("Bad expression: ", expected)
288
c.Error("Skip() didn't log properly:\n", output.value)
292
// -----------------------------------------------------------------------
293
// Check minimum *log.Logger interface provided by *gocheck.C.
295
type minLogger interface {
296
Output(calldepth int, s string) error
299
func (s *BootstrapS) TestMinLogger(c *gocheck.C) {
301
logger = log.New(os.Stderr, "", 0)
303
logger.Output(0, "Hello there")
304
expected := "\\[LOG\\] [.0-9]+ Hello there\n"
305
output := c.GetTestLog()
306
matched, err := regexp.MatchString(expected, output)
308
c.Error("Bad expression: ", expected)
310
c.Error("Output() didn't log properly:\n", output)
314
// -----------------------------------------------------------------------
315
// Ensure that suites with embedded types are working fine, including the
316
// the workaround for issue 906.
318
type EmbeddedInternalS struct {
322
type EmbeddedS struct {
326
var embeddedS = gocheck.Suite(&EmbeddedS{})
328
func (s *EmbeddedS) TestCountSuite(c *gocheck.C) {
332
func (s *EmbeddedInternalS) TestMethod(c *gocheck.C) {
333
c.Error("TestMethod() of the embedded type was called!?")
336
func (s *EmbeddedS) TestMethod(c *gocheck.C) {
337
// http://code.google.com/p/go/issues/detail?id=906
338
c.Check(s.called, gocheck.Equals, false) // Go issue 906 is affecting the runner?