stringutils_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 TestGenerateRandomAlphaOnlyStringLength(t *testing.T) {
  33. testLengthHelper(GenerateRandomAlphaOnlyString, t)
  34. }
  35. func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) {
  36. testUniquenessHelper(GenerateRandomAlphaOnlyString, t)
  37. }
  38. func TestGenerateRandomAsciiStringLength(t *testing.T) {
  39. testLengthHelper(GenerateRandomASCIIString, t)
  40. }
  41. func TestGenerateRandomAsciiStringUniqueness(t *testing.T) {
  42. testUniquenessHelper(GenerateRandomASCIIString, t)
  43. }
  44. func TestGenerateRandomAsciiStringIsAscii(t *testing.T) {
  45. str := GenerateRandomASCIIString(64)
  46. if !isASCII(str) {
  47. t.Fatalf("%s contained non-ascii characters", str)
  48. }
  49. }
  50. func TestEllipsis(t *testing.T) {
  51. str := "t🐳ststring"
  52. newstr := Ellipsis(str, 3)
  53. if newstr != "t🐳s" {
  54. t.Fatalf("Expected t🐳s, got %s", newstr)
  55. }
  56. newstr = Ellipsis(str, 8)
  57. if newstr != "t🐳sts..." {
  58. t.Fatalf("Expected tests..., got %s", newstr)
  59. }
  60. newstr = Ellipsis(str, 20)
  61. if newstr != "t🐳ststring" {
  62. t.Fatalf("Expected t🐳ststring, got %s", newstr)
  63. }
  64. }
  65. func TestTruncate(t *testing.T) {
  66. str := "t🐳ststring"
  67. newstr := Truncate(str, 4)
  68. if newstr != "t🐳st" {
  69. t.Fatalf("Expected t🐳st, got %s", newstr)
  70. }
  71. newstr = Truncate(str, 20)
  72. if newstr != "t🐳ststring" {
  73. t.Fatalf("Expected t🐳ststring, got %s", newstr)
  74. }
  75. }
  76. func TestInSlice(t *testing.T) {
  77. slice := []string{"t🐳st", "in", "slice"}
  78. test := InSlice(slice, "t🐳st")
  79. if !test {
  80. t.Fatalf("Expected string t🐳st to be in slice")
  81. }
  82. test = InSlice(slice, "SLICE")
  83. if !test {
  84. t.Fatalf("Expected string SLICE to be in slice")
  85. }
  86. test = InSlice(slice, "notinslice")
  87. if test {
  88. t.Fatalf("Expected string notinslice not to be in slice")
  89. }
  90. }
  91. func TestShellQuoteArgumentsEmpty(t *testing.T) {
  92. actual := ShellQuoteArguments([]string{})
  93. expected := ""
  94. if actual != expected {
  95. t.Fatalf("Expected an empty string")
  96. }
  97. }
  98. func TestShellQuoteArguments(t *testing.T) {
  99. simpleString := "simpleString"
  100. complexString := "This is a 'more' complex $tring with some special char *"
  101. actual := ShellQuoteArguments([]string{simpleString, complexString})
  102. expected := "simpleString 'This is a '\\''more'\\'' complex $tring with some special char *'"
  103. if actual != expected {
  104. t.Fatalf("Expected \"%v\", got \"%v\"", expected, actual)
  105. }
  106. }