github.com/seeker-insurance/kit@v0.0.13/set/string_test.go (about) 1 package set 2 3 import ( 4 "reflect" 5 "sort" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 const ( 12 _str_foo string = string('a' + iota) 13 _str_bar 14 _str_baz 15 _str_moo 16 ) 17 18 var ( 19 _str_fooBar = FromStrings(_str_foo, _str_bar) 20 _str_barBaz = FromStrings(_str_bar, _str_baz) 21 _str_fooBaz = FromStrings(_str_foo, _str_baz) 22 _str_fooBarBaz = FromStrings(_str_foo, _str_bar, _str_baz) 23 ) 24 25 func TestString_Union(t *testing.T) { 26 type args struct { 27 strings []String 28 } 29 30 tests := []struct { 31 name string 32 s String 33 args args 34 wantUnion String 35 }{ 36 { 37 name: "ok", 38 s: _str_fooBar, 39 args: args{[]String{_str_barBaz}}, 40 wantUnion: _str_fooBarBaz, 41 }, 42 } 43 for _, tt := range tests { 44 t.Run(tt.name, func(t *testing.T) { 45 if gotUnion := tt.s.Union(tt.args.strings...); !reflect.DeepEqual(gotUnion, tt.wantUnion) { 46 t.Errorf("String.Union() = %v, want %v", gotUnion, tt.wantUnion) 47 } 48 }) 49 } 50 } 51 52 func TestString_Difference(t *testing.T) { 53 type args struct { 54 strings []String 55 } 56 tests := []struct { 57 name string 58 s String 59 args args 60 wantDifference String 61 }{ 62 { 63 name: "ok", 64 s: _str_fooBarBaz, 65 args: args{[]String{_str_fooBar}}, 66 wantDifference: FromStrings(_str_baz), 67 }, 68 { 69 name: "return self", 70 s: _str_fooBarBaz, 71 wantDifference: _str_fooBarBaz, 72 }, 73 { 74 name: "multiples ok", 75 s: _str_fooBarBaz, 76 args: args{[]String{_str_fooBar, _str_barBaz}}, 77 }, 78 } 79 for _, tt := range tests { 80 t.Run(tt.name, func(t *testing.T) { 81 got := tt.s.Difference(tt.args.strings...) 82 if !got.Equal(tt.wantDifference) { 83 t.Errorf("got %v, but want %v", got, tt.wantDifference) 84 } 85 }) 86 } 87 } 88 89 func TestString_Add(t *testing.T) { 90 type args struct { 91 keys []string 92 } 93 tests := []struct { 94 name string 95 s String 96 args args 97 want String 98 }{ 99 { 100 name: "add", 101 s: String{_str_foo: yes}, 102 args: args{[]string{_str_foo, _str_foo, _str_bar, _str_baz}}, 103 want: _str_fooBarBaz, 104 }, 105 } 106 for _, tt := range tests { 107 t.Run(tt.name, func(t *testing.T) { 108 tt.s.Add(tt.args.keys...) 109 if !tt.s.Equal(tt.want) { 110 t.Errorf("reciever of s.Add() is %v, but should be %v", tt.s, tt.want) 111 } 112 }) 113 } 114 } 115 116 func TestString_Sorted(t *testing.T) { 117 tests := []struct { 118 name string 119 s String 120 want []string 121 wantFail bool 122 }{ 123 { 124 name: "ok", 125 s: _str_fooBar, 126 want: []string{_str_foo, _str_bar}, 127 }, 128 } 129 for _, tt := range tests { 130 t.Run(tt.name, func(t *testing.T) { 131 got := tt.s.Sorted() 132 sort.Strings(got) 133 sort.Strings(tt.want) 134 if !reflect.DeepEqual(got, tt.want) && !tt.wantFail { 135 t.Errorf("String.ToSlice() = %v, want %v", got, tt.want) 136 } 137 }) 138 } 139 } 140 141 func TestString_Intersection(t *testing.T) { 142 type args struct { 143 strings []String 144 } 145 tests := []struct { 146 name string 147 s String 148 args args 149 wantIntersection String 150 }{ 151 {"ok", _str_fooBar, args{[]String{_str_barBaz}}, FromStrings(_str_bar)}, 152 {"multiples", _str_fooBar, args{[]String{_str_fooBarBaz, _str_barBaz}}, FromStrings(_str_bar)}, 153 } 154 for _, tt := range tests { 155 t.Run(tt.name, func(t *testing.T) { 156 if got := tt.s.Intersection(tt.args.strings...); !got.Equal(tt.wantIntersection) { 157 t.Errorf("String.Intersection() = %v, want %v", got, tt.wantIntersection) 158 } 159 }) 160 } 161 } 162 163 func TestString_Equal(t *testing.T) { 164 type args struct { 165 other String 166 } 167 tests := []struct { 168 name string 169 s String 170 args args 171 want bool 172 }{ 173 { 174 "yes", 175 String{_str_foo: yes, _str_bar: yes}, 176 args{String{_str_foo: yes, _str_bar: yes}}, 177 true, 178 }, 179 { 180 "no", 181 String{_str_foo: yes, _str_bar: yes}, 182 args{String{}}, 183 false, 184 }, { 185 "no - nonoverlapping keys", 186 FromStrings(_str_foo, _str_bar), 187 args{other: FromStrings(_str_baz, _str_moo)}, 188 false, 189 }, 190 } 191 for _, tt := range tests { 192 t.Run(tt.name, func(t *testing.T) { 193 if got := tt.s.Equal(tt.args.other); got != tt.want { 194 t.Errorf("String.Equal() = %v, want %v", got, tt.want) 195 } 196 }) 197 } 198 } 199 200 func TestString_IUnion(t *testing.T) { 201 set := make(String) 202 a := FromStrings(_str_foo, _str_bar) 203 b := FromStrings(_str_baz) 204 205 set.IUnion(a) 206 assert.Equal(t, set, a) 207 set.IUnion(b) 208 b.IUnion(a) 209 assert.Equal(t, set, b) 210 } 211 func TestString_XOR(t *testing.T) { 212 type args struct { 213 a String 214 b String 215 } 216 tests := []struct { 217 name string 218 args args 219 want String 220 }{ 221 { 222 "ok", 223 args{String{_str_foo: yes, _str_bar: yes}, String{_str_foo: yes, _str_baz: yes}}, 224 String{_str_bar: yes, _str_baz: yes}, 225 }, 226 } 227 for _, tt := range tests { 228 t.Run(tt.name, func(t *testing.T) { 229 if got := tt.args.a.XOR(tt.args.b); !reflect.DeepEqual(got, tt.want) { 230 t.Errorf("XOR() = %v, want %v", got, tt.want) 231 } 232 }) 233 } 234 } 235 236 func TestString_Reduce(t *testing.T) { 237 min := func(a, b string) string { 238 if a < b { 239 return a 240 } 241 return b 242 } 243 244 set := FromStrings("aaa", "ab", "ccc") 245 want := "aaa" 246 got, _ := set.Reduce(min) 247 if want != got { 248 t.Errorf("reduce broken: should get %s, but got %s", want, got) 249 } 250 var emptySet String 251 if _, ok := emptySet.Reduce(min); ok { 252 t.Errorf("should not get OK on empty set") 253 } 254 } 255 256 func TestString_Map(t *testing.T) { 257 double := func(s string) string { 258 out := "" 259 for _, r := range s { 260 out += string(r) + string(r) 261 } 262 return out 263 } 264 set := FromStrings("ab", "cd") 265 want := FromStrings("aabb", "ccdd") 266 got := set.Map(double) 267 if !got.Equal(want) { 268 t.Errorf("map broken: %v", got.XOR(want)) 269 } 270 } 271 272 func TestString_Filter(t *testing.T) { 273 capital := func(s string) bool { 274 for _, r := range s { 275 if r < 'A' || r > 'Z' { 276 return false 277 } 278 } 279 return true 280 } 281 set := FromStrings("AAA", "BBB", "crasn", "44s") 282 want := FromStrings("AAA", "BBB") 283 got := set.Filter(capital) 284 if !want.Equal(got) { 285 t.Errorf("should get %v, but got %v", want, got) 286 } 287 }