doc.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Package assertions contains the implementations for all assertions which
  2. // are referenced in goconvey's `convey` package
  3. // (github.com/smartystreets/goconvey/convey) and gunit (github.com/smartystreets/gunit)
  4. // for use with the So(...) method.
  5. // They can also be used in traditional Go test functions and even in
  6. // applications.
  7. //
  8. // Many of the assertions lean heavily on work done by Aaron Jacobs in his excellent oglematchers library.
  9. // (https://github.com/jacobsa/oglematchers)
  10. // The ShouldResemble assertion leans heavily on work done by Daniel Jacques in his very helpful go-render library.
  11. // (https://github.com/luci/go-render)
  12. package assertions
  13. import (
  14. "fmt"
  15. "runtime"
  16. )
  17. // By default we use a no-op serializer. The actual Serializer provides a JSON
  18. // representation of failure results on selected assertions so the goconvey
  19. // web UI can display a convenient diff.
  20. var serializer Serializer = new(noopSerializer)
  21. // GoConveyMode provides control over JSON serialization of failures. When
  22. // using the assertions in this package from the convey package JSON results
  23. // are very helpful and can be rendered in a DIFF view. In that case, this function
  24. // will be called with a true value to enable the JSON serialization. By default,
  25. // the assertions in this package will not serializer a JSON result, making
  26. // standalone ussage more convenient.
  27. func GoConveyMode(yes bool) {
  28. if yes {
  29. serializer = newSerializer()
  30. } else {
  31. serializer = new(noopSerializer)
  32. }
  33. }
  34. type testingT interface {
  35. Error(args ...interface{})
  36. }
  37. type Assertion struct {
  38. t testingT
  39. failed bool
  40. }
  41. // New swallows the *testing.T struct and prints failed assertions using t.Error.
  42. // Example: assertions.New(t).So(1, should.Equal, 1)
  43. func New(t testingT) *Assertion {
  44. return &Assertion{t: t}
  45. }
  46. // Failed reports whether any calls to So (on this Assertion instance) have failed.
  47. func (this *Assertion) Failed() bool {
  48. return this.failed
  49. }
  50. // So calls the standalone So function and additionally, calls t.Error in failure scenarios.
  51. func (this *Assertion) So(actual interface{}, assert assertion, expected ...interface{}) bool {
  52. ok, result := So(actual, assert, expected...)
  53. if !ok {
  54. this.failed = true
  55. _, file, line, _ := runtime.Caller(1)
  56. this.t.Error(fmt.Sprintf("\n%s:%d\n%s", file, line, result))
  57. }
  58. return ok
  59. }
  60. // So is a convenience function (as opposed to an inconvenience function?)
  61. // for running assertions on arbitrary arguments in any context, be it for testing or even
  62. // application logging. It allows you to perform assertion-like behavior (and get nicely
  63. // formatted messages detailing discrepancies) but without the program blowing up or panicking.
  64. // All that is required is to import this package and call `So` with one of the assertions
  65. // exported by this package as the second parameter.
  66. // The first return parameter is a boolean indicating if the assertion was true. The second
  67. // return parameter is the well-formatted message showing why an assertion was incorrect, or
  68. // blank if the assertion was correct.
  69. //
  70. // Example:
  71. //
  72. // if ok, message := So(x, ShouldBeGreaterThan, y); !ok {
  73. // log.Println(message)
  74. // }
  75. //
  76. func So(actual interface{}, assert assertion, expected ...interface{}) (bool, string) {
  77. if result := so(actual, assert, expected...); len(result) == 0 {
  78. return true, result
  79. } else {
  80. return false, result
  81. }
  82. }
  83. // so is like So, except that it only returns the string message, which is blank if the
  84. // assertion passed. Used to facilitate testing.
  85. func so(actual interface{}, assert func(interface{}, ...interface{}) string, expected ...interface{}) string {
  86. return assert(actual, expected...)
  87. }
  88. // assertion is an alias for a function with a signature that the So()
  89. // function can handle. Any future or custom assertions should conform to this
  90. // method signature. The return value should be an empty string if the assertion
  91. // passes and a well-formed failure message if not.
  92. type assertion func(actual interface{}, expected ...interface{}) string
  93. ////////////////////////////////////////////////////////////////////////////