-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
iter_test.go
62 lines (54 loc) · 1.25 KB
/
iter_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
56
57
58
59
60
61
62
//go:build go1.23
package linq_test
import (
"context"
"reflect"
"testing"
"github.com/makiuchi-d/linq/v2"
)
func TestIterAll(t *testing.T) {
src := linq.FromSlice([]int{1, 2, 3, 4, 5})
c := 0
s := 0
for n, err := range src.All() {
if err != nil {
t.Fatalf("%v", err)
}
c++
s += n
}
if c != 5 || s != 15 {
t.Fatalf("(c, s) = (%v, %v) wants (%v, %v)", c, s, 5, 15)
}
}
func TestFromIterator(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
e := linq.FromIterator(ctx, func(yield func(int) bool) {
_ = yield(1) && yield(2) && yield(3) && yield(4) && yield(5)
})
e = linq.Take(e, 3)
a, err := linq.ToSlice(e)
if err != nil {
t.Fatalf("%+v", err)
}
exp := []int{1, 2, 3}
if !reflect.DeepEqual(a, exp) {
t.Fatalf("ToSlice: %v, wants %v", a, exp)
}
}
func TestFromIterator2(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
e := linq.FromIterator2(ctx, func(yield func(int, string) bool) {
_ = yield(1, "one") && yield(2, "two") && yield(3, "three")
})
a, err := linq.ToMap(e)
if err != nil {
t.Fatalf("%+v", err)
}
exp := map[int]string{1: "one", 2: "two", 3: "three"}
if !reflect.DeepEqual(a, exp) {
t.Fatalf("ToMap: %v, wants %v", a, exp)
}
}