reporter_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package check_test
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "runtime"
  6. . "gopkg.in/check.v1"
  7. )
  8. var _ = Suite(&reporterS{})
  9. type reporterS struct {
  10. testFile string
  11. }
  12. func (s *reporterS) SetUpSuite(c *C) {
  13. _, fileName, _, ok := runtime.Caller(0)
  14. c.Assert(ok, Equals, true)
  15. s.testFile = filepath.Base(fileName)
  16. }
  17. func (s *reporterS) TestWrite(c *C) {
  18. testString := "test string"
  19. output := String{}
  20. dummyStream := true
  21. dummyVerbose := true
  22. o := NewOutputWriter(&output, dummyStream, dummyVerbose)
  23. o.Write([]byte(testString))
  24. c.Assert(output.value, Equals, testString)
  25. }
  26. func (s *reporterS) TestWriteCallStartedWithStreamFlag(c *C) {
  27. testLabel := "test started label"
  28. stream := true
  29. output := String{}
  30. dummyVerbose := true
  31. o := NewOutputWriter(&output, stream, dummyVerbose)
  32. o.WriteCallStarted(testLabel, c)
  33. expected := fmt.Sprintf("%s: %s:\\d+: %s\n", testLabel, s.testFile, c.TestName())
  34. c.Assert(output.value, Matches, expected)
  35. }
  36. func (s *reporterS) TestWriteCallStartedWithoutStreamFlag(c *C) {
  37. stream := false
  38. output := String{}
  39. dummyLabel := "dummy"
  40. dummyVerbose := true
  41. o := NewOutputWriter(&output, stream, dummyVerbose)
  42. o.WriteCallStarted(dummyLabel, c)
  43. c.Assert(output.value, Equals, "")
  44. }
  45. func (s *reporterS) TestWriteCallProblemWithStreamFlag(c *C) {
  46. testLabel := "test problem label"
  47. stream := true
  48. output := String{}
  49. dummyVerbose := true
  50. o := NewOutputWriter(&output, stream, dummyVerbose)
  51. o.WriteCallProblem(testLabel, c)
  52. expected := fmt.Sprintf("%s: %s:\\d+: %s\n\n", testLabel, s.testFile, c.TestName())
  53. c.Assert(output.value, Matches, expected)
  54. }
  55. func (s *reporterS) TestWriteCallProblemWithoutStreamFlag(c *C) {
  56. testLabel := "test problem label"
  57. stream := false
  58. output := String{}
  59. dummyVerbose := true
  60. o := NewOutputWriter(&output, stream, dummyVerbose)
  61. o.WriteCallProblem(testLabel, c)
  62. expected := fmt.Sprintf(""+
  63. "\n"+
  64. "----------------------------------------------------------------------\n"+
  65. "%s: %s:\\d+: %s\n\n", testLabel, s.testFile, c.TestName())
  66. c.Assert(output.value, Matches, expected)
  67. }
  68. func (s *reporterS) TestWriteCallProblemWithoutStreamFlagWithLog(c *C) {
  69. testLabel := "test problem label"
  70. testLog := "test log"
  71. stream := false
  72. output := String{}
  73. dummyVerbose := true
  74. o := NewOutputWriter(&output, stream, dummyVerbose)
  75. c.Log(testLog)
  76. o.WriteCallProblem(testLabel, c)
  77. expected := fmt.Sprintf(""+
  78. "\n"+
  79. "----------------------------------------------------------------------\n"+
  80. "%s: %s:\\d+: %s\n\n%s\n", testLabel, s.testFile, c.TestName(), testLog)
  81. c.Assert(output.value, Matches, expected)
  82. }
  83. func (s *reporterS) TestWriteCallSuccessWithStreamFlag(c *C) {
  84. testLabel := "test success label"
  85. stream := true
  86. output := String{}
  87. dummyVerbose := true
  88. o := NewOutputWriter(&output, stream, dummyVerbose)
  89. o.WriteCallSuccess(testLabel, c)
  90. expected := fmt.Sprintf("%s: %s:\\d+: %s\t\\d\\.\\d+s\n\n", testLabel, s.testFile, c.TestName())
  91. c.Assert(output.value, Matches, expected)
  92. }
  93. func (s *reporterS) TestWriteCallSuccessWithStreamFlagAndReason(c *C) {
  94. testLabel := "test success label"
  95. testReason := "test skip reason"
  96. stream := true
  97. output := String{}
  98. dummyVerbose := true
  99. o := NewOutputWriter(&output, stream, dummyVerbose)
  100. c.FakeSkip(testReason)
  101. o.WriteCallSuccess(testLabel, c)
  102. expected := fmt.Sprintf("%s: %s:\\d+: %s \\(%s\\)\t\\d\\.\\d+s\n\n",
  103. testLabel, s.testFile, c.TestName(), testReason)
  104. c.Assert(output.value, Matches, expected)
  105. }
  106. func (s *reporterS) TestWriteCallSuccessWithoutStreamFlagWithVerboseFlag(c *C) {
  107. testLabel := "test success label"
  108. stream := false
  109. verbose := true
  110. output := String{}
  111. o := NewOutputWriter(&output, stream, verbose)
  112. o.WriteCallSuccess(testLabel, c)
  113. expected := fmt.Sprintf("%s: %s:\\d+: %s\t\\d\\.\\d+s\n", testLabel, s.testFile, c.TestName())
  114. c.Assert(output.value, Matches, expected)
  115. }
  116. func (s *reporterS) TestWriteCallSuccessWithoutStreamFlagWithoutVerboseFlag(c *C) {
  117. testLabel := "test success label"
  118. stream := false
  119. verbose := false
  120. output := String{}
  121. o := NewOutputWriter(&output, stream, verbose)
  122. o.WriteCallSuccess(testLabel, c)
  123. c.Assert(output.value, Equals, "")
  124. }