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