Skip to content

Commit 7dbf718

Browse files
committed
tests: Refine tests for sortObjectsByKeysAsc and *Desc
* Ensure that `sortObjectsByKeysAsc` and `sortObjectsByKeysDesc` do not mutate the input slice. * Be more strict when checking the sorted results. * Improve readability and reduce duplication by converting to idiomatic table-driven tests.
1 parent ffa6d17 commit 7dbf718

File tree

1 file changed

+38
-74
lines changed

1 file changed

+38
-74
lines changed

internal/template/sort_test.go

Lines changed: 38 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,84 +19,48 @@ func TestSortStringsDesc(t *testing.T) {
1919
assert.Equal(t, expected, sortStringsDesc(strings))
2020
}
2121

22-
func TestSortObjectsByKeysAsc(t *testing.T) {
23-
containers := []*context.RuntimeContainer{
24-
{
25-
Env: map[string]string{
26-
"VIRTUAL_HOST": "bar.localhost",
27-
},
28-
ID: "9",
29-
},
30-
{
31-
Env: map[string]string{
32-
"VIRTUAL_HOST": "foo.localhost",
33-
},
34-
ID: "1",
35-
},
36-
{
37-
Env: map[string]string{
38-
"VIRTUAL_HOST": "baz.localhost",
39-
},
40-
ID: "3",
41-
},
42-
{
43-
Env: map[string]string{},
44-
ID: "8",
22+
func TestSortObjectsByKeys(t *testing.T) {
23+
o0 := &context.RuntimeContainer{
24+
Env: map[string]string{
25+
"VIRTUAL_HOST": "bar.localhost",
4526
},
27+
ID: "9",
4628
}
47-
48-
sorted, err := sortObjectsByKeysAsc(containers, "ID")
49-
50-
assert.NoError(t, err)
51-
assert.Len(t, sorted, 4)
52-
assert.Equal(t, "foo.localhost", sorted[0].(*context.RuntimeContainer).Env["VIRTUAL_HOST"])
53-
assert.Equal(t, "9", sorted[3].(*context.RuntimeContainer).ID)
54-
55-
sorted, err = sortObjectsByKeysAsc(sorted, "Env.VIRTUAL_HOST")
56-
57-
assert.NoError(t, err)
58-
assert.Len(t, sorted, 4)
59-
assert.Equal(t, "foo.localhost", sorted[3].(*context.RuntimeContainer).Env["VIRTUAL_HOST"])
60-
assert.Equal(t, "8", sorted[0].(*context.RuntimeContainer).ID)
61-
}
62-
63-
func TestSortObjectsByKeysDesc(t *testing.T) {
64-
containers := []*context.RuntimeContainer{
65-
{
66-
Env: map[string]string{
67-
"VIRTUAL_HOST": "bar.localhost",
68-
},
69-
ID: "9",
29+
o1 := &context.RuntimeContainer{
30+
Env: map[string]string{
31+
"VIRTUAL_HOST": "foo.localhost",
7032
},
71-
{
72-
Env: map[string]string{
73-
"VIRTUAL_HOST": "foo.localhost",
74-
},
75-
ID: "1",
76-
},
77-
{
78-
Env: map[string]string{
79-
"VIRTUAL_HOST": "baz.localhost",
80-
},
81-
ID: "3",
82-
},
83-
{
84-
Env: map[string]string{},
85-
ID: "8",
33+
ID: "1",
34+
}
35+
o2 := &context.RuntimeContainer{
36+
Env: map[string]string{
37+
"VIRTUAL_HOST": "baz.localhost",
8638
},
39+
ID: "3",
8740
}
41+
o3 := &context.RuntimeContainer{
42+
Env: map[string]string{},
43+
ID: "8",
44+
}
45+
containers := []*context.RuntimeContainer{o0, o1, o2, o3}
8846

89-
sorted, err := sortObjectsByKeysDesc(containers, "ID")
90-
91-
assert.NoError(t, err)
92-
assert.Len(t, sorted, 4)
93-
assert.Equal(t, "bar.localhost", sorted[0].(*context.RuntimeContainer).Env["VIRTUAL_HOST"])
94-
assert.Equal(t, "1", sorted[3].(*context.RuntimeContainer).ID)
95-
96-
sorted, err = sortObjectsByKeysDesc(sorted, "Env.VIRTUAL_HOST")
97-
98-
assert.NoError(t, err)
99-
assert.Len(t, sorted, 4)
100-
assert.Equal(t, "", sorted[3].(*context.RuntimeContainer).Env["VIRTUAL_HOST"])
101-
assert.Equal(t, "1", sorted[0].(*context.RuntimeContainer).ID)
47+
for _, tc := range []struct {
48+
desc string
49+
fn func(interface{}, string) ([]interface{}, error)
50+
key string
51+
want []interface{}
52+
}{
53+
{"Asc simple", sortObjectsByKeysAsc, "ID", []interface{}{o1, o2, o3, o0}},
54+
{"Asc complex", sortObjectsByKeysAsc, "Env.VIRTUAL_HOST", []interface{}{o3, o0, o2, o1}},
55+
{"Desc simple", sortObjectsByKeysDesc, "ID", []interface{}{o0, o3, o2, o1}},
56+
{"Desc complex", sortObjectsByKeysDesc, "Env.VIRTUAL_HOST", []interface{}{o1, o2, o0, o3}},
57+
} {
58+
t.Run(tc.desc, func(t *testing.T) {
59+
got, err := tc.fn(containers, tc.key)
60+
assert.NoError(t, err)
61+
// The function should return a sorted copy of the slice, not modify the original.
62+
assert.Equal(t, []*context.RuntimeContainer{o0, o1, o2, o3}, containers)
63+
assert.Equal(t, tc.want, got)
64+
})
65+
}
10266
}

0 commit comments

Comments
 (0)