type.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package assertions
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. // ShouldHaveSameTypeAs receives exactly two parameters and compares their underlying types for equality.
  7. func ShouldHaveSameTypeAs(actual interface{}, expected ...interface{}) string {
  8. if fail := need(1, expected); fail != success {
  9. return fail
  10. }
  11. first := reflect.TypeOf(actual)
  12. second := reflect.TypeOf(expected[0])
  13. if equal := ShouldEqual(first, second); equal != success {
  14. return serializer.serialize(second, first, fmt.Sprintf(shouldHaveBeenA, actual, second, first))
  15. }
  16. return success
  17. }
  18. // ShouldNotHaveSameTypeAs receives exactly two parameters and compares their underlying types for inequality.
  19. func ShouldNotHaveSameTypeAs(actual interface{}, expected ...interface{}) string {
  20. if fail := need(1, expected); fail != success {
  21. return fail
  22. }
  23. first := reflect.TypeOf(actual)
  24. second := reflect.TypeOf(expected[0])
  25. if equal := ShouldEqual(first, second); equal == success {
  26. return fmt.Sprintf(shouldNotHaveBeenA, actual, second)
  27. }
  28. return success
  29. }
  30. // ShouldImplement receives exactly two parameters and ensures
  31. // that the first implements the interface type of the second.
  32. func ShouldImplement(actual interface{}, expectedList ...interface{}) string {
  33. if fail := need(1, expectedList); fail != success {
  34. return fail
  35. }
  36. expected := expectedList[0]
  37. if fail := ShouldBeNil(expected); fail != success {
  38. return shouldCompareWithInterfacePointer
  39. }
  40. if fail := ShouldNotBeNil(actual); fail != success {
  41. return shouldNotBeNilActual
  42. }
  43. var actualType reflect.Type
  44. if reflect.TypeOf(actual).Kind() != reflect.Ptr {
  45. actualType = reflect.PtrTo(reflect.TypeOf(actual))
  46. } else {
  47. actualType = reflect.TypeOf(actual)
  48. }
  49. expectedType := reflect.TypeOf(expected)
  50. if fail := ShouldNotBeNil(expectedType); fail != success {
  51. return shouldCompareWithInterfacePointer
  52. }
  53. expectedInterface := expectedType.Elem()
  54. if actualType == nil {
  55. return fmt.Sprintf(shouldHaveImplemented, expectedInterface, actual)
  56. }
  57. if !actualType.Implements(expectedInterface) {
  58. return fmt.Sprintf(shouldHaveImplemented, expectedInterface, actualType)
  59. }
  60. return success
  61. }
  62. // ShouldNotImplement receives exactly two parameters and ensures
  63. // that the first does NOT implement the interface type of the second.
  64. func ShouldNotImplement(actual interface{}, expectedList ...interface{}) string {
  65. if fail := need(1, expectedList); fail != success {
  66. return fail
  67. }
  68. expected := expectedList[0]
  69. if fail := ShouldBeNil(expected); fail != success {
  70. return shouldCompareWithInterfacePointer
  71. }
  72. if fail := ShouldNotBeNil(actual); fail != success {
  73. return shouldNotBeNilActual
  74. }
  75. var actualType reflect.Type
  76. if reflect.TypeOf(actual).Kind() != reflect.Ptr {
  77. actualType = reflect.PtrTo(reflect.TypeOf(actual))
  78. } else {
  79. actualType = reflect.TypeOf(actual)
  80. }
  81. expectedType := reflect.TypeOf(expected)
  82. if fail := ShouldNotBeNil(expectedType); fail != success {
  83. return shouldCompareWithInterfacePointer
  84. }
  85. expectedInterface := expectedType.Elem()
  86. if actualType.Implements(expectedInterface) {
  87. return fmt.Sprintf(shouldNotHaveImplemented, actualType, expectedInterface)
  88. }
  89. return success
  90. }