You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.1 KiB

  1. package strings
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestSplitAndTrimEmpty(t *testing.T) {
  7. testCases := []struct {
  8. s string
  9. sep string
  10. cutset string
  11. expected []string
  12. }{
  13. {"a,b,c", ",", " ", []string{"a", "b", "c"}},
  14. {" a , b , c ", ",", " ", []string{"a", "b", "c"}},
  15. {" a, b, c ", ",", " ", []string{"a", "b", "c"}},
  16. {" a, ", ",", " ", []string{"a"}},
  17. {" ", ",", " ", []string{}},
  18. }
  19. for _, tc := range testCases {
  20. require.Equal(t, tc.expected, SplitAndTrimEmpty(tc.s, tc.sep, tc.cutset), "%s", tc.s)
  21. }
  22. }
  23. func TestStringInSlice(t *testing.T) {
  24. require.True(t, StringInSlice("a", []string{"a", "b", "c"}))
  25. require.False(t, StringInSlice("d", []string{"a", "b", "c"}))
  26. require.True(t, StringInSlice("", []string{""}))
  27. require.False(t, StringInSlice("", []string{}))
  28. }
  29. func TestIsASCIIText(t *testing.T) {
  30. notASCIIText := []string{
  31. "", "\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t",
  32. }
  33. for _, v := range notASCIIText {
  34. require.False(t, IsASCIIText(v), "%q is not ascii-text", v)
  35. }
  36. asciiText := []string{
  37. " ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123",
  38. }
  39. for _, v := range asciiText {
  40. require.True(t, IsASCIIText(v), "%q is ascii-text", v)
  41. }
  42. }
  43. func TestASCIITrim(t *testing.T) {
  44. require.Equal(t, ASCIITrim(" "), "")
  45. require.Equal(t, ASCIITrim(" a"), "a")
  46. require.Equal(t, ASCIITrim("a "), "a")
  47. require.Equal(t, ASCIITrim(" a "), "a")
  48. require.Panics(t, func() { ASCIITrim("\xC2\xA2") })
  49. }
  50. func TestStringSliceEqual(t *testing.T) {
  51. tests := []struct {
  52. a []string
  53. b []string
  54. want bool
  55. }{
  56. {[]string{"hello", "world"}, []string{"hello", "world"}, true},
  57. {[]string{"test"}, []string{"test"}, true},
  58. {[]string{"test1"}, []string{"test2"}, false},
  59. {[]string{"hello", "world."}, []string{"hello", "world!"}, false},
  60. {[]string{"only 1 word"}, []string{"two", "words!"}, false},
  61. {[]string{"two", "words!"}, []string{"only 1 word"}, false},
  62. }
  63. for i, tt := range tests {
  64. require.Equal(t, tt.want, StringSliceEqual(tt.a, tt.b),
  65. "StringSliceEqual failed on test %d", i)
  66. }
  67. }