foundation_test.go 8.8 KB

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