-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorder_test.go
146 lines (131 loc) · 4.26 KB
/
order_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package wildcat
import "testing"
func parseUtil(str string) *Order {
order, _ := ParseOrder(str)
return order
}
func checkIndex(t *testing.T, orig, order *Order, index, wontValue int) {
if order.index != wontValue {
t.Errorf("%s.toSlice did not match, wont orders[%d] is %d, got %d", orig.String(), index, wontValue, order.index)
}
}
func TestToSlice(t *testing.T) {
order1 := parseUtil("1.2.3.4.5")
if order1.depth() != 5 {
t.Errorf("\"%s\".depth() did not match, wont 5, got %d", order1.String(), order1.depth())
}
orders1 := order1.toSlice()
checkIndex(t, order1, orders1[0], 0, 1)
checkIndex(t, order1, orders1[1], 1, 2)
checkIndex(t, order1, orders1[2], 2, 3)
checkIndex(t, order1, orders1[3], 3, 4)
checkIndex(t, order1, orders1[4], 4, 5)
}
func checkCompare(t *testing.T, order1, order2 *Order, wont int) {
result := order1.Compare(order2)
if result != wont {
t.Errorf("\"%s\".Compare(\"%s\") did not match, wont %d, got %d", order1.String(), order2.String(), wont, result)
}
}
func TestCompare(t *testing.T) {
orders := []*Order{
parseUtil("1"), // 0
parseUtil("2"), // 1
parseUtil("1.1"), // 2
parseUtil("1.3"), // 3
parseUtil("3.4"), // 4
parseUtil("3.4.1"), // 5
}
checkCompare(t, orders[0], orders[0], 0)
checkCompare(t, orders[0], orders[1], -1)
checkCompare(t, orders[0], orders[2], -1)
checkCompare(t, orders[0], orders[3], -1)
checkCompare(t, orders[0], orders[4], -1)
checkCompare(t, orders[0], orders[5], -1)
checkCompare(t, orders[1], orders[0], 1)
checkCompare(t, orders[1], orders[1], 0)
checkCompare(t, orders[1], orders[2], 1)
checkCompare(t, orders[1], orders[3], 1)
checkCompare(t, orders[1], orders[4], -1)
checkCompare(t, orders[1], orders[5], -1)
checkCompare(t, orders[2], orders[0], 1)
checkCompare(t, orders[2], orders[1], -1)
checkCompare(t, orders[2], orders[2], 0)
checkCompare(t, orders[2], orders[3], -1)
checkCompare(t, orders[2], orders[4], -1)
checkCompare(t, orders[2], orders[5], -1)
checkCompare(t, orders[3], orders[0], 1)
checkCompare(t, orders[3], orders[1], -1)
checkCompare(t, orders[3], orders[2], 1)
checkCompare(t, orders[3], orders[3], 0)
checkCompare(t, orders[3], orders[4], -1)
checkCompare(t, orders[3], orders[5], -1)
checkCompare(t, orders[4], orders[0], 1)
checkCompare(t, orders[4], orders[1], 1)
checkCompare(t, orders[4], orders[2], 1)
checkCompare(t, orders[4], orders[3], 1)
checkCompare(t, orders[4], orders[4], 0)
checkCompare(t, orders[4], orders[5], -1)
checkCompare(t, orders[5], orders[0], 1)
checkCompare(t, orders[5], orders[1], 1)
checkCompare(t, orders[5], orders[2], 1)
checkCompare(t, orders[5], orders[3], 1)
checkCompare(t, orders[5], orders[4], 1)
checkCompare(t, orders[5], orders[5], 0)
}
func TestParsedOrder(t *testing.T) {
order, _ := ParseOrder("3.2.1")
next := order.Next()
str := next.String()
if str != "3.2.2" {
t.Errorf("next.String() did not match, wont \"3.2.2\", but got \"%s\"", str)
}
if order.Compare(next) > 1 {
t.Errorf("compare failed, wont %d, got %d", -1, order.Compare(next))
}
}
func TestOrderNext(t *testing.T) {
order := NewOrder()
if order.index != 0 && order.parent != nil {
t.Errorf("NewOrder did not match, wont index 0, but %d", order.index)
}
next := order.Next()
if next.index != 1 && next.parent != nil {
t.Errorf("next did not match, wont index 1, but %d", next.index)
}
str := next.String()
if str != "1" {
t.Errorf("String did not match, wont \"1\", but got \"%s\"", str)
}
sub := next.Sub()
str2 := sub.String()
if str2 != "1.0" {
t.Errorf("sub.String() did not match, wont \"1.0\", but got \"%s\"", str2)
}
}
func TestOrderString(t *testing.T) {
testdata := []struct {
giveString string
wontString string
wontError bool
}{
{"1.1.1", "1.1.1", false},
{"1.2.3", "1.2.3", false},
{"1.2.a", "", true},
}
for _, td := range testdata {
order, err := ParseOrder(td.giveString)
if td.wontError && err == nil {
t.Errorf("%s: wont error, but got no error", td.giveString)
}
if !td.wontError && err != nil {
t.Errorf("%s: wont no error, but got error (%s)", td.giveString, err.Error())
}
if err != nil {
gotString := order.String()
if gotString != td.wontString {
t.Errorf("%s: got string did not match, wont %s, got %s", td.giveString, td.wontString, gotString)
}
}
}
}