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.

74 lines
1.9 KiB

  1. package common
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestStringInSlice(t *testing.T) {
  7. assert.True(t, StringInSlice("a", []string{"a", "b", "c"}))
  8. assert.False(t, StringInSlice("d", []string{"a", "b", "c"}))
  9. assert.True(t, StringInSlice("", []string{""}))
  10. assert.False(t, StringInSlice("", []string{}))
  11. }
  12. func TestIsHex(t *testing.T) {
  13. notHex := []string{
  14. "", " ", "a", "x", "0", "0x", "0X", "0x ", "0X ", "0X a",
  15. "0xf ", "0x f", "0xp", "0x-",
  16. "0xf", "0XBED", "0xF", "0xbed", // Odd lengths
  17. }
  18. for _, v := range notHex {
  19. assert.False(t, IsHex(v), "%q is not hex", v)
  20. }
  21. hex := []string{
  22. "0x00", "0x0a", "0x0F", "0xFFFFFF", "0Xdeadbeef", "0x0BED",
  23. "0X12", "0X0A",
  24. }
  25. for _, v := range hex {
  26. assert.True(t, IsHex(v), "%q is hex", v)
  27. }
  28. }
  29. func TestSplitAndTrim(t *testing.T) {
  30. testCases := []struct {
  31. s string
  32. sep string
  33. cutset string
  34. expected []string
  35. }{
  36. {"a,b,c", ",", " ", []string{"a", "b", "c"}},
  37. {" a , b , c ", ",", " ", []string{"a", "b", "c"}},
  38. {" a, b, c ", ",", " ", []string{"a", "b", "c"}},
  39. {" , ", ",", " ", []string{"", ""}},
  40. {" ", ",", " ", []string{""}},
  41. }
  42. for _, tc := range testCases {
  43. assert.Equal(t, tc.expected, SplitAndTrim(tc.s, tc.sep, tc.cutset), "%s", tc.s)
  44. }
  45. }
  46. func TestIsASCIIText(t *testing.T) {
  47. notASCIIText := []string{
  48. "", "\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t",
  49. }
  50. for _, v := range notASCIIText {
  51. assert.False(t, IsHex(v), "%q is not ascii-text", v)
  52. }
  53. asciiText := []string{
  54. " ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123",
  55. }
  56. for _, v := range asciiText {
  57. assert.True(t, IsASCIIText(v), "%q is ascii-text", v)
  58. }
  59. }
  60. func TestASCIITrim(t *testing.T) {
  61. assert.Equal(t, ASCIITrim(" "), "")
  62. assert.Equal(t, ASCIITrim(" a"), "a")
  63. assert.Equal(t, ASCIITrim("a "), "a")
  64. assert.Equal(t, ASCIITrim(" a "), "a")
  65. assert.Panics(t, func() { ASCIITrim("\xC2\xA2") })
  66. }