-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
orderby_test.go
91 lines (77 loc) · 2.23 KB
/
orderby_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package linq_test
import (
"testing"
"github.com/makiuchi-d/linq/v2"
)
func TestOrderBy(t *testing.T) {
fruits := []string{
"grape", "passionfruit", "banana", "mango",
"orange", "raspberry", "apple", "blueberry"}
e1 := linq.FromSlice(fruits)
e2 := linq.OrderBy(e1, func(v string) (int, error) { return len(v), nil })
r, err := linq.ToSlice(e2)
if err != nil {
t.Fatalf("%v", err)
}
for i := 1; i < len(r); i++ {
if len(r[i-1]) > len(r[i]) {
t.Fatalf("len(%q) > len(%q)\n\t%v", r[i-1], r[i], r)
}
}
}
func TestOrderByDescending(t *testing.T) {
fruits := []string{
"grape", "passionfruit", "banana", "mango",
"orange", "raspberry", "apple", "blueberry"}
e1 := linq.FromSlice(fruits)
e2 := linq.OrderByDescending(e1, func(v string) (int, error) { return len(v), nil })
r, err := linq.ToSlice(e2)
if err != nil {
t.Fatalf("%v", err)
}
for i := 1; i < len(r); i++ {
if len(r[i-1]) < len(r[i]) {
t.Fatalf("len(%q) < len(%q)\n\t%v", r[i-1], r[i], r)
}
}
}
func TestThenBy(t *testing.T) {
fruits := []string{
"grape", "passionfruit", "banana", "mango",
"orange", "raspberry", "apple", "blueberry"}
e1 := linq.FromSlice(fruits)
e2 := linq.OrderByDescending(e1, func(v string) (int, error) { return len(v), nil })
e3 := linq.ThenBy(e2, func(v string) (string, error) { return v, nil })
r, err := linq.ToSlice(e3)
if err != nil {
t.Fatalf("%v", err)
}
for i := 1; i < len(r); i++ {
if len(r[i-1]) < len(r[i]) {
t.Fatalf("len(%q) < len(%q)\n\t%v", r[i-1], r[i], r)
}
if len(r[i-1]) == len(r[i]) && r[i-1] > r[i] {
t.Fatalf("%q > %q\n\t%v", r[i-1], r[i], r)
}
}
}
func TestThenByDescending(t *testing.T) {
fruits := []string{
"grape", "passionfruit", "banana", "mango",
"orange", "raspberry", "apple", "blueberry"}
e1 := linq.FromSlice(fruits)
e2 := linq.OrderBy(e1, func(v string) (int, error) { return len(v), nil })
e3 := linq.ThenByDescending(e2, func(v string) (string, error) { return v, nil })
r, err := linq.ToSlice(e3)
if err != nil {
t.Fatalf("%v", err)
}
for i := 1; i < len(r); i++ {
if len(r[i-1]) > len(r[i]) {
t.Fatalf("len(%q) > len(%q)\n\t%v", r[i-1], r[i], r)
}
if len(r[i-1]) == len(r[i]) && r[i-1] < r[i] {
t.Fatalf("%q < %q\n\t%v", r[i-1], r[i], r)
}
}
}