stringutils_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 TestTruncate(t *testing.T) {
  51. str := "teststring"
  52. newstr := Truncate(str, 4)
  53. if newstr != "test" {
  54. t.Fatalf("Expected test, got %s", newstr)
  55. }
  56. newstr = Truncate(str, 20)
  57. if newstr != "teststring" {
  58. t.Fatalf("Expected teststring, got %s", newstr)
  59. }
  60. }
  61. func TestInSlice(t *testing.T) {
  62. slice := []string{"test", "in", "slice"}
  63. test := InSlice(slice, "test")
  64. if !test {
  65. t.Fatalf("Expected string test to be in slice")
  66. }
  67. test = InSlice(slice, "SLICE")
  68. if !test {
  69. t.Fatalf("Expected string SLICE to be in slice")
  70. }
  71. test = InSlice(slice, "notinslice")
  72. if test {
  73. t.Fatalf("Expected string notinslice not to be in slice")
  74. }
  75. }
  76. func TestShellQuoteArgumentsEmpty(t *testing.T) {
  77. actual := ShellQuoteArguments([]string{})
  78. expected := ""
  79. if actual != expected {
  80. t.Fatalf("Expected an empty string")
  81. }
  82. }
  83. func TestShellQuoteArguments(t *testing.T) {
  84. simpleString := "simpleString"
  85. complexString := "This is a 'more' complex $tring with some special char *"
  86. actual := ShellQuoteArguments([]string{simpleString, complexString})
  87. expected := "simpleString 'This is a '\\''more'\\'' complex $tring with some special char *'"
  88. if actual != expected {
  89. t.Fatalf("Expected \"%v\", got \"%v\"", expected, actual)
  90. }
  91. }