github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xstrings/mutable_string_test.go (about) 1 package xstrings 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestMutableStringToString(t *testing.T) { 10 s := FromString("Hello,world") 11 require.Equal(t, "Hello,world", s.ToString()) 12 } 13 14 func TestMutableStringInsert(t *testing.T) { 15 t.Run("Insert", func(t *testing.T) { 16 s := FromString("Hello,world") 17 s.Insert(0, '!') 18 require.Equal(t, "!Hello,world", s.ToString()) 19 }) 20 t.Run("InsertString", func(t *testing.T) { 21 s := FromString("Hello,") 22 s.InsertString(s.Length(), "world") 23 require.Equal(t, "Hello,world", s.ToString()) 24 }) 25 } 26 27 func TestMutableStringRemove(t *testing.T) { 28 t.Run("Remove", func(t *testing.T) { 29 s := FromString("Hello,world") 30 s.Remove(0) 31 require.Equal(t, "ello,world", s.ToString()) 32 s.Remove(3) 33 require.Equal(t, "ell,world", s.ToString()) 34 }) 35 t.Run("RemoveRange", func(t *testing.T) { 36 s := FromString("Hello,world") 37 s.RemoveRange(0, 2) 38 require.Equal(t, "llo,world", s.ToString()) 39 s.RemoveRange(3, 3) 40 require.Equal(t, "llorld", s.ToString()) 41 }) 42 } 43 44 func TestMutableStringReplace(t *testing.T) { 45 t.Run("Replace", func(t *testing.T) { 46 s := FromString("Hello,world") 47 s.Replace(0, 'A') 48 require.Equal(t, "Aello,world", s.ToString()) 49 s.Replace(2, 'b') 50 require.Equal(t, "Aeblo,world", s.ToString()) 51 }) 52 t.Run("ReplaceRange", func(t *testing.T) { 53 s := FromString("Hello,world") 54 s.ReplaceRange(0, []byte{'W', 'o', 'r', 'l', 'd'}) 55 require.Equal(t, "World,world", s.ToString()) 56 s.ReplaceRange(6, []byte{'h', 'e', 'l', 'l', 'o'}) 57 require.Equal(t, "World,hello", s.ToString()) 58 }) 59 }