-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinct_test.go
55 lines (48 loc) · 1.51 KB
/
distinct_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package golinq
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenericDistinct(t *testing.T) {
t.Parallel()
test := GenericTest{
Str: "generic",
IntList: []int{1, 3, 4, 5, 3, 3, 2, 3, 3, 4, 2, 2},
StrList: []string{"g", "e", "n", "e", "r", "i", "c"},
MapIntStr: map[int]string{-1: "a", 2: "a", 3: "a"},
ChanFloat: make(chan float64, 3),
}
wanted := GenericTest{
Int: 1,
RuneList: []int32{'g', 'e', 'n', 'r', 'i', 'c'},
IntList: []int{1, 3, 4, 5, 2},
StrList: []string{"g", "e", "n", "r", "i", "c"},
MapIntStr: map[int]string{-1: "a"},
FloatList: []float64{1.0},
}
t.Run("String", func(t *testing.T) {
res := FromString(test.Str).Distinct().ToSlice()
assert.Equal(t, wanted.RuneList, res)
})
t.Run("IntList", func(t *testing.T) {
res := FromSlice(test.IntList).Distinct().ToSlice()
assert.Equal(t, wanted.IntList, res)
})
t.Run("StringList", func(t *testing.T) {
res := FromSlice(test.StrList).Distinct().ToSlice()
assert.Equal(t, wanted.StrList, res)
})
t.Run("MapIntStr", func(t *testing.T) {
res := make(map[int]string)
FromMap(test.MapIntStr).DistinctBy(func(i int, s string) any { return s }).AsMap(&res)
assert.Equal(t, wanted.Int, len(res))
})
t.Run("ChanFloat", func(t *testing.T) {
test.ChanFloat <- 1.0
test.ChanFloat <- 2.0
test.ChanFloat <- 3.0
close(test.ChanFloat)
res := FromChan(test.ChanFloat).DistinctBy(func(i int, f float64) any { return f - float64(i) }).ToSlice()
assert.Equal(t, wanted.FloatList, res)
})
}