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.

58 lines
1.6 KiB

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