-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsort_test.go
38 lines (30 loc) · 986 Bytes
/
sort_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
package collectjs
import (
"bytes"
"testing"
)
var planets = []*Collect{
New(`["Venus", 0.815, 0.7]`),
New(`["Earth", 1.0, 1.0]`),
New(`["Mars", 0.107, 1.5]`),
}
func TestSort(t *testing.T) {
// Closures that order the Planet structure.
name := func(p1, p2 *Collect) bool {
return bytes.Compare(p1.Get("[0]").raw, p2.Get("[0]").raw) > 0
}
mass := func(p1, p2 *Collect) bool {
return bytes.Compare(p1.Get("[1]").raw, p2.Get("[1]").raw) > 0
}
distance := func(p1, p2 *Collect) bool {
return bytes.Compare(p1.Get("[2]").raw, p2.Get("[2]").raw) > 0
}
// Sort the planets by the various criteria.
By(name).Sort(planets)
t.Log("By name:", string(planets[0].raw), string(planets[1].raw), string(planets[2].raw))
By(mass).Sort(planets)
t.Log("By mass:", string(planets[0].raw), string(planets[1].raw), string(planets[2].raw))
By(distance).Sort(planets)
t.Log("By distance:", string(planets[0].raw), string(planets[1].raw), string(planets[2].raw))
// Output:
}