github.com/openflowlabs/storage@v1.12.13/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 TestEllipsis(t *testing.T) {
    61  	str := "t🐳ststring"
    62  	newstr := Ellipsis(str, 3)
    63  	if newstr != "t🐳s" {
    64  		t.Fatalf("Expected t🐳s, got %s", newstr)
    65  	}
    66  	newstr = Ellipsis(str, 8)
    67  	if newstr != "t🐳sts..." {
    68  		t.Fatalf("Expected tests..., got %s", newstr)
    69  	}
    70  	newstr = Ellipsis(str, 20)
    71  	if newstr != "t🐳ststring" {
    72  		t.Fatalf("Expected t🐳ststring, got %s", newstr)
    73  	}
    74  }
    75  
    76  func TestTruncate(t *testing.T) {
    77  	str := "t🐳ststring"
    78  	newstr := Truncate(str, 4)
    79  	if newstr != "t🐳st" {
    80  		t.Fatalf("Expected t🐳st, got %s", newstr)
    81  	}
    82  	newstr = Truncate(str, 20)
    83  	if newstr != "t🐳ststring" {
    84  		t.Fatalf("Expected t🐳ststring, got %s", newstr)
    85  	}
    86  }
    87  
    88  func TestInSlice(t *testing.T) {
    89  	slice := []string{"t🐳st", "in", "slice"}
    90  
    91  	test := InSlice(slice, "t🐳st")
    92  	if !test {
    93  		t.Fatalf("Expected string t🐳st to be in slice")
    94  	}
    95  	test = InSlice(slice, "SLICE")
    96  	if !test {
    97  		t.Fatalf("Expected string SLICE to be in slice")
    98  	}
    99  	test = InSlice(slice, "notinslice")
   100  	if test {
   101  		t.Fatalf("Expected string notinslice not to be in slice")
   102  	}
   103  }
   104  
   105  func TestShellQuoteArgumentsEmpty(t *testing.T) {
   106  	actual := ShellQuoteArguments([]string{})
   107  	expected := ""
   108  	if actual != expected {
   109  		t.Fatalf("Expected an empty string")
   110  	}
   111  }
   112  
   113  func TestShellQuoteArguments(t *testing.T) {
   114  	simpleString := "simpleString"
   115  	complexString := "This is a 'more' complex $tring with some special char *"
   116  	actual := ShellQuoteArguments([]string{simpleString, complexString})
   117  	expected := "simpleString 'This is a '\\''more'\\'' complex $tring with some special char *'"
   118  	if actual != expected {
   119  		t.Fatalf("Expected \"%v\", got \"%v\"", expected, actual)
   120  	}
   121  }