assert.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "unicode"
  10. "github.com/davecgh/go-spew/spew"
  11. )
  12. // TestingT is an interface which defines the methods of testing.T that are
  13. // required by this package
  14. type TestingT interface {
  15. Fatalf(string, ...interface{})
  16. }
  17. // Equal compare the actual value to the expected value and fails the test if
  18. // they are not equal.
  19. func Equal(t TestingT, actual, expected interface{}, extra ...string) {
  20. if expected != actual {
  21. fatalWithExtra(t, extra, "Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual)
  22. }
  23. }
  24. // EqualNormalizedString compare the actual value to the expected value after applying the specified
  25. // transform function. It fails the test if these two transformed string are not equal.
  26. // For example `EqualNormalizedString(t, RemoveSpace, "foo\n", "foo")` wouldn't fail the test as
  27. // spaces (and thus '\n') are removed before comparing the string.
  28. func EqualNormalizedString(t TestingT, transformFun func(rune) rune, actual, expected string) {
  29. if strings.Map(transformFun, actual) != strings.Map(transformFun, expected) {
  30. fatal(t, "Expected '%v' got '%v'", expected, expected, actual, actual)
  31. }
  32. }
  33. // RemoveSpace returns -1 if the specified runes is considered as a space (unicode)
  34. // and the rune itself otherwise.
  35. func RemoveSpace(r rune) rune {
  36. if unicode.IsSpace(r) {
  37. return -1
  38. }
  39. return r
  40. }
  41. //EqualStringSlice compares two slices and fails the test if they do not contain
  42. // the same items.
  43. func EqualStringSlice(t TestingT, actual, expected []string) {
  44. if len(actual) != len(expected) {
  45. fatal(t, "Expected (length %d): %q\nActual (length %d): %q",
  46. len(expected), expected, len(actual), actual)
  47. }
  48. for i, item := range actual {
  49. if item != expected[i] {
  50. fatal(t, "Slices differ at element %d, expected %q got %q",
  51. i, expected[i], item)
  52. }
  53. }
  54. }
  55. // NilError asserts that the error is nil, otherwise it fails the test.
  56. func NilError(t TestingT, err error) {
  57. if err != nil {
  58. fatal(t, "Expected no error, got: %s", err.Error())
  59. }
  60. }
  61. // DeepEqual compare the actual value to the expected value and fails the test if
  62. // they are not "deeply equal".
  63. func DeepEqual(t TestingT, actual, expected interface{}) {
  64. if !reflect.DeepEqual(actual, expected) {
  65. fatal(t, "Expected (%T):\n%v\n\ngot (%T):\n%s\n",
  66. expected, spew.Sdump(expected), actual, spew.Sdump(actual))
  67. }
  68. }
  69. // Error asserts that error is not nil, and contains the expected text,
  70. // otherwise it fails the test.
  71. func Error(t TestingT, err error, contains string) {
  72. if err == nil {
  73. fatal(t, "Expected an error, but error was nil")
  74. }
  75. if !strings.Contains(err.Error(), contains) {
  76. fatal(t, "Expected error to contain '%s', got '%s'", contains, err.Error())
  77. }
  78. }
  79. // Contains asserts that the string contains a substring, otherwise it fails the
  80. // test.
  81. func Contains(t TestingT, actual, contains string) {
  82. if !strings.Contains(actual, contains) {
  83. fatal(t, "Expected '%s' to contain '%s'", actual, contains)
  84. }
  85. }
  86. // NotNil fails the test if the object is nil
  87. func NotNil(t TestingT, obj interface{}) {
  88. if obj == nil {
  89. fatal(t, "Expected non-nil value.")
  90. }
  91. }
  92. // Nil fails the test if the object is not nil
  93. func Nil(t TestingT, obj interface{}) {
  94. if obj != nil {
  95. fatal(t, "Expected nil value, got (%T) %s", obj, obj)
  96. }
  97. }
  98. func fatal(t TestingT, format string, args ...interface{}) {
  99. t.Fatalf(errorSource()+format, args...)
  100. }
  101. func fatalWithExtra(t TestingT, extra []string, format string, args ...interface{}) {
  102. msg := fmt.Sprintf(errorSource()+format, args...)
  103. if len(extra) > 0 {
  104. msg += ": " + strings.Join(extra, ", ")
  105. }
  106. t.Fatalf(msg)
  107. }
  108. // See testing.decorate()
  109. func errorSource() string {
  110. _, filename, line, ok := runtime.Caller(3)
  111. if !ok {
  112. return ""
  113. }
  114. return fmt.Sprintf("%s:%d: ", filepath.Base(filename), line)
  115. }