stringutils_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package stringutils
  2. import "testing"
  3. func testLengthHelper(generator func(int) string, t *testing.T) {
  4. expectedLength := 20
  5. s := generator(expectedLength)
  6. if len(s) != expectedLength {
  7. t.Fatalf("Length of %s was %d but expected length %d", s, len(s), expectedLength)
  8. }
  9. }
  10. func testUniquenessHelper(generator func(int) string, t *testing.T) {
  11. repeats := 25
  12. set := make(map[string]struct{}, repeats)
  13. for i := 0; i < repeats; i = i + 1 {
  14. str := generator(64)
  15. if len(str) != 64 {
  16. t.Fatalf("Id returned is incorrect: %s", str)
  17. }
  18. if _, ok := set[str]; ok {
  19. t.Fatalf("Random number is repeated")
  20. }
  21. set[str] = struct{}{}
  22. }
  23. }
  24. func isASCII(s string) bool {
  25. for _, c := range s {
  26. if c > 127 {
  27. return false
  28. }
  29. }
  30. return true
  31. }
  32. func TestGenerateRandomAsciiStringLength(t *testing.T) {
  33. testLengthHelper(GenerateRandomASCIIString, t)
  34. }
  35. func TestGenerateRandomAsciiStringUniqueness(t *testing.T) {
  36. testUniquenessHelper(GenerateRandomASCIIString, t)
  37. }
  38. func TestGenerateRandomAsciiStringIsAscii(t *testing.T) {
  39. str := GenerateRandomASCIIString(64)
  40. if !isASCII(str) {
  41. t.Fatalf("%s contained non-ascii characters", str)
  42. }
  43. }
  44. func TestEllipsis(t *testing.T) {
  45. str := "t🐳ststring"
  46. newstr := Ellipsis(str, 3)
  47. if newstr != "t🐳s" {
  48. t.Fatalf("Expected t🐳s, got %s", newstr)
  49. }
  50. newstr = Ellipsis(str, 8)
  51. if newstr != "t🐳sts..." {
  52. t.Fatalf("Expected tests..., got %s", newstr)
  53. }
  54. newstr = Ellipsis(str, 20)
  55. if newstr != "t🐳ststring" {
  56. t.Fatalf("Expected t🐳ststring, got %s", newstr)
  57. }
  58. }
  59. func TestTruncate(t *testing.T) {
  60. str := "t🐳ststring"
  61. newstr := Truncate(str, 4)
  62. if newstr != "t🐳st" {
  63. t.Fatalf("Expected t🐳st, got %s", newstr)
  64. }
  65. newstr = Truncate(str, 20)
  66. if newstr != "t🐳ststring" {
  67. t.Fatalf("Expected t🐳ststring, got %s", newstr)
  68. }
  69. }
  70. func TestInSlice(t *testing.T) {
  71. slice := []string{"t🐳st", "in", "slice"}
  72. test := InSlice(slice, "t🐳st")
  73. if !test {
  74. t.Fatalf("Expected string t🐳st to be in slice")
  75. }
  76. test = InSlice(slice, "SLICE")
  77. if !test {
  78. t.Fatalf("Expected string SLICE to be in slice")
  79. }
  80. test = InSlice(slice, "notinslice")
  81. if test {
  82. t.Fatalf("Expected string notinslice not to be in slice")
  83. }
  84. }
  85. func TestShellQuoteArgumentsEmpty(t *testing.T) {
  86. actual := ShellQuoteArguments([]string{})
  87. expected := ""
  88. if actual != expected {
  89. t.Fatalf("Expected an empty string")
  90. }
  91. }
  92. func TestShellQuoteArguments(t *testing.T) {
  93. simpleString := "simpleString"
  94. complexString := "This is a 'more' complex $tring with some special char *"
  95. actual := ShellQuoteArguments([]string{simpleString, complexString})
  96. expected := "simpleString 'This is a '\\''more'\\'' complex $tring with some special char *'"
  97. if actual != expected {
  98. t.Fatalf("Expected \"%v\", got \"%v\"", expected, actual)
  99. }
  100. }