github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/libs/strings/string_test.go (about)

     1  package strings
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestSplitAndTrimEmpty(t *testing.T) {
    10  	testCases := []struct {
    11  		s        string
    12  		sep      string
    13  		cutset   string
    14  		expected []string
    15  	}{
    16  		{"a,b,c", ",", " ", []string{"a", "b", "c"}},
    17  		{" a , b , c ", ",", " ", []string{"a", "b", "c"}},
    18  		{" a, b, c ", ",", " ", []string{"a", "b", "c"}},
    19  		{" a, ", ",", " ", []string{"a"}},
    20  		{"   ", ",", " ", []string{}},
    21  	}
    22  
    23  	for _, tc := range testCases {
    24  		require.Equal(t, tc.expected, SplitAndTrimEmpty(tc.s, tc.sep, tc.cutset), "%s", tc.s)
    25  	}
    26  }
    27  
    28  func assertCorrectTrim(t *testing.T, input, expected string) {
    29  	t.Helper()
    30  	output, err := ASCIITrim(input)
    31  	require.NoError(t, err)
    32  	require.Equal(t, expected, output)
    33  }
    34  
    35  func TestASCIITrim(t *testing.T) {
    36  	t.Run("Validation", func(t *testing.T) {
    37  		t.Run("NonASCII", func(t *testing.T) {
    38  			notASCIIText := []string{
    39  				"\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t",
    40  			}
    41  			for _, v := range notASCIIText {
    42  				_, err := ASCIITrim(v)
    43  				require.Error(t, err, "%q is not ascii-text", v)
    44  			}
    45  		})
    46  		t.Run("EmptyString", func(t *testing.T) {
    47  			out, err := ASCIITrim("")
    48  			require.NoError(t, err)
    49  			require.Zero(t, out)
    50  		})
    51  		t.Run("ASCIIText", func(t *testing.T) {
    52  			asciiText := []string{
    53  				" ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123",
    54  			}
    55  			for _, v := range asciiText {
    56  				_, err := ASCIITrim(v)
    57  				require.NoError(t, err, "%q is  ascii-text", v)
    58  			}
    59  		})
    60  		_, err := ASCIITrim("\xC2\xA2")
    61  		require.Error(t, err)
    62  	})
    63  	t.Run("Trimming", func(t *testing.T) {
    64  		assertCorrectTrim(t, " ", "")
    65  		assertCorrectTrim(t, " a", "a")
    66  		assertCorrectTrim(t, "a ", "a")
    67  		assertCorrectTrim(t, " a ", "a")
    68  	})
    69  
    70  }
    71  
    72  func TestStringSliceEqual(t *testing.T) {
    73  	tests := []struct {
    74  		a    []string
    75  		b    []string
    76  		want bool
    77  	}{
    78  		{[]string{"hello", "world"}, []string{"hello", "world"}, true},
    79  		{[]string{"test"}, []string{"test"}, true},
    80  		{[]string{"test1"}, []string{"test2"}, false},
    81  		{[]string{"hello", "world."}, []string{"hello", "world!"}, false},
    82  		{[]string{"only 1 word"}, []string{"two", "words!"}, false},
    83  		{[]string{"two", "words!"}, []string{"only 1 word"}, false},
    84  	}
    85  	for i, tt := range tests {
    86  		require.Equal(t, tt.want, StringSliceEqual(tt.a, tt.b),
    87  			"StringSliceEqual failed on test %d", i)
    88  	}
    89  }