@@ -308,6 +308,92 @@ func TestUnion(t *testing.T) {
308308 is .IsType (nonempty , allStrings , "type preserved" )
309309}
310310
311+ func TestUnionBy (t * testing.T ) {
312+ t .Parallel ()
313+ is := assert .New (t )
314+
315+ testFunc := func (i int ) int {
316+ return i / 2
317+ }
318+
319+ result1 := UnionBy (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {0 , 2 , 10 })
320+ result2 := UnionBy (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {6 , 7 })
321+ result3 := UnionBy (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {})
322+ result4 := UnionBy (testFunc , []int {0 , 1 , 2 }, []int {0 , 1 , 2 })
323+ result5 := UnionBy (testFunc , []int {}, []int {})
324+ is .Equal ([]int {0 , 2 , 4 , 10 }, result1 )
325+ is .Equal ([]int {0 , 2 , 4 , 6 }, result2 )
326+ is .Equal ([]int {0 , 2 , 4 }, result3 )
327+ is .Equal ([]int {0 , 2 }, result4 )
328+ is .Equal ([]int {}, result5 )
329+
330+ result11 := UnionBy (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {0 , 2 , 10 }, []int {0 , 1 , 11 })
331+ result12 := UnionBy (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {6 , 7 }, []int {8 , 9 })
332+ result13 := UnionBy (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {}, []int {})
333+ result14 := UnionBy (testFunc , []int {0 , 1 , 2 }, []int {0 , 1 , 2 }, []int {0 , 1 , 2 })
334+ result15 := UnionBy (testFunc , []int {}, []int {}, []int {})
335+ is .Equal ([]int {0 , 2 , 4 , 10 }, result11 )
336+ is .Equal ([]int {0 , 2 , 4 , 6 , 8 }, result12 )
337+ is .Equal ([]int {0 , 2 , 4 }, result13 )
338+ is .Equal ([]int {0 , 2 }, result14 )
339+ is .Equal ([]int {}, result15 )
340+ }
341+
342+ func TestUnionByErr (t * testing.T ) {
343+ t .Parallel ()
344+ is := assert .New (t )
345+
346+ testFunc := func (i int ) (int , error ) {
347+ return i / 2 , nil
348+ }
349+
350+ result1 , err1 := UnionByErr (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {0 , 2 , 10 })
351+ result2 , err2 := UnionByErr (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {6 , 7 })
352+ result3 , err3 := UnionByErr (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {})
353+ result4 , err4 := UnionByErr (testFunc , []int {0 , 1 , 2 }, []int {0 , 1 , 2 })
354+ result5 , err5 := UnionByErr (testFunc , []int {}, []int {})
355+ is .NoError (err1 )
356+ is .NoError (err2 )
357+ is .NoError (err3 )
358+ is .NoError (err4 )
359+ is .NoError (err5 )
360+ is .Equal ([]int {0 , 2 , 4 , 10 }, result1 )
361+ is .Equal ([]int {0 , 2 , 4 , 6 }, result2 )
362+ is .Equal ([]int {0 , 2 , 4 }, result3 )
363+ is .Equal ([]int {0 , 2 }, result4 )
364+ is .Equal ([]int {}, result5 )
365+
366+ result11 , err11 := UnionByErr (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {0 , 2 , 10 }, []int {0 , 1 , 11 })
367+ result12 , err12 := UnionByErr (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {6 , 7 }, []int {8 , 9 })
368+ result13 , err13 := UnionByErr (testFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {}, []int {})
369+ result14 , err14 := UnionByErr (testFunc , []int {0 , 1 , 2 }, []int {0 , 1 , 2 }, []int {0 , 1 , 2 })
370+ result15 , err15 := UnionByErr (testFunc , []int {}, []int {}, []int {})
371+ is .NoError (err11 )
372+ is .NoError (err12 )
373+ is .NoError (err13 )
374+ is .NoError (err14 )
375+ is .NoError (err15 )
376+ is .Equal ([]int {0 , 2 , 4 , 10 }, result11 )
377+ is .Equal ([]int {0 , 2 , 4 , 6 , 8 }, result12 )
378+ is .Equal ([]int {0 , 2 , 4 }, result13 )
379+ is .Equal ([]int {0 , 2 }, result14 )
380+ is .Equal ([]int {}, result15 )
381+
382+ // Test error case
383+ errFunc := func (i int ) (int , error ) {
384+ if i == 2 {
385+ return 0 , assert .AnError
386+ }
387+ return i / 2 , nil
388+ }
389+
390+ _ , err6 := UnionByErr (errFunc , []int {0 , 1 , 2 , 3 , 4 , 5 }, []int {0 , 2 , 10 })
391+ is .Error (err6 )
392+
393+ _ , err7 := UnionByErr (errFunc , []int {0 , 1 , 3 , 4 , 5 }, []int {2 , 10 })
394+ is .Error (err7 )
395+ }
396+
311397func TestWithout (t * testing.T ) {
312398 t .Parallel ()
313399 is := assert .New (t )
0 commit comments