reporting_hooks_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package convey
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httptest"
  6. "path"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. "github.com/smartystreets/goconvey/convey/reporting"
  12. )
  13. func TestSingleScopeReported(t *testing.T) {
  14. myReporter, test := setupFakeReporter()
  15. Convey("A", test, func() {
  16. So(1, ShouldEqual, 1)
  17. })
  18. expectEqual(t, "Begin|A|Success|Exit|End", myReporter.wholeStory())
  19. }
  20. func TestNestedScopeReported(t *testing.T) {
  21. myReporter, test := setupFakeReporter()
  22. Convey("A", test, func() {
  23. Convey("B", func() {
  24. So(1, ShouldEqual, 1)
  25. })
  26. })
  27. expectEqual(t, "Begin|A|B|Success|Exit|Exit|End", myReporter.wholeStory())
  28. }
  29. func TestFailureReported(t *testing.T) {
  30. myReporter, test := setupFakeReporter()
  31. Convey("A", test, func() {
  32. So(1, ShouldBeNil)
  33. })
  34. expectEqual(t, "Begin|A|Failure|Exit|End", myReporter.wholeStory())
  35. }
  36. func TestFirstFailureEndsScopeExecution(t *testing.T) {
  37. myReporter, test := setupFakeReporter()
  38. Convey("A", test, func() {
  39. So(1, ShouldBeNil)
  40. So(nil, ShouldBeNil)
  41. })
  42. expectEqual(t, "Begin|A|Failure|Exit|End", myReporter.wholeStory())
  43. }
  44. func TestComparisonFailureDeserializedAndReported(t *testing.T) {
  45. myReporter, test := setupFakeReporter()
  46. Convey("A", test, func() {
  47. So("hi", ShouldEqual, "bye")
  48. })
  49. expectEqual(t, "Begin|A|Failure(bye/hi)|Exit|End", myReporter.wholeStory())
  50. }
  51. func TestNestedFailureReported(t *testing.T) {
  52. myReporter, test := setupFakeReporter()
  53. Convey("A", test, func() {
  54. Convey("B", func() {
  55. So(2, ShouldBeNil)
  56. })
  57. })
  58. expectEqual(t, "Begin|A|B|Failure|Exit|Exit|End", myReporter.wholeStory())
  59. }
  60. func TestSuccessAndFailureReported(t *testing.T) {
  61. myReporter, test := setupFakeReporter()
  62. Convey("A", test, func() {
  63. So(nil, ShouldBeNil)
  64. So(1, ShouldBeNil)
  65. })
  66. expectEqual(t, "Begin|A|Success|Failure|Exit|End", myReporter.wholeStory())
  67. }
  68. func TestIncompleteActionReportedAsSkipped(t *testing.T) {
  69. myReporter, test := setupFakeReporter()
  70. Convey("A", test, func() {
  71. Convey("B", nil)
  72. })
  73. expectEqual(t, "Begin|A|B|Skipped|Exit|Exit|End", myReporter.wholeStory())
  74. }
  75. func TestSkippedConveyReportedAsSkipped(t *testing.T) {
  76. myReporter, test := setupFakeReporter()
  77. Convey("A", test, func() {
  78. SkipConvey("B", func() {
  79. So(1, ShouldEqual, 1)
  80. })
  81. })
  82. expectEqual(t, "Begin|A|B|Skipped|Exit|Exit|End", myReporter.wholeStory())
  83. }
  84. func TestMultipleSkipsAreReported(t *testing.T) {
  85. myReporter, test := setupFakeReporter()
  86. Convey("A", test, func() {
  87. Convey("0", func() {
  88. So(nil, ShouldBeNil)
  89. })
  90. SkipConvey("1", func() {})
  91. SkipConvey("2", func() {})
  92. Convey("3", nil)
  93. Convey("4", nil)
  94. Convey("5", func() {
  95. So(nil, ShouldBeNil)
  96. })
  97. })
  98. expected := "Begin" +
  99. "|A|0|Success|Exit|Exit" +
  100. "|A|1|Skipped|Exit|Exit" +
  101. "|A|2|Skipped|Exit|Exit" +
  102. "|A|3|Skipped|Exit|Exit" +
  103. "|A|4|Skipped|Exit|Exit" +
  104. "|A|5|Success|Exit|Exit" +
  105. "|End"
  106. expectEqual(t, expected, myReporter.wholeStory())
  107. }
  108. func TestSkippedAssertionIsNotReported(t *testing.T) {
  109. myReporter, test := setupFakeReporter()
  110. Convey("A", test, func() {
  111. SkipSo(1, ShouldEqual, 1)
  112. })
  113. expectEqual(t, "Begin|A|Skipped|Exit|End", myReporter.wholeStory())
  114. }
  115. func TestMultipleSkippedAssertionsAreNotReported(t *testing.T) {
  116. myReporter, test := setupFakeReporter()
  117. Convey("A", test, func() {
  118. SkipSo(1, ShouldEqual, 1)
  119. So(1, ShouldEqual, 1)
  120. SkipSo(1, ShouldEqual, 1)
  121. })
  122. expectEqual(t, "Begin|A|Skipped|Success|Skipped|Exit|End", myReporter.wholeStory())
  123. }
  124. func TestErrorByManualPanicReported(t *testing.T) {
  125. myReporter, test := setupFakeReporter()
  126. Convey("A", test, func() {
  127. panic("Gopher alert!")
  128. })
  129. expectEqual(t, "Begin|A|Error|Exit|End", myReporter.wholeStory())
  130. }
  131. func TestIterativeConveysReported(t *testing.T) {
  132. myReporter, test := setupFakeReporter()
  133. Convey("A", test, func() {
  134. for x := 0; x < 3; x++ {
  135. Convey(strconv.Itoa(x), func() {
  136. So(x, ShouldEqual, x)
  137. })
  138. }
  139. })
  140. expectEqual(t, "Begin|A|0|Success|Exit|Exit|A|1|Success|Exit|Exit|A|2|Success|Exit|Exit|End", myReporter.wholeStory())
  141. }
  142. func TestNestedIterativeConveysReported(t *testing.T) {
  143. myReporter, test := setupFakeReporter()
  144. Convey("A", test, func() {
  145. for x := 0; x < 3; x++ {
  146. Convey(strconv.Itoa(x), func() {
  147. for y := 0; y < 3; y++ {
  148. Convey("< "+strconv.Itoa(y), func() {
  149. So(x, ShouldBeLessThan, y)
  150. })
  151. }
  152. })
  153. }
  154. })
  155. expectEqual(t, ("Begin|" +
  156. "A|0|< 0|Failure|Exit|Exit|Exit|" +
  157. "A|0|< 1|Success|Exit|Exit|Exit|" +
  158. "A|0|< 2|Success|Exit|Exit|Exit|" +
  159. "A|1|< 0|Failure|Exit|Exit|Exit|" +
  160. "A|1|< 1|Failure|Exit|Exit|Exit|" +
  161. "A|1|< 2|Success|Exit|Exit|Exit|" +
  162. "A|2|< 0|Failure|Exit|Exit|Exit|" +
  163. "A|2|< 1|Failure|Exit|Exit|Exit|" +
  164. "A|2|< 2|Failure|Exit|Exit|Exit|" +
  165. "End"), myReporter.wholeStory())
  166. }
  167. func TestEmbeddedAssertionReported(t *testing.T) {
  168. myReporter, test := setupFakeReporter()
  169. Convey("A", test, func(c C) {
  170. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  171. c.So(r.FormValue("msg"), ShouldEqual, "ping")
  172. }))
  173. http.DefaultClient.Get(ts.URL + "?msg=ping")
  174. })
  175. expectEqual(t, "Begin|A|Success|Exit|End", myReporter.wholeStory())
  176. }
  177. func TestEmbeddedContextHelperReported(t *testing.T) {
  178. myReporter, test := setupFakeReporter()
  179. helper := func(c C) http.HandlerFunc {
  180. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  181. c.Convey("Embedded", func() {
  182. So(r.FormValue("msg"), ShouldEqual, "ping")
  183. })
  184. })
  185. }
  186. Convey("A", test, func(c C) {
  187. ts := httptest.NewServer(helper(c))
  188. http.DefaultClient.Get(ts.URL + "?msg=ping")
  189. })
  190. expectEqual(t, "Begin|A|Embedded|Success|Exit|Exit|End", myReporter.wholeStory())
  191. }
  192. func expectEqual(t *testing.T, expected interface{}, actual interface{}) {
  193. if expected != actual {
  194. _, file, line, _ := runtime.Caller(1)
  195. t.Errorf("Expected '%v' to be '%v' but it wasn't. See '%s' at line %d.",
  196. actual, expected, path.Base(file), line)
  197. }
  198. }
  199. func setupFakeReporter() (*fakeReporter, *fakeGoTest) {
  200. myReporter := new(fakeReporter)
  201. myReporter.calls = []string{}
  202. testReporter = myReporter
  203. return myReporter, new(fakeGoTest)
  204. }
  205. type fakeReporter struct {
  206. calls []string
  207. }
  208. func (self *fakeReporter) BeginStory(story *reporting.StoryReport) {
  209. self.calls = append(self.calls, "Begin")
  210. }
  211. func (self *fakeReporter) Enter(scope *reporting.ScopeReport) {
  212. self.calls = append(self.calls, scope.Title)
  213. }
  214. func (self *fakeReporter) Report(report *reporting.AssertionResult) {
  215. if report.Error != nil {
  216. self.calls = append(self.calls, "Error")
  217. } else if report.Failure != "" {
  218. message := "Failure"
  219. if report.Expected != "" || report.Actual != "" {
  220. message += fmt.Sprintf("(%s/%s)", report.Expected, report.Actual)
  221. }
  222. self.calls = append(self.calls, message)
  223. } else if report.Skipped {
  224. self.calls = append(self.calls, "Skipped")
  225. } else {
  226. self.calls = append(self.calls, "Success")
  227. }
  228. }
  229. func (self *fakeReporter) Exit() {
  230. self.calls = append(self.calls, "Exit")
  231. }
  232. func (self *fakeReporter) EndStory() {
  233. self.calls = append(self.calls, "End")
  234. }
  235. func (self *fakeReporter) Write(content []byte) (int, error) {
  236. return len(content), nil // no-op
  237. }
  238. func (self *fakeReporter) wholeStory() string {
  239. return strings.Join(self.calls, "|")
  240. }
  241. ////////////////////////////////
  242. type fakeGoTest struct{}
  243. func (self *fakeGoTest) Fail() {}
  244. func (self *fakeGoTest) Fatalf(format string, args ...interface{}) {}
  245. var test t = new(fakeGoTest)