github.com/vipernet-xyz/tm@v0.34.24/libs/strings/string_test.go (about)

     1  package strings
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestStringInSlice(t *testing.T) {
    12  	assert.True(t, StringInSlice("a", []string{"a", "b", "c"}))
    13  	assert.False(t, StringInSlice("d", []string{"a", "b", "c"}))
    14  	assert.True(t, StringInSlice("", []string{""}))
    15  	assert.False(t, StringInSlice("", []string{}))
    16  }
    17  
    18  func TestIsASCIIText(t *testing.T) {
    19  	notASCIIText := []string{
    20  		"", "\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t",
    21  	}
    22  	for _, v := range notASCIIText {
    23  		assert.False(t, IsASCIIText(v), "%q is not ascii-text", v)
    24  	}
    25  	asciiText := []string{
    26  		" ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123",
    27  	}
    28  	for _, v := range asciiText {
    29  		assert.True(t, IsASCIIText(v), "%q is ascii-text", v)
    30  	}
    31  }
    32  
    33  func TestASCIITrim(t *testing.T) {
    34  	assert.Equal(t, ASCIITrim(" "), "")
    35  	assert.Equal(t, ASCIITrim(" a"), "a")
    36  	assert.Equal(t, ASCIITrim("a "), "a")
    37  	assert.Equal(t, ASCIITrim(" a "), "a")
    38  	assert.Panics(t, func() { ASCIITrim("\xC2\xA2") })
    39  }
    40  
    41  func TestStringSliceEqual(t *testing.T) {
    42  	tests := []struct {
    43  		a    []string
    44  		b    []string
    45  		want bool
    46  	}{
    47  		{[]string{"hello", "world"}, []string{"hello", "world"}, true},
    48  		{[]string{"test"}, []string{"test"}, true},
    49  		{[]string{"test1"}, []string{"test2"}, false},
    50  		{[]string{"hello", "world."}, []string{"hello", "world!"}, false},
    51  		{[]string{"only 1 word"}, []string{"two", "words!"}, false},
    52  		{[]string{"two", "words!"}, []string{"only 1 word"}, false},
    53  	}
    54  	for i, tt := range tests {
    55  		require.Equal(t, tt.want, StringSliceEqual(tt.a, tt.b),
    56  			"StringSliceEqual failed on test %d", i)
    57  	}
    58  }