github.com/miqui/docker@v1.9.1/pkg/stringutils/stringutils_test.go (about)

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