| 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. |
| 6 | |
| 7 | package check_test |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | "log" |
| 12 | "os" |
| 13 | "regexp" |
| 14 | "strings" |
| 15 | |
| 16 | "github.com/pingcap/check" |
| 17 | ) |
| 18 | |
| 19 | // ----------------------------------------------------------------------- |
| 20 | // Foundation test suite. |
| 21 | |
| 22 | type FoundationS struct{} |
| 23 | |
| 24 | var foundationS = check.Suite(&FoundationS{}) |
| 25 | |
| 26 | func (s *FoundationS) TestCountSuite(c *check.C) { |
| 27 | suitesRun += 1 |
| 28 | } |
| 29 | |
| 30 | func (s *FoundationS) TestErrorf(c *check.C) { |
| 31 | // Do not use checkState() here. It depends on Errorf() working. |
| 32 | expectedLog := fmt.Sprintf("foundation_test.go:%d:\n"+ |
| 33 | " c.Errorf(\"Error %%v!\", \"message\")\n"+ |
| 34 | "... Error: Error message!\n\n", |
| 35 | getMyLine()+1) |
| 36 | c.Errorf("Error %v!", "message") |
| 37 | failed := c.Failed() |
| 38 | c.Succeed() |
| 39 | if log := c.GetTestLog(); log != expectedLog { |
| 40 | c.Logf("Errorf() logged %#v rather than %#v", log, expectedLog) |
| 41 | c.Fail() |
| 42 | } |
| 43 | if !failed { |
| 44 | c.Logf("Errorf() didn't put the test in a failed state") |
| 45 | c.Fail() |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func (s *FoundationS) TestError(c *check.C) { |
| 50 | expectedLog := fmt.Sprintf("foundation_test.go:%d:\n"+ |
| 51 | " c\\.Error\\(\"Error \", \"message!\"\\)\n"+ |
| 52 | "\\.\\.\\. Error: Error message!\n\n", |
| 53 | getMyLine()+1) |
| 54 | c.Error("Error ", "message!") |
| 55 | checkState(c, nil, |
| 56 | &expectedState{ |
| 57 | name: "Error(`Error `, `message!`)", |
| 58 | failed: true, |
| 59 | log: expectedLog, |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | func (s *FoundationS) TestFailNow(c *check.C) { |
| 64 | defer (func() { |
| 65 | if !c.Failed() { |
| 66 | c.Error("FailNow() didn't fail the test") |
| 67 | } else { |
| 68 | c.Succeed() |
| 69 | if c.GetTestLog() != "" { |
| 70 | c.Error("Something got logged:\n" + c.GetTestLog()) |
| 71 | } |
| 72 | } |
| 73 | })() |
| 74 | |
| 75 | c.FailNow() |
| 76 | c.Log("FailNow() didn't stop the test") |
| 77 | } |
| 78 | |
| 79 | func (s *FoundationS) TestSucceedNow(c *check.C) { |
| 80 | defer (func() { |
| 81 | if c.Failed() { |
| 82 | c.Error("SucceedNow() didn't succeed the test") |
| 83 | } |
| 84 | if c.GetTestLog() != "" { |
| 85 | c.Error("Something got logged:\n" + c.GetTestLog()) |
| 86 | } |
| 87 | })() |
| 88 | |
| 89 | c.Fail() |
| 90 | c.SucceedNow() |
| 91 | c.Log("SucceedNow() didn't stop the test") |
| 92 | } |
| 93 | |
| 94 | func (s *FoundationS) TestFailureHeader(c *check.C) { |
| 95 | output := String{} |
| 96 | failHelper := FailHelper{} |
| 97 | check.Run(&failHelper, &check.RunConf{Output: &output}) |
| 98 | header := fmt.Sprintf(""+ |
| 99 | "\n-----------------------------------"+ |
| 100 | "-----------------------------------\n"+ |
| 101 | "FAIL: check_test.go:%d: FailHelper.TestLogAndFail\n", |
| 102 | failHelper.testLine) |
| 103 | if strings.Index(output.value, header) == -1 { |
| 104 | c.Errorf(""+ |
| 105 | "Failure didn't print a proper header.\n"+ |
| 106 | "... Got:\n%s... Expected something with:\n%s", |
| 107 | output.value, header) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func (s *FoundationS) TestFatal(c *check.C) { |
| 112 | var line int |
| 113 | defer (func() { |
| 114 | if !c.Failed() { |
| 115 | c.Error("Fatal() didn't fail the test") |
| 116 | } else { |
| 117 | c.Succeed() |
| 118 | expected := fmt.Sprintf("foundation_test.go:%d:\n"+ |
| 119 | " c.Fatal(\"Die \", \"now!\")\n"+ |
| 120 | "... Error: Die now!\n\n", |
| 121 | line) |
| 122 | if c.GetTestLog() != expected { |
| 123 | c.Error("Incorrect log:", c.GetTestLog()) |
| 124 | } |
| 125 | } |
| 126 | })() |
| 127 | |
| 128 | line = getMyLine() + 1 |
| 129 | c.Fatal("Die ", "now!") |
| 130 | c.Log("Fatal() didn't stop the test") |
| 131 | } |
| 132 | |
| 133 | func (s *FoundationS) TestFatalf(c *check.C) { |
| 134 | var line int |
| 135 | defer (func() { |
| 136 | if !c.Failed() { |
| 137 | c.Error("Fatalf() didn't fail the test") |
| 138 | } else { |
| 139 | c.Succeed() |
| 140 | expected := fmt.Sprintf("foundation_test.go:%d:\n"+ |
| 141 | " c.Fatalf(\"Die %%s!\", \"now\")\n"+ |
| 142 | "... Error: Die now!\n\n", |
| 143 | line) |
| 144 | if c.GetTestLog() != expected { |
| 145 | c.Error("Incorrect log:", c.GetTestLog()) |
| 146 | } |
| 147 | } |
| 148 | })() |
| 149 | |
| 150 | line = getMyLine() + 1 |
| 151 | c.Fatalf("Die %s!", "now") |
| 152 | c.Log("Fatalf() didn't stop the test") |
| 153 | } |
| 154 | |
| 155 | func (s *FoundationS) TestCallerLoggingInsideTest(c *check.C) { |
| 156 | log := fmt.Sprintf(""+ |
| 157 | "foundation_test.go:%d:\n"+ |
| 158 | " result := c.Check\\(10, check.Equals, 20\\)\n"+ |
| 159 | "\\.\\.\\. obtained int = 10\n"+ |
| 160 | "\\.\\.\\. expected int = 20\n\n", |
| 161 | getMyLine()+1) |
| 162 | result := c.Check(10, check.Equals, 20) |
| 163 | checkState(c, result, |
| 164 | &expectedState{ |
| 165 | name: "Check(10, Equals, 20)", |
| 166 | result: false, |
| 167 | failed: true, |
| 168 | log: log, |
| 169 | }) |
| 170 | } |
| 171 | |
| 172 | func (s *FoundationS) TestCallerLoggingInDifferentFile(c *check.C) { |
| 173 | result, line := checkEqualWrapper(c, 10, 20) |
| 174 | testLine := getMyLine() - 1 |
| 175 | log := fmt.Sprintf(""+ |
| 176 | "foundation_test.go:%d:\n"+ |
| 177 | " result, line := checkEqualWrapper\\(c, 10, 20\\)\n"+ |
| 178 | "check_test.go:%d:\n"+ |
| 179 | " return c.Check\\(obtained, check.Equals, expected\\), getMyLine\\(\\)\n"+ |
| 180 | "\\.\\.\\. obtained int = 10\n"+ |
| 181 | "\\.\\.\\. expected int = 20\n\n", |
| 182 | testLine, line) |
| 183 | checkState(c, result, |
| 184 | &expectedState{ |
| 185 | name: "Check(10, Equals, 20)", |
| 186 | result: false, |
| 187 | failed: true, |
| 188 | log: log, |
| 189 | }) |
| 190 | } |
| 191 | |
| 192 | // ----------------------------------------------------------------------- |
| 193 | // ExpectFailure() inverts the logic of failure. |
| 194 | |
| 195 | type ExpectFailureSucceedHelper struct{} |
| 196 | |
| 197 | func (s *ExpectFailureSucceedHelper) TestSucceed(c *check.C) { |
| 198 | c.ExpectFailure("It booms!") |
| 199 | c.Error("Boom!") |
| 200 | } |
| 201 | |
| 202 | type ExpectFailureFailHelper struct{} |
| 203 | |
| 204 | func (s *ExpectFailureFailHelper) TestFail(c *check.C) { |
| 205 | c.ExpectFailure("Bug #XYZ") |
| 206 | } |
| 207 | |
| 208 | func (s *FoundationS) TestExpectFailureFail(c *check.C) { |
| 209 | helper := ExpectFailureFailHelper{} |
| 210 | output := String{} |
| 211 | result := check.Run(&helper, &check.RunConf{Output: &output}) |
| 212 | |
| 213 | expected := "" + |
| 214 | "^\n-+\n" + |
| 215 | "FAIL: foundation_test\\.go:[0-9]+:" + |
| 216 | " ExpectFailureFailHelper\\.TestFail\n\n" + |
| 217 | "\\.\\.\\. Error: Test succeeded, but was expected to fail\n" + |
| 218 | "\\.\\.\\. Reason: Bug #XYZ\n$" |
| 219 | |
| 220 | matched, err := regexp.MatchString(expected, output.value) |
| 221 | if err != nil { |
| 222 | c.Error("Bad expression: ", expected) |
| 223 | } else if !matched { |
| 224 | c.Error("ExpectFailure() didn't log properly:\n", output.value) |
| 225 | } |
| 226 | |
| 227 | c.Assert(result.ExpectedFailures, check.Equals, 0) |
| 228 | } |
| 229 | |
| 230 | func (s *FoundationS) TestExpectFailureSucceed(c *check.C) { |
| 231 | helper := ExpectFailureSucceedHelper{} |
| 232 | output := String{} |
| 233 | result := check.Run(&helper, &check.RunConf{Output: &output}) |
| 234 | |
| 235 | c.Assert(output.value, check.Equals, "") |
| 236 | c.Assert(result.ExpectedFailures, check.Equals, 1) |
| 237 | } |
| 238 | |
| 239 | func (s *FoundationS) TestExpectFailureSucceedVerbose(c *check.C) { |
| 240 | helper := ExpectFailureSucceedHelper{} |
| 241 | output := String{} |
| 242 | result := check.Run(&helper, &check.RunConf{Output: &output, Verbose: true}) |
| 243 | |
| 244 | expected := "" + |
| 245 | "FAIL EXPECTED: foundation_test\\.go:[0-9]+:" + |
| 246 | " ExpectFailureSucceedHelper\\.TestSucceed \\(It booms!\\)\t *[.0-9]+s\n" |
| 247 | |
| 248 | matched, err := regexp.MatchString(expected, output.value) |
| 249 | if err != nil { |
| 250 | c.Error("Bad expression: ", expected) |
| 251 | } else if !matched { |
| 252 | c.Error("ExpectFailure() didn't log properly:\n", output.value) |
| 253 | } |
| 254 | |
| 255 | c.Assert(result.ExpectedFailures, check.Equals, 1) |
| 256 | } |
| 257 | |
| 258 | // ----------------------------------------------------------------------- |
| 259 | // Skip() allows stopping a test without positive/negative results. |
| 260 | |
| 261 | type SkipTestHelper struct{} |
| 262 | |
| 263 | func (s *SkipTestHelper) TestFail(c *check.C) { |
| 264 | c.Skip("Wrong platform or whatever") |
| 265 | c.Error("Boom!") |
| 266 | } |
| 267 | |
| 268 | func (s *FoundationS) TestSkip(c *check.C) { |
| 269 | helper := SkipTestHelper{} |
| 270 | output := String{} |
| 271 | check.Run(&helper, &check.RunConf{Output: &output}) |
| 272 | |
| 273 | if output.value != "" { |
| 274 | c.Error("Skip() logged something:\n", output.value) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | func (s *FoundationS) TestSkipVerbose(c *check.C) { |
| 279 | helper := SkipTestHelper{} |
| 280 | output := String{} |
| 281 | check.Run(&helper, &check.RunConf{Output: &output, Verbose: true}) |
| 282 | |
| 283 | expected := "SKIP: foundation_test\\.go:[0-9]+: SkipTestHelper\\.TestFail" + |
| 284 | " \\(Wrong platform or whatever\\)" |
| 285 | matched, err := regexp.MatchString(expected, output.value) |
| 286 | if err != nil { |
| 287 | c.Error("Bad expression: ", expected) |
| 288 | } else if !matched { |
| 289 | c.Error("Skip() didn't log properly:\n", output.value) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // ----------------------------------------------------------------------- |
| 294 | // Check minimum *log.Logger interface provided by *check.C. |
| 295 | |
| 296 | type minLogger interface { |
| 297 | Output(calldepth int, s string) error |
| 298 | } |
| 299 | |
| 300 | func (s *BootstrapS) TestMinLogger(c *check.C) { |
| 301 | var logger minLogger |
| 302 | logger = log.New(os.Stderr, "", 0) |
| 303 | logger = c |
| 304 | logger.Output(0, "Hello there") |
| 305 | expected := `\[LOG\] [0-9]+:[0-9][0-9]\.[0-9][0-9][0-9] +Hello there\n` |
| 306 | output := c.GetTestLog() |
| 307 | c.Assert(output, check.Matches, expected) |
| 308 | } |
| 309 | |
| 310 | // ----------------------------------------------------------------------- |
| 311 | // Ensure that suites with embedded types are working fine, including the |
| 312 | // the workaround for issue 906. |
| 313 | |
| 314 | type EmbeddedInternalS struct { |
| 315 | called bool |
| 316 | } |
| 317 | |
| 318 | type EmbeddedS struct { |
| 319 | EmbeddedInternalS |
| 320 | } |
| 321 | |
| 322 | var embeddedS = check.Suite(&EmbeddedS{}) |
| 323 | |
| 324 | func (s *EmbeddedS) TestCountSuite(c *check.C) { |
| 325 | suitesRun += 1 |
| 326 | } |
| 327 | |
| 328 | func (s *EmbeddedInternalS) TestMethod(c *check.C) { |
| 329 | c.Error("TestMethod() of the embedded type was called!?") |
| 330 | } |
| 331 | |
| 332 | func (s *EmbeddedS) TestMethod(c *check.C) { |
| 333 | // http://code.google.com/p/go/issues/detail?id=906 |
| 334 | c.Check(s.called, check.Equals, false) // Go issue 906 is affecting the runner? |
| 335 | s.called = true |
| 336 | } |