github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/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 t.Parallel() 13 14 assert.True(t, StringInSlice("a", []string{"a", "b", "c"})) 15 assert.False(t, StringInSlice("d", []string{"a", "b", "c"})) 16 assert.True(t, StringInSlice("", []string{""})) 17 assert.False(t, StringInSlice("", []string{})) 18 } 19 20 func TestIsASCIIText(t *testing.T) { 21 t.Parallel() 22 23 notASCIIText := []string{ 24 "", "\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t", 25 } 26 for _, v := range notASCIIText { 27 assert.False(t, IsASCIIText(v), "%q is not ascii-text", v) 28 } 29 asciiText := []string{ 30 " ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123", 31 } 32 for _, v := range asciiText { 33 assert.True(t, IsASCIIText(v), "%q is ascii-text", v) 34 } 35 } 36 37 func TestASCIITrim(t *testing.T) { 38 t.Parallel() 39 40 assert.Equal(t, ASCIITrim(" "), "") 41 assert.Equal(t, ASCIITrim(" a"), "a") 42 assert.Equal(t, ASCIITrim("a "), "a") 43 assert.Equal(t, ASCIITrim(" a "), "a") 44 assert.Panics(t, func() { ASCIITrim("\xC2\xA2") }) 45 } 46 47 func TestStringSliceEqual(t *testing.T) { 48 t.Parallel() 49 50 tests := []struct { 51 a []string 52 b []string 53 want bool 54 }{ 55 {[]string{"hello", "world"}, []string{"hello", "world"}, true}, 56 {[]string{"test"}, []string{"test"}, true}, 57 {[]string{"test1"}, []string{"test2"}, false}, 58 {[]string{"hello", "world."}, []string{"hello", "world!"}, false}, 59 {[]string{"only 1 word"}, []string{"two", "words!"}, false}, 60 {[]string{"two", "words!"}, []string{"only 1 word"}, false}, 61 } 62 for i, tt := range tests { 63 require.Equal(t, tt.want, StringSliceEqual(tt.a, tt.b), 64 "StringSliceEqual failed on test %d", i) 65 } 66 }