-
Notifications
You must be signed in to change notification settings - Fork 5
/
sort_test.go
127 lines (106 loc) · 3.52 KB
/
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package i18n
import (
. "launchpad.net/gocheck"
)
func (s *MySuite) TestLen(c *C) {
i := new(i18nSorter)
i.toBeSorted = []interface{}{"a", "b", "c"}
c.Check(i.Len(), Equals, 3)
}
func (s *MySuite) TestSwap(c *C) {
i := new(i18nSorter)
i.toBeSorted = []interface{}{"a", "b", "c"}
i.Swap(0, 2)
c.Check(i.toBeSorted[0], Equals, "c")
c.Check(i.toBeSorted[1], Equals, "b")
c.Check(i.toBeSorted[2], Equals, "a")
}
func (s *MySuite) TestLess(c *C) {
i := new(i18nSorter)
i.toBeSorted = []interface{}{"a", "b", "c"}
i.getComparisonValueFunc = func(n interface{}) string {
if s, ok := n.(string); ok {
return s
}
return ""
}
c.Check(i.Less(0, 0), Equals, false)
c.Check(i.Less(0, 1), Equals, true)
c.Check(i.Less(0, 2), Equals, true)
c.Check(i.Less(1, 0), Equals, false)
c.Check(i.Less(1, 1), Equals, false)
c.Check(i.Less(1, 2), Equals, true)
c.Check(i.Less(2, 0), Equals, false)
c.Check(i.Less(2, 1), Equals, false)
c.Check(i.Less(2, 2), Equals, false)
}
func (s *MySuite) TestSortUniversal(c *C) {
toBeSorted := []interface{}{"apple", "beet", "carrot", "ȧpricot", "ḃanana", "ċlementine"}
getComparisonValueFunc := func(i interface{}) string {
if s, ok := i.(string); ok {
return s
}
return ""
}
SortUniversal(toBeSorted, getComparisonValueFunc)
c.Assert(toBeSorted, HasLen, 6)
c.Check(toBeSorted[0], Equals, "apple")
c.Check(toBeSorted[1], Equals, "ȧpricot")
c.Check(toBeSorted[2], Equals, "beet") // unicode normalization puts "b" before "ḃ"
c.Check(toBeSorted[3], Equals, "ḃanana")
c.Check(toBeSorted[4], Equals, "carrot")
c.Check(toBeSorted[5], Equals, "ċlementine")
}
func (s *MySuite) TestSortLocal(c *C) {
toBeSorted := []interface{}{"apple", "beet", "carrot", "ȧpricot", "ḃanana", "ċlementine"}
getComparisonValueFunc := func(i interface{}) string {
if s, ok := i.(string); ok {
return s
}
return ""
}
SortLocal("en", toBeSorted, getComparisonValueFunc)
c.Assert(toBeSorted, HasLen, 6)
c.Check(toBeSorted[0], Equals, "apple")
c.Check(toBeSorted[1], Equals, "ȧpricot")
c.Check(toBeSorted[2], Equals, "ḃanana")
c.Check(toBeSorted[3], Equals, "beet")
c.Check(toBeSorted[4], Equals, "carrot")
c.Check(toBeSorted[5], Equals, "ċlementine")
}
func (s *MySuite) TestSort(c *C) {
f, errors := NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
c.Assert(f, NotNil)
c.Check(errors, HasLen, 0)
// there will be a collator for "en", so SortLocal will be used
tEn, _ := f.GetTranslator("en")
toBeSorted := []interface{}{"apple", "beet", "carrot", "ȧpricot", "ḃanana", "ċlementine"}
getComparisonValueFunc := func(i interface{}) string {
if s, ok := i.(string); ok {
return s
}
return ""
}
tEn.Sort(toBeSorted, getComparisonValueFunc)
c.Assert(toBeSorted, HasLen, 6)
c.Check(toBeSorted[0], Equals, "apple")
c.Check(toBeSorted[1], Equals, "ȧpricot")
c.Check(toBeSorted[2], Equals, "ḃanana")
c.Check(toBeSorted[3], Equals, "beet")
c.Check(toBeSorted[4], Equals, "carrot")
c.Check(toBeSorted[5], Equals, "ċlementine")
// there will not be a collator for "does-not-exist", so SortUniversal will be used
tWhat, _ := f.GetTranslator("does-not-exist")
tWhat.Sort(toBeSorted, getComparisonValueFunc)
c.Assert(toBeSorted, HasLen, 6)
c.Check(toBeSorted[0], Equals, "apple")
c.Check(toBeSorted[1], Equals, "ȧpricot")
c.Check(toBeSorted[2], Equals, "beet") // unicode normalization puts "b" before "ḃ"
c.Check(toBeSorted[3], Equals, "ḃanana")
c.Check(toBeSorted[4], Equals, "carrot")
c.Check(toBeSorted[5], Equals, "ċlementine")
}