-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
single_test.go
61 lines (55 loc) · 1.22 KB
/
single_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
package linq_test
import (
"errors"
"testing"
"github.com/makiuchi-d/linq/v2"
)
func TestSingle(t *testing.T) {
src := linq.FromSlice([]int{1, 2, 3})
r, err := linq.Single(linq.Where(src,
func(v int) (bool, error) {
return v%2 == 0, nil
}))
if err != nil {
t.Fatalf("%v", err)
}
exp := 2
if r != exp {
t.Fatalf("%v, wants %v", r, exp)
}
_, err = linq.Single(linq.Where(src,
func(v int) (bool, error) {
return v%2 != 0, nil
}))
if !errors.Is(err, linq.InvalidOperation) {
t.Fatalf("%#v, wants %#v", err, linq.InvalidOperation)
}
}
func TestSingleOrDefault(t *testing.T) {
src := linq.FromSlice([]int{1, 2, 3})
def := 42
r, err := linq.SingleOrDefault(linq.Where(src,
func(v int) (bool, error) { return v%2 == 0, nil }),
def)
if err != nil {
t.Fatalf("%v", err)
}
exp := 2
if r != exp {
t.Fatalf("%v, wants %v", r, exp)
}
r, err = linq.SingleOrDefault(linq.Empty[int](), def)
if err != nil {
t.Fatalf("%v", err)
}
exp = def
if r != exp {
t.Fatalf("%v, wants %v", r, exp)
}
_, err = linq.SingleOrDefault(linq.Where(src,
func(v int) (bool, error) { return v%2 != 0, nil }),
def)
if !errors.Is(err, linq.InvalidOperation) {
t.Fatalf("%#v, wants %#v", err, linq.InvalidOperation)
}
}