assert.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Package assert contains functions for making assertions in unit tests
  2. package assert
  3. import (
  4. "fmt"
  5. "path/filepath"
  6. "reflect"
  7. "runtime"
  8. "strings"
  9. "github.com/davecgh/go-spew/spew"
  10. )
  11. // TestingT is an interface which defines the methods of testing.T that are
  12. // required by this package
  13. type TestingT interface {
  14. Fatalf(string, ...interface{})
  15. }
  16. // Equal compare the actual value to the expected value and fails the test if
  17. // they are not equal.
  18. func Equal(t TestingT, actual, expected interface{}) {
  19. if expected != actual {
  20. fatal(t, "Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual)
  21. }
  22. }
  23. //EqualStringSlice compares two slices and fails the test if they do not contain
  24. // the same items.
  25. func EqualStringSlice(t TestingT, actual, expected []string) {
  26. if len(actual) != len(expected) {
  27. fatal(t, "Expected (length %d): %q\nActual (length %d): %q",
  28. len(expected), expected, len(actual), actual)
  29. }
  30. for i, item := range actual {
  31. if item != expected[i] {
  32. fatal(t, "Slices differ at element %d, expected %q got %q",
  33. i, expected[i], item)
  34. }
  35. }
  36. }
  37. // NilError asserts that the error is nil, otherwise it fails the test.
  38. func NilError(t TestingT, err error) {
  39. if err != nil {
  40. fatal(t, "Expected no error, got: %s", err.Error())
  41. }
  42. }
  43. // DeepEqual compare the actual value to the expected value and fails the test if
  44. // they are not "deeply equal".
  45. func DeepEqual(t TestingT, actual, expected interface{}) {
  46. if !reflect.DeepEqual(actual, expected) {
  47. fatal(t, "Expected (%T):\n%v\n\ngot (%T):\n%s\n",
  48. expected, spew.Sdump(expected), actual, spew.Sdump(actual))
  49. }
  50. }
  51. // Error asserts that error is not nil, and contains the expected text,
  52. // otherwise it fails the test.
  53. func Error(t TestingT, err error, contains string) {
  54. if err == nil {
  55. fatal(t, "Expected an error, but error was nil")
  56. }
  57. if !strings.Contains(err.Error(), contains) {
  58. fatal(t, "Expected error to contain '%s', got '%s'", contains, err.Error())
  59. }
  60. }
  61. // Contains asserts that the string contains a substring, otherwise it fails the
  62. // test.
  63. func Contains(t TestingT, actual, contains string) {
  64. if !strings.Contains(actual, contains) {
  65. fatal(t, "Expected '%s' to contain '%s'", actual, contains)
  66. }
  67. }
  68. // NotNil fails the test if the object is nil
  69. func NotNil(t TestingT, obj interface{}) {
  70. if obj == nil {
  71. fatal(t, "Expected non-nil value.")
  72. }
  73. }
  74. func fatal(t TestingT, format string, args ...interface{}) {
  75. t.Fatalf(errorSource()+format, args...)
  76. }
  77. // See testing.decorate()
  78. func errorSource() string {
  79. _, filename, line, ok := runtime.Caller(3)
  80. if !ok {
  81. return ""
  82. }
  83. return fmt.Sprintf("%s:%d: ", filepath.Base(filename), line)
  84. }