-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathsort_test.go
102 lines (94 loc) · 3.04 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
package template
import (
"testing"
"time"
"github.com/nginx-proxy/docker-gen/internal/context"
"github.com/stretchr/testify/assert"
)
func TestSortStringsAsc(t *testing.T) {
strings := []string{"foo", "bar", "baz", "qux"}
expected := []string{"bar", "baz", "foo", "qux"}
assert.Equal(t, expected, sortStringsAsc(strings))
}
func TestSortStringsDesc(t *testing.T) {
strings := []string{"foo", "bar", "baz", "qux"}
expected := []string{"qux", "foo", "baz", "bar"}
assert.Equal(t, expected, sortStringsDesc(strings))
}
func TestGetFieldAsString(t *testing.T) {
testStruct := struct {
String string
BoolT bool
BoolF bool
Int int
Int32 int32
Int64 int64
Time time.Time
}{
String: "foo",
BoolT: true,
BoolF: false,
Int: 42,
Int32: 43,
Int64: 44,
Time: time.Date(2023, 12, 19, 0, 0, 0, 0, time.UTC),
}
assert.Equal(t, "foo", getFieldAsString(testStruct, "String"))
assert.Equal(t, "true", getFieldAsString(testStruct, "BoolT"))
assert.Equal(t, "false", getFieldAsString(testStruct, "BoolF"))
assert.Equal(t, "42", getFieldAsString(testStruct, "Int"))
assert.Equal(t, "43", getFieldAsString(testStruct, "Int32"))
assert.Equal(t, "44", getFieldAsString(testStruct, "Int64"))
assert.Equal(t, "2023-12-19 00:00:00 +0000 UTC", getFieldAsString(testStruct, "Time"))
assert.Equal(t, "", getFieldAsString(testStruct, "InvalidField"))
}
func TestSortObjectsByKeys(t *testing.T) {
o0 := &context.RuntimeContainer{
Created: time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC),
Env: map[string]string{
"VIRTUAL_HOST": "bar.localhost",
},
ID: "9",
}
o1 := &context.RuntimeContainer{
Created: time.Date(2021, 1, 2, 0, 0, 10, 0, time.UTC),
Env: map[string]string{
"VIRTUAL_HOST": "foo.localhost",
},
ID: "1",
}
o2 := &context.RuntimeContainer{
Created: time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC),
Env: map[string]string{
"VIRTUAL_HOST": "baz.localhost",
},
ID: "3",
}
o3 := &context.RuntimeContainer{
Created: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
Env: map[string]string{},
ID: "8",
}
containers := []*context.RuntimeContainer{o0, o1, o2, o3}
for _, tc := range []struct {
desc string
fn func(interface{}, string) ([]interface{}, error)
key string
want []interface{}
}{
{"Asc simple", sortObjectsByKeysAsc, "ID", []interface{}{o1, o2, o3, o0}},
{"Asc complex", sortObjectsByKeysAsc, "Env.VIRTUAL_HOST", []interface{}{o3, o0, o2, o1}},
{"Desc simple", sortObjectsByKeysDesc, "ID", []interface{}{o0, o3, o2, o1}},
{"Desc complex", sortObjectsByKeysDesc, "Env.VIRTUAL_HOST", []interface{}{o1, o2, o0, o3}},
{"Asc time", sortObjectsByKeysAsc, "Created", []interface{}{o3, o0, o2, o1}},
{"Desc time", sortObjectsByKeysDesc, "Created", []interface{}{o1, o2, o0, o3}},
} {
t.Run(tc.desc, func(t *testing.T) {
got, err := tc.fn(containers, tc.key)
assert.NoError(t, err)
// The function should return a sorted copy of the slice, not modify the original.
assert.Equal(t, []*context.RuntimeContainer{o0, o1, o2, o3}, containers)
assert.Equal(t, tc.want, got)
})
}
}