-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-recursive-sort_test.go
113 lines (107 loc) · 2.75 KB
/
go-recursive-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
package recursivesort
import (
"testing"
"reflect"
)
func recSortWithOrder(order ...interface{}) *RecursiveSort {
priorityLookup := TypePriorityLookup{}.FromValues(order...)
return &RecursiveSort{
TypePriorityLookupHelper: priorityLookup,
}
}
func TestAssumptions(t *testing.T) {
t.Parallel()
for _, c := range []struct {
a, b interface{}
equal bool
}{
{map[string]int{"b": 1, "a": 2}, map[string]int{"a": 2, "b": 1}, true},
} {
if equal := reflect.DeepEqual(c.a, c.b); equal != c.equal {
t.Errorf("expected reflect.DeepEqual(%s, %s) == %t", c.a, c.b, c.equal)
}
}
}
func TestSortSliceOfUnexportedStruct(t *testing.T) {
t.Parallel()
sort := &RecursiveSort{
StructSortField: "Exported",
}
type TestStruct struct {
unexported string
// Exported string
Exported int64
}
value := []interface{}{
TestStruct{
unexported: "test",
Exported: 0,
},
TestStruct{
unexported: "test",
Exported: 1,
},
}
t.Logf("before: %v", value)
sort.Sort(&value)
t.Logf("after: %v", value)
}
func TestRecSort(t *testing.T) {
t.Parallel()
defaultSort := &RecursiveSort{}
for _, c := range []struct {
sort *RecursiveSort
input interface{}
expected interface{}
}{
{defaultSort, []string{"a", "c", "b"}, []string{"a", "b", "c"}},
{
defaultSort,
struct{ Nested []string }{Nested: []string{"a", "c", "b"}},
struct{ Nested []string }{Nested: []string{"a", "b", "c"}},
},
{defaultSort, []interface{}{"a", "c", "b"}, []interface{}{"a", "b", "c"}},
{
recSortWithOrder("", int(0), uint32(0)),
[]interface{}{"a", 1, uint32(2)},
[]interface{}{"a", 1, uint32(2)},
},
{
recSortWithOrder(int(0), uint32(0), ""),
[]interface{}{"a", 1, uint32(2)},
[]interface{}{1, uint32(2), "a"},
},
{
defaultSort, // int < string < uint32
[]interface{}{"a", 1, uint32(2)},
[]interface{}{1, "a", uint32(2)},
},
{
recSortWithOrder("", map[string]int{}),
[]interface{}{"c", "a", map[string]int{"b": 1, "a": 2}},
[]interface{}{"a", "c", map[string]int{"a": 2, "b": 1}},
},
{
recSortWithOrder(map[string]int{}, ""),
[]interface{}{"c", "a", map[string]int{"b": 1, "a": 2}},
[]interface{}{map[string]int{"a": 2, "b": 1}, "a", "c"},
},
{
defaultSort, // does not change
[]interface{}{map[string]int{"a": 2}, map[string]int{"a": 1}},
[]interface{}{map[string]int{"a": 2}, map[string]int{"a": 1}},
},
{
&RecursiveSort{MapSortKey: "a"},
[]interface{}{map[string]int{"a": 2}, map[string]int{"a": 1}},
[]interface{}{map[string]int{"a": 1}, map[string]int{"a": 2}},
},
} {
t.Logf("before: %v", c.input)
c.sort.Sort(c.input)
t.Logf("after: %v", c.input)
if equal := reflect.DeepEqual(c.input, c.expected); !equal {
t.Errorf("expected %v but got %v", c.expected, c.input)
}
}
}