collections.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package assertions
  2. import (
  3. "fmt"
  4. "reflect"
  5. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
  6. )
  7. // ShouldContain receives exactly two parameters. The first is a slice and the
  8. // second is a proposed member. Membership is determined using ShouldEqual.
  9. func ShouldContain(actual interface{}, expected ...interface{}) string {
  10. if fail := need(1, expected); fail != success {
  11. return fail
  12. }
  13. if matchError := oglematchers.Contains(expected[0]).Matches(actual); matchError != nil {
  14. typeName := reflect.TypeOf(actual)
  15. if fmt.Sprintf("%v", matchError) == "which is not a slice or array" {
  16. return fmt.Sprintf(shouldHaveBeenAValidCollection, typeName)
  17. }
  18. return fmt.Sprintf(shouldHaveContained, typeName, expected[0])
  19. }
  20. return success
  21. }
  22. // ShouldNotContain receives exactly two parameters. The first is a slice and the
  23. // second is a proposed member. Membership is determinied using ShouldEqual.
  24. func ShouldNotContain(actual interface{}, expected ...interface{}) string {
  25. if fail := need(1, expected); fail != success {
  26. return fail
  27. }
  28. typeName := reflect.TypeOf(actual)
  29. if matchError := oglematchers.Contains(expected[0]).Matches(actual); matchError != nil {
  30. if fmt.Sprintf("%v", matchError) == "which is not a slice or array" {
  31. return fmt.Sprintf(shouldHaveBeenAValidCollection, typeName)
  32. }
  33. return success
  34. }
  35. return fmt.Sprintf(shouldNotHaveContained, typeName, expected[0])
  36. }
  37. // ShouldBeIn receives at least 2 parameters. The first is a proposed member of the collection
  38. // that is passed in either as the second parameter, or of the collection that is comprised
  39. // of all the remaining parameters. This assertion ensures that the proposed member is in
  40. // the collection (using ShouldEqual).
  41. func ShouldBeIn(actual interface{}, expected ...interface{}) string {
  42. if fail := atLeast(1, expected); fail != success {
  43. return fail
  44. }
  45. if len(expected) == 1 {
  46. return shouldBeIn(actual, expected[0])
  47. }
  48. return shouldBeIn(actual, expected)
  49. }
  50. func shouldBeIn(actual interface{}, expected interface{}) string {
  51. if matchError := oglematchers.Contains(actual).Matches(expected); matchError != nil {
  52. return fmt.Sprintf(shouldHaveBeenIn, actual, reflect.TypeOf(expected))
  53. }
  54. return success
  55. }
  56. // ShouldNotBeIn receives at least 2 parameters. The first is a proposed member of the collection
  57. // that is passed in either as the second parameter, or of the collection that is comprised
  58. // of all the remaining parameters. This assertion ensures that the proposed member is NOT in
  59. // the collection (using ShouldEqual).
  60. func ShouldNotBeIn(actual interface{}, expected ...interface{}) string {
  61. if fail := atLeast(1, expected); fail != success {
  62. return fail
  63. }
  64. if len(expected) == 1 {
  65. return shouldNotBeIn(actual, expected[0])
  66. }
  67. return shouldNotBeIn(actual, expected)
  68. }
  69. func shouldNotBeIn(actual interface{}, expected interface{}) string {
  70. if matchError := oglematchers.Contains(actual).Matches(expected); matchError == nil {
  71. return fmt.Sprintf(shouldNotHaveBeenIn, actual, reflect.TypeOf(expected))
  72. }
  73. return success
  74. }
  75. // ShouldBeEmpty receives a single parameter (actual) and determines whether or not
  76. // calling len(actual) would return `0`. It obeys the rules specified by the len
  77. // function for determining length: http://golang.org/pkg/builtin/#len
  78. func ShouldBeEmpty(actual interface{}, expected ...interface{}) string {
  79. if fail := need(0, expected); fail != success {
  80. return fail
  81. }
  82. if actual == nil {
  83. return success
  84. }
  85. value := reflect.ValueOf(actual)
  86. switch value.Kind() {
  87. case reflect.Slice:
  88. if value.Len() == 0 {
  89. return success
  90. }
  91. case reflect.Chan:
  92. if value.Len() == 0 {
  93. return success
  94. }
  95. case reflect.Map:
  96. if value.Len() == 0 {
  97. return success
  98. }
  99. case reflect.String:
  100. if value.Len() == 0 {
  101. return success
  102. }
  103. case reflect.Ptr:
  104. elem := value.Elem()
  105. kind := elem.Kind()
  106. if (kind == reflect.Slice || kind == reflect.Array) && elem.Len() == 0 {
  107. return success
  108. }
  109. }
  110. return fmt.Sprintf(shouldHaveBeenEmpty, actual)
  111. }
  112. // ShouldNotBeEmpty receives a single parameter (actual) and determines whether or not
  113. // calling len(actual) would return a value greater than zero. It obeys the rules
  114. // specified by the `len` function for determining length: http://golang.org/pkg/builtin/#len
  115. func ShouldNotBeEmpty(actual interface{}, expected ...interface{}) string {
  116. if fail := need(0, expected); fail != success {
  117. return fail
  118. }
  119. if empty := ShouldBeEmpty(actual, expected...); empty != success {
  120. return success
  121. }
  122. return fmt.Sprintf(shouldNotHaveBeenEmpty, actual)
  123. }