assert.go 2.5 KB

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