44 "strconv"
55 "testing"
66
7+ "github.com/stretchr/testify/assert"
8+
79 "github.com/m4gshm/gollections/convert/ptr"
810 "github.com/m4gshm/gollections/map_"
911 "github.com/m4gshm/gollections/map_/clone"
@@ -12,7 +14,6 @@ import (
1214 "github.com/m4gshm/gollections/op"
1315 "github.com/m4gshm/gollections/slice"
1416 "github.com/m4gshm/gollections/slice/clone/sort"
15- "github.com/stretchr/testify/assert"
1617)
1718
1819type entity struct { val string }
@@ -178,3 +179,60 @@ func Test_ToSliceErrorable(t *testing.T) {
178179 })
179180 assert .Equal (t , slice .Of (2 , 4 , 6 ), sort .Of (result ))
180181}
182+
183+ func Test_Filter (t * testing.T ) {
184+ elements := map [int ]string {4 : "4" , 2 : "2" , 1 : "1" , 3 : "3" }
185+
186+ result := map_ .Filter (elements , func (key int , val string ) bool { return key <= 2 || val == "4" })
187+ check := map_ .KeyChecker (result )
188+ assert .Equal (t , 3 , len (result ))
189+ assert .True (t , check (1 ))
190+ assert .True (t , check (2 ))
191+ assert .False (t , check (3 ))
192+ assert .True (t , check (4 ))
193+ }
194+
195+ func Test_FilterKeys (t * testing.T ) {
196+ elements := map [int ]string {4 : "4" , 2 : "2" , 1 : "1" , 3 : "3" }
197+
198+ result := map_ .FilterKeys (elements , func (key int ) bool { return key <= 2 })
199+ check := map_ .KeyChecker (result )
200+ assert .Equal (t , 2 , len (result ))
201+ assert .True (t , check (1 ))
202+ assert .True (t , check (2 ))
203+ assert .False (t , check (3 ))
204+ assert .False (t , check (4 ))
205+ }
206+
207+ func Test_FilterValues (t * testing.T ) {
208+ elements := map [int ]string {4 : "4" , 2 : "2" , 1 : "1" , 3 : "3" }
209+
210+ result := map_ .FilterValues (elements , func (val string ) bool { return val <= "2" })
211+ check := map_ .KeyChecker (result )
212+ assert .Equal (t , 2 , len (result ))
213+ assert .True (t , check (1 ))
214+ assert .True (t , check (2 ))
215+ assert .False (t , check (3 ))
216+ assert .False (t , check (4 ))
217+ }
218+
219+ func Test_Getter (t * testing.T ) {
220+ elements := map [int ]string {4 : "4" , 2 : "2" , 1 : "1" , 3 : "3" }
221+
222+ getter := map_ .Getter (elements )
223+ assert .Equal (t , "1" , getter (1 ))
224+ assert .Equal (t , "2" , getter (2 ))
225+ assert .Equal (t , "3" , getter (3 ))
226+ assert .Equal (t , "4" , getter (4 ))
227+
228+ nilGetter := map_.Getter [map [int ]string ](nil )
229+
230+ assert .Equal (t , "" , nilGetter (0 ))
231+
232+ getterOk := map_ .GetterOk (elements )
233+
234+ _ , ok := getterOk (1 )
235+ assert .True (t , ok )
236+ _ , ok = getterOk (0 )
237+ assert .False (t , ok )
238+ }
0 commit comments