helpers.go 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. package testutil
  2. import (
  3. "strings"
  4. "unicode"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. // ErrorContains checks that the error is not nil, and contains the expected
  9. // substring.
  10. func ErrorContains(t require.TestingT, err error, expectedError string) {
  11. require.Error(t, err)
  12. assert.Contains(t, err.Error(), expectedError)
  13. }
  14. // EqualNormalizedString compare the actual value to the expected value after applying the specified
  15. // transform function. It fails the test if these two transformed string are not equal.
  16. // For example `EqualNormalizedString(t, RemoveSpace, "foo\n", "foo")` wouldn't fail the test as
  17. // spaces (and thus '\n') are removed before comparing the string.
  18. func EqualNormalizedString(t require.TestingT, transformFun func(rune) rune, actual, expected string) {
  19. require.Equal(t, strings.Map(transformFun, expected), strings.Map(transformFun, actual))
  20. }
  21. // RemoveSpace returns -1 if the specified runes is considered as a space (unicode)
  22. // and the rune itself otherwise.
  23. func RemoveSpace(r rune) rune {
  24. if unicode.IsSpace(r) {
  25. return -1
  26. }
  27. return r
  28. }