doc.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Package convey contains all of the public-facing entry points to this project.
  2. // This means that it should never be required of the user to import any other
  3. // packages from this project as they serve internal purposes.
  4. package convey
  5. ////////////////////////////////// suite //////////////////////////////////
  6. // C is the Convey context which you can optionally obtain in your action
  7. // by calling Convey like:
  8. //
  9. // Convey(..., func(c C) {
  10. // ...
  11. // })
  12. //
  13. // See the documentation on Convey for more details.
  14. //
  15. // All methods in this context behave identically to the global functions of the
  16. // same name in this package.
  17. type C interface {
  18. Convey(items ...interface{})
  19. SkipConvey(items ...interface{})
  20. FocusConvey(items ...interface{})
  21. So(actual interface{}, assert assertion, expected ...interface{})
  22. SkipSo(stuff ...interface{})
  23. Reset(action func())
  24. Println(items ...interface{}) (int, error)
  25. Print(items ...interface{}) (int, error)
  26. Printf(format string, items ...interface{}) (int, error)
  27. }
  28. // Convey is the method intended for use when declaring the scopes of
  29. // a specification. Each scope has a description and a func() which may contain
  30. // other calls to Convey(), Reset() or Should-style assertions. Convey calls can
  31. // be nested as far as you see fit.
  32. //
  33. // IMPORTANT NOTE: The top-level Convey() within a Test method
  34. // must conform to the following signature:
  35. //
  36. // Convey(description string, t *testing.T, action func())
  37. //
  38. // All other calls should look like this (no need to pass in *testing.T):
  39. //
  40. // Convey(description string, action func())
  41. //
  42. // Don't worry, goconvey will panic if you get it wrong so you can fix it.
  43. //
  44. // Additionally, you may explicitly obtain access to the Convey context by doing:
  45. //
  46. // Convey(description string, action func(c C))
  47. //
  48. // You may need to do this if you want to pass the context through to a
  49. // goroutine, or to close over the context in a handler to a library which
  50. // calls your handler in a goroutine (httptest comes to mind).
  51. //
  52. // All Convey()-blocks also accept an optional parameter of FailureMode which sets
  53. // how goconvey should treat failures for So()-assertions in the block and
  54. // nested blocks. See the constants in this file for the available options.
  55. //
  56. // By default it will inherit from its parent block and the top-level blocks
  57. // default to the FailureHalts setting.
  58. //
  59. // This parameter is inserted before the block itself:
  60. //
  61. // Convey(description string, t *testing.T, mode FailureMode, action func())
  62. // Convey(description string, mode FailureMode, action func())
  63. //
  64. // See the examples package for, well, examples.
  65. func Convey(items ...interface{}) {
  66. if ctx := getCurrentContext(); ctx == nil {
  67. rootConvey(items...)
  68. } else {
  69. ctx.Convey(items...)
  70. }
  71. }
  72. // SkipConvey is analagous to Convey except that the scope is not executed
  73. // (which means that child scopes defined within this scope are not run either).
  74. // The reporter will be notified that this step was skipped.
  75. func SkipConvey(items ...interface{}) {
  76. Convey(append(items, skipConvey)...)
  77. }
  78. // FocusConvey is has the inverse effect of SkipConvey. If the top-level
  79. // Convey is changed to `FocusConvey`, only nested scopes that are defined
  80. // with FocusConvey will be run. The rest will be ignored completely. This
  81. // is handy when debugging a large suite that runs a misbehaving function
  82. // repeatedly as you can disable all but one of that function
  83. // without swaths of `SkipConvey` calls, just a targeted chain of calls
  84. // to FocusConvey.
  85. func FocusConvey(items ...interface{}) {
  86. Convey(append(items, focusConvey)...)
  87. }
  88. // Reset registers a cleanup function to be run after each Convey()
  89. // in the same scope. See the examples package for a simple use case.
  90. func Reset(action func()) {
  91. mustGetCurrentContext().Reset(action)
  92. }
  93. /////////////////////////////////// Assertions ///////////////////////////////////
  94. // assertion is an alias for a function with a signature that the convey.So()
  95. // method can handle. Any future or custom assertions should conform to this
  96. // method signature. The return value should be an empty string if the assertion
  97. // passes and a well-formed failure message if not.
  98. type assertion func(actual interface{}, expected ...interface{}) string
  99. const assertionSuccess = ""
  100. // So is the means by which assertions are made against the system under test.
  101. // The majority of exported names in the assertions package begin with the word
  102. // 'Should' and describe how the first argument (actual) should compare with any
  103. // of the final (expected) arguments. How many final arguments are accepted
  104. // depends on the particular assertion that is passed in as the assert argument.
  105. // See the examples package for use cases and the assertions package for
  106. // documentation on specific assertion methods. A failing assertion will
  107. // cause t.Fail() to be invoked--you should never call this method (or other
  108. // failure-inducing methods) in your test code. Leave that to GoConvey.
  109. func So(actual interface{}, assert assertion, expected ...interface{}) {
  110. mustGetCurrentContext().So(actual, assert, expected...)
  111. }
  112. // SkipSo is analagous to So except that the assertion that would have been passed
  113. // to So is not executed and the reporter is notified that the assertion was skipped.
  114. func SkipSo(stuff ...interface{}) {
  115. mustGetCurrentContext().SkipSo()
  116. }
  117. // FailureMode is a type which determines how the So() blocks should fail
  118. // if their assertion fails. See constants further down for acceptable values
  119. type FailureMode string
  120. const (
  121. // FailureContinues is a failure mode which prevents failing
  122. // So()-assertions from halting Convey-block execution, instead
  123. // allowing the test to continue past failing So()-assertions.
  124. FailureContinues FailureMode = "continue"
  125. // FailureHalts is the default setting for a top-level Convey()-block
  126. // and will cause all failing So()-assertions to halt further execution
  127. // in that test-arm and continue on to the next arm.
  128. FailureHalts FailureMode = "halt"
  129. // FailureInherits is the default setting for failure-mode, it will
  130. // default to the failure-mode of the parent block. You should never
  131. // need to specify this mode in your tests..
  132. FailureInherits FailureMode = "inherits"
  133. )
  134. func (f FailureMode) combine(other FailureMode) FailureMode {
  135. if other == FailureInherits {
  136. return f
  137. }
  138. return other
  139. }
  140. var defaultFailureMode FailureMode = FailureHalts
  141. // SetDefaultFailureMode allows you to specify the default failure mode
  142. // for all Convey blocks. It is meant to be used in an init function to
  143. // allow the default mode to be changdd across all tests for an entire packgae
  144. // but it can be used anywhere.
  145. func SetDefaultFailureMode(mode FailureMode) {
  146. if mode == FailureContinues || mode == FailureHalts {
  147. defaultFailureMode = mode
  148. } else {
  149. panic("You may only use the constants named 'FailureContinues' and 'FailureHalts' as default failure modes.")
  150. }
  151. }
  152. //////////////////////////////////// Print functions ////////////////////////////////////
  153. // Print is analogous to fmt.Print (and it even calls fmt.Print). It ensures that
  154. // output is aligned with the corresponding scopes in the web UI.
  155. func Print(items ...interface{}) (written int, err error) {
  156. return mustGetCurrentContext().Print(items...)
  157. }
  158. // Print is analogous to fmt.Println (and it even calls fmt.Println). It ensures that
  159. // output is aligned with the corresponding scopes in the web UI.
  160. func Println(items ...interface{}) (written int, err error) {
  161. return mustGetCurrentContext().Println(items...)
  162. }
  163. // Print is analogous to fmt.Printf (and it even calls fmt.Printf). It ensures that
  164. // output is aligned with the corresponding scopes in the web UI.
  165. func Printf(format string, items ...interface{}) (written int, err error) {
  166. return mustGetCurrentContext().Printf(format, items...)
  167. }