-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
context_test.go
119 lines (100 loc) · 2.14 KB
/
context_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
package dasel
import (
"errors"
"reflect"
"testing"
)
func sameSlice(x, y []interface{}) bool {
if len(x) != len(y) {
return false
}
if reflect.DeepEqual(x, y) {
return true
}
// Test for equality ignoring ordering
diff := make([]interface{}, len(y))
copy(diff, y)
for _, xv := range x {
for di, dv := range diff {
if reflect.DeepEqual(xv, dv) {
diff = append(diff[0:di], diff[di+1:]...)
break
}
}
}
return len(diff) == 0
}
func selectTest(selector string, original interface{}, exp []interface{}) func(t *testing.T) {
return func(t *testing.T) {
c := newSelectContext(original, selector)
values, err := c.Run()
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
got := values.Interfaces()
if !sameSlice(exp, got) {
t.Errorf("expected %v, got %v", exp, got)
return
}
}
}
func selectTestAssert(selector string, original interface{}, assertFn func(t *testing.T, got []any)) func(t *testing.T) {
return func(t *testing.T) {
c := newSelectContext(original, selector)
values, err := c.Run()
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
got := values.Interfaces()
assertFn(t, got)
}
}
func selectTestErr(selector string, original interface{}, expErr error) func(t *testing.T) {
return func(t *testing.T) {
c := newSelectContext(original, selector)
_, err := c.Run()
if !errors.Is(err, expErr) {
t.Errorf("expected error: %v, got %v", expErr, err)
return
}
}
}
func TestContext_Step(t *testing.T) {
step1 := &Step{index: 0}
step2 := &Step{index: 1}
c := &Context{
steps: []*Step{
step1, step2,
},
}
expSteps := map[int]*Step{
-1: nil,
0: step1,
1: step2,
2: nil,
}
for index, exp := range expSteps {
got := c.Step(index)
if exp != got {
t.Errorf("expected %v, got %v", exp, got)
}
}
}
func TestContext_WithMetadata(t *testing.T) {
c := (&Context{}).
WithMetadata("x", 1).
WithMetadata("y", 2)
expMetadata := map[string]interface{}{
"x": 1,
"y": 2,
"z": nil,
}
for index, exp := range expMetadata {
got := c.Metadata(index)
if exp != got {
t.Errorf("expected %v, got %v", exp, got)
}
}
}