github.com/seeker-insurance/kit@v0.0.13/str/str_test.go (about) 1 package str 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/seeker-insurance/kit/pretty" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestRemoveRunes(t *testing.T) { 13 var alphabet = []rune(ASCIILowercase) 14 var digits = []rune(ASCIINumerics) 15 type args struct { 16 s string 17 runes []rune 18 } 19 tests := []struct { 20 name string 21 args args 22 want string 23 }{ 24 {"remove_alphabet", args{"foo11829bar", alphabet}, "11829"}, 25 {"remove_digits", args{"foo11829bar", digits}, "foobar"}, 26 {"no_op", args{"foobar", []rune{}}, "foobar"}, 27 } 28 for _, tt := range tests { 29 t.Run(tt.name, func(t *testing.T) { 30 if got := RemoveRunes(tt.args.s, tt.args.runes...); got != tt.want { 31 t.Errorf("RemoveRunes() = %v, want %v", got, tt.want) 32 } 33 }) 34 } 35 } 36 37 func TestDiffs_String(t *testing.T) { 38 tests := []struct { 39 name string 40 d Diffs 41 want string 42 }{ 43 // TODO: Add test cases. 44 } 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 if got := tt.d.String(); got != tt.want { 48 t.Errorf("Diffs.String() = %v, want %v", got, tt.want) 49 } 50 }) 51 } 52 } 53 54 func Test_runeDiff(t *testing.T) { 55 type args struct { 56 s []rune 57 q []rune 58 } 59 tests := []struct { 60 name string 61 args args 62 want Diffs 63 }{ 64 // TODO: Add test cases. 65 } 66 for _, tt := range tests { 67 t.Run(tt.name, func(t *testing.T) { 68 if got := runeDiff(tt.args.s, tt.args.q); !reflect.DeepEqual(got, tt.want) { 69 t.Errorf("runeDiff() = %v, want %v", got, tt.want) 70 } 71 }) 72 } 73 } 74 75 func TestDiff(t *testing.T) { 76 type args struct { 77 a string 78 b string 79 } 80 const accent rune = 769 81 tests := []struct { 82 name string 83 args args 84 want Diffs 85 }{ 86 { 87 name: "ok", 88 args: args{a: "Foo", b: "foo"}, 89 want: Diffs{RuneDiff{position: 0, a: 'F', b: 'f'}}, 90 }, 91 { 92 name: "don't fold combining", 93 args: args{a: NFC("Finé"), b: NFD("Finé")}, 94 want: Diffs{RuneDiff{position: 3, a: 'é', b: 'e'}, RuneDiff{position: 4, a: noChar, b: accent}}, 95 }, { 96 name: "don't fold combining pt 2", 97 args: args{a: NFD("Finé"), b: NFC("Finé")}, 98 want: Diffs{RuneDiff{position: 3, a: 'e', b: 'é'}, RuneDiff{position: 4, a: accent, b: noChar}}, 99 }, 100 } 101 for _, tt := range tests { 102 t.Run(tt.name, func(t *testing.T) { 103 if got := Diff(tt.args.a, tt.args.b); !reflect.DeepEqual(got, tt.want) { 104 pretty.Print(pretty.Diff(got, tt.want)) 105 t.Errorf("got %v, wanted %v", got, tt.want) 106 107 } 108 }) 109 } 110 } 111 112 func Test_subIfNoChar(t *testing.T) { 113 type args struct { 114 r rune 115 } 116 tests := []struct { 117 name string 118 args args 119 want string 120 }{ 121 { 122 name: "ok", 123 args: args{noChar}, 124 want: "NO_CHAR", 125 }, 126 {name: "self", 127 args: args{'f'}, 128 want: "f", 129 }, 130 } 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 if got := subIfNoChar(tt.args.r); got != tt.want { 134 t.Errorf("subIfNoChar() = %v, want %v", got, tt.want) 135 } 136 }) 137 } 138 } 139 140 func TestRuneDiff_String(t *testing.T) { 141 type fields struct { 142 a rune 143 b rune 144 position int 145 } 146 tests := []struct { 147 name string 148 fields fields 149 want string 150 }{ 151 // TODO: Add test cases. 152 } 153 for _, tt := range tests { 154 t.Run(tt.name, func(t *testing.T) { 155 rd := RuneDiff{ 156 a: tt.fields.a, 157 b: tt.fields.b, 158 position: tt.fields.position, 159 } 160 if got := rd.String(); got != tt.want { 161 t.Errorf("RuneDiff.String() = %v, want %v", got, tt.want) 162 } 163 }) 164 } 165 } 166 167 func Test_min(t *testing.T) { 168 type args struct { 169 a int 170 b int 171 } 172 tests := []struct { 173 name string 174 args args 175 want int 176 }{ 177 // TODO: Add test cases. 178 } 179 for _, tt := range tests { 180 t.Run(tt.name, func(t *testing.T) { 181 if got := min(tt.args.a, tt.args.b); got != tt.want { 182 t.Errorf("min() = %v, want %v", got, tt.want) 183 } 184 }) 185 } 186 } 187 188 func TestMap(t *testing.T) { 189 type args struct { 190 f func(string) string 191 a []string 192 } 193 194 tests := []struct { 195 name string 196 args args 197 want []string 198 }{ 199 { 200 name: "capitalize", 201 args: args{strings.ToUpper, []string{"lower", "upper"}}, 202 want: []string{"LOWER", "UPPER"}, 203 }, 204 } 205 for _, tt := range tests { 206 t.Run(tt.name, func(t *testing.T) { 207 if got := Map(tt.args.f, tt.args.a); !reflect.DeepEqual(got, tt.want) { 208 t.Errorf("Map() = %v, want %v", got, tt.want) 209 } 210 }) 211 } 212 } 213 214 func Test_ContainsAny(t *testing.T) { 215 const ( 216 s = "asdasdmansdasd_foo_bar_klasmcdlaksmdasd" 217 x = "XXX" 218 z = "ZZZ" 219 ) 220 assert.True(t, ContainsAny(s, "foo", x, z)) 221 assert.False(t, ContainsAny(s, x, z)) 222 assert.True(t, ContainsAny(s, "bar")) 223 assert.False(t, ContainsAny(s)) 224 225 } 226 227 func Test_ContainsAll(t *testing.T) { 228 const ( 229 s = "asdasdmansdasd_foo_bar_klasmcdlaksmdasd" 230 x = "XXX" 231 z = "ZZZ" 232 ) 233 234 assert.True(t, ContainsAll(s, "foo", "bar")) 235 assert.False(t, ContainsAll(s, "foo", "bar", "baz")) 236 assert.True(t, ContainsAll(s)) 237 } 238 239 func TestTrim(t *testing.T) { 240 241 const ( 242 quickbrown = "the quick brown fox jumps over the lazy dog" 243 trimmed = "the quick brown" 244 filler = "..." 245 want = trimmed + filler 246 ) 247 assert.Equal(t, want, Trim(quickbrown, len(trimmed), filler)) 248 }