strings.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package assertions
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. )
  7. // ShouldStartWith receives exactly 2 string parameters and ensures that the first starts with the second.
  8. func ShouldStartWith(actual interface{}, expected ...interface{}) string {
  9. if fail := need(1, expected); fail != success {
  10. return fail
  11. }
  12. value, valueIsString := actual.(string)
  13. prefix, prefixIsString := expected[0].(string)
  14. if !valueIsString || !prefixIsString {
  15. return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
  16. }
  17. return shouldStartWith(value, prefix)
  18. }
  19. func shouldStartWith(value, prefix string) string {
  20. if !strings.HasPrefix(value, prefix) {
  21. shortval := value
  22. if len(shortval) > len(prefix) {
  23. shortval = shortval[:len(prefix)] + "..."
  24. }
  25. return serializer.serialize(prefix, shortval, fmt.Sprintf(shouldHaveStartedWith, value, prefix))
  26. }
  27. return success
  28. }
  29. // ShouldNotStartWith receives exactly 2 string parameters and ensures that the first does not start with the second.
  30. func ShouldNotStartWith(actual interface{}, expected ...interface{}) string {
  31. if fail := need(1, expected); fail != success {
  32. return fail
  33. }
  34. value, valueIsString := actual.(string)
  35. prefix, prefixIsString := expected[0].(string)
  36. if !valueIsString || !prefixIsString {
  37. return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
  38. }
  39. return shouldNotStartWith(value, prefix)
  40. }
  41. func shouldNotStartWith(value, prefix string) string {
  42. if strings.HasPrefix(value, prefix) {
  43. if value == "" {
  44. value = "<empty>"
  45. }
  46. if prefix == "" {
  47. prefix = "<empty>"
  48. }
  49. return fmt.Sprintf(shouldNotHaveStartedWith, value, prefix)
  50. }
  51. return success
  52. }
  53. // ShouldEndWith receives exactly 2 string parameters and ensures that the first ends with the second.
  54. func ShouldEndWith(actual interface{}, expected ...interface{}) string {
  55. if fail := need(1, expected); fail != success {
  56. return fail
  57. }
  58. value, valueIsString := actual.(string)
  59. suffix, suffixIsString := expected[0].(string)
  60. if !valueIsString || !suffixIsString {
  61. return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
  62. }
  63. return shouldEndWith(value, suffix)
  64. }
  65. func shouldEndWith(value, suffix string) string {
  66. if !strings.HasSuffix(value, suffix) {
  67. shortval := value
  68. if len(shortval) > len(suffix) {
  69. shortval = "..." + shortval[len(shortval)-len(suffix):]
  70. }
  71. return serializer.serialize(suffix, shortval, fmt.Sprintf(shouldHaveEndedWith, value, suffix))
  72. }
  73. return success
  74. }
  75. // ShouldEndWith receives exactly 2 string parameters and ensures that the first does not end with the second.
  76. func ShouldNotEndWith(actual interface{}, expected ...interface{}) string {
  77. if fail := need(1, expected); fail != success {
  78. return fail
  79. }
  80. value, valueIsString := actual.(string)
  81. suffix, suffixIsString := expected[0].(string)
  82. if !valueIsString || !suffixIsString {
  83. return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
  84. }
  85. return shouldNotEndWith(value, suffix)
  86. }
  87. func shouldNotEndWith(value, suffix string) string {
  88. if strings.HasSuffix(value, suffix) {
  89. if value == "" {
  90. value = "<empty>"
  91. }
  92. if suffix == "" {
  93. suffix = "<empty>"
  94. }
  95. return fmt.Sprintf(shouldNotHaveEndedWith, value, suffix)
  96. }
  97. return success
  98. }
  99. // ShouldContainSubstring receives exactly 2 string parameters and ensures that the first contains the second as a substring.
  100. func ShouldContainSubstring(actual interface{}, expected ...interface{}) string {
  101. if fail := need(1, expected); fail != success {
  102. return fail
  103. }
  104. long, longOk := actual.(string)
  105. short, shortOk := expected[0].(string)
  106. if !longOk || !shortOk {
  107. return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
  108. }
  109. if !strings.Contains(long, short) {
  110. return serializer.serialize(expected[0], actual, fmt.Sprintf(shouldHaveContainedSubstring, long, short))
  111. }
  112. return success
  113. }
  114. // ShouldNotContainSubstring receives exactly 2 string parameters and ensures that the first does NOT contain the second as a substring.
  115. func ShouldNotContainSubstring(actual interface{}, expected ...interface{}) string {
  116. if fail := need(1, expected); fail != success {
  117. return fail
  118. }
  119. long, longOk := actual.(string)
  120. short, shortOk := expected[0].(string)
  121. if !longOk || !shortOk {
  122. return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
  123. }
  124. if strings.Contains(long, short) {
  125. return fmt.Sprintf(shouldNotHaveContainedSubstring, long, short)
  126. }
  127. return success
  128. }
  129. // ShouldBeBlank receives exactly 1 string parameter and ensures that it is equal to "".
  130. func ShouldBeBlank(actual interface{}, expected ...interface{}) string {
  131. if fail := need(0, expected); fail != success {
  132. return fail
  133. }
  134. value, ok := actual.(string)
  135. if !ok {
  136. return fmt.Sprintf(shouldBeString, reflect.TypeOf(actual))
  137. }
  138. if value != "" {
  139. return serializer.serialize("", value, fmt.Sprintf(shouldHaveBeenBlank, value))
  140. }
  141. return success
  142. }
  143. // ShouldNotBeBlank receives exactly 1 string parameter and ensures that it is equal to "".
  144. func ShouldNotBeBlank(actual interface{}, expected ...interface{}) string {
  145. if fail := need(0, expected); fail != success {
  146. return fail
  147. }
  148. value, ok := actual.(string)
  149. if !ok {
  150. return fmt.Sprintf(shouldBeString, reflect.TypeOf(actual))
  151. }
  152. if value == "" {
  153. return shouldNotHaveBeenBlank
  154. }
  155. return success
  156. }
  157. // ShouldEqualWithout receives exactly 3 string parameters and ensures that the first is equal to the second
  158. // after removing all instances of the third from the first using strings.Replace(first, third, "", -1).
  159. func ShouldEqualWithout(actual interface{}, expected ...interface{}) string {
  160. if fail := need(2, expected); fail != success {
  161. return fail
  162. }
  163. actualString, ok1 := actual.(string)
  164. expectedString, ok2 := expected[0].(string)
  165. replace, ok3 := expected[1].(string)
  166. if !ok1 || !ok2 || !ok3 {
  167. return fmt.Sprintf(shouldAllBeStrings, []reflect.Type{
  168. reflect.TypeOf(actual),
  169. reflect.TypeOf(expected[0]),
  170. reflect.TypeOf(expected[1]),
  171. })
  172. }
  173. replaced := strings.Replace(actualString, replace, "", -1)
  174. if replaced == expectedString {
  175. return ""
  176. }
  177. return fmt.Sprintf("Expected '%s' to equal '%s' but without any '%s' (but it didn't).", actualString, expectedString, replace)
  178. }
  179. // ShouldEqualTrimSpace receives exactly 2 string parameters and ensures that the first is equal to the second
  180. // after removing all leading and trailing whitespace using strings.TrimSpace(first).
  181. func ShouldEqualTrimSpace(actual interface{}, expected ...interface{}) string {
  182. if fail := need(1, expected); fail != success {
  183. return fail
  184. }
  185. actualString, valueIsString := actual.(string)
  186. _, value2IsString := expected[0].(string)
  187. if !valueIsString || !value2IsString {
  188. return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
  189. }
  190. actualString = strings.TrimSpace(actualString)
  191. return ShouldEqual(actualString, expected[0])
  192. }