-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhook_test.go
More file actions
108 lines (101 loc) · 2.24 KB
/
hook_test.go
File metadata and controls
108 lines (101 loc) · 2.24 KB
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
package lu
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSortHooks(t *testing.T) {
testCases := []struct {
name string
hooks []hook
expSorted []hook
}{
{
name: "by order",
hooks: []hook{
{Name: "c", createOrder: 3},
{Name: "a", createOrder: 1},
{Name: "b", createOrder: 2},
},
expSorted: []hook{
{Name: "a", createOrder: 1},
{Name: "b", createOrder: 2},
{Name: "c", createOrder: 3},
},
},
{
name: "by priority",
hooks: []hook{
{Priority: 2},
{Priority: 3},
{Priority: 1},
},
expSorted: []hook{
{Priority: 1},
{Priority: 2},
{Priority: 3},
},
},
{
name: "by both",
hooks: []hook{
{createOrder: 1, Priority: HookPriorityDefault},
{createOrder: 2, Priority: HookPriorityDefault},
{createOrder: 3, Priority: HookPriorityLast},
{createOrder: 4, Priority: HookPriorityDefault},
{createOrder: 5, Priority: HookPriorityFirst},
},
expSorted: []hook{
{createOrder: 5, Priority: HookPriorityFirst},
{createOrder: 1, Priority: HookPriorityDefault},
{createOrder: 2, Priority: HookPriorityDefault},
{createOrder: 4, Priority: HookPriorityDefault},
{createOrder: 3, Priority: HookPriorityLast},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
sortedHooks := make([]hook, len(tc.hooks))
copy(sortedHooks, tc.hooks)
sortHooks(sortedHooks)
assert.Equal(t, tc.expSorted, sortedHooks)
})
}
}
func TestOptions(t *testing.T) {
testCases := []struct {
name string
options []HookOption
expHook hook
}{
{
name: "defaults",
expHook: hook{Priority: HookPriorityDefault},
},
{
name: "name",
options: []HookOption{WithHookName("test name")},
expHook: hook{Name: "test name", Priority: HookPriorityDefault},
},
{
name: "priority",
options: []HookOption{WithHookPriority(23)},
expHook: hook{Priority: 23},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var h hook
applyHookOptions(&h, tc.options)
assert.Equal(t, tc.expHook, h)
})
}
}
func TestPriorityPanic(t *testing.T) {
assert.Panics(t, func() {
WithHookPriority(-101)
})
assert.Panics(t, func() {
WithHookPriority(101)
})
}