doc.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Package assertions contains the implementations for all assertions which
  2. // are referenced in the convey package for use with the So(...) method.
  3. package assertions
  4. // This function is not used by the goconvey library. It's actually a convenience method
  5. // for running assertions on arbitrary arguments outside of any testing context, like for
  6. // application logging. It allows you to perform assertion-like behavior (and get nicely
  7. // formatted messages detailing discrepancies) but without the probram blowing up or panicking.
  8. // All that is required is to import this package and call `So` with one of the assertions
  9. // exported by this package as the second parameter.
  10. // The first return parameter is a boolean indicating if the assertion was true. The second
  11. // return parameter is the well-formatted message showing why an assertion was incorrect, or
  12. // blank if the assertion was correct.
  13. //
  14. // Example:
  15. //
  16. // if ok, message := So(x, ShouldBeGreaterThan, y); !ok {
  17. // log.Println(message)
  18. // }
  19. //
  20. func So(actual interface{}, assert assertion, expected ...interface{}) (bool, string) {
  21. serializer = noop
  22. if result := so(actual, assert, expected...); len(result) == 0 {
  23. return true, result
  24. } else {
  25. return false, result
  26. }
  27. }
  28. // so is like So, except that it only returns the string message, which is blank if the
  29. // assertion passed. Used to facilitate testing.
  30. func so(actual interface{}, assert func(interface{}, ...interface{}) string, expected ...interface{}) string {
  31. return assert(actual, expected...)
  32. }
  33. // assertion is an alias for a function with a signature that the So()
  34. // function can handle. Any future or custom assertions should conform to this
  35. // method signature. The return value should be an empty string if the assertion
  36. // passes and a well-formed failure message if not.
  37. type assertion func(actual interface{}, expected ...interface{}) string
  38. ////////////////////////////////////////////////////////////////////////////