-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathactions_test.go
248 lines (229 loc) · 6.03 KB
/
actions_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* Copyright 2021 Comcast Cable Communications Management, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package core
import (
"context"
"errors"
"testing"
. "github.com/Comcast/sheens/match"
)
var (
// ensures that the InterpretersMap is an Interpreters.
_ Interpreters = InterpretersMap{}
goodAS = &ActionSource{
Interpreter: "test interpreter",
Source: "test source",
Binds: []Bindings{
{"a": "b", "c": 5, "d": false, "e": 1.25},
{"test": true, "binding": true, "two": false},
},
}
errTestCompile = errors.New("test interpreter failed to compile")
)
type testInterpreter struct {
errOnCompile bool
}
func (i *testInterpreter) Compile(_ context.Context, _ interface{}) (interface{}, error) {
if i.errOnCompile {
return nil, errTestCompile
}
return nil, nil
}
func (*testInterpreter) Exec(_ context.Context, _ Bindings, _ StepProps, _ interface{}, _ interface{}) (*Execution, error) {
return nil, nil
}
func TestPermanentBindings(t *testing.T) {
if !Exp_PermanentBindings {
return
}
ctx := context.Background()
action := &FuncAction{
F: func(ctx context.Context, bs Bindings, props StepProps) (*Execution, error) {
return &Execution{
Bs: bs.Remove("ephemeral", "permament!"),
Events: newEvents(),
}, nil
},
}
bs := NewBindings()
bs["ephemeral"] = "queso"
bs["permament!"] = "tacos"
exe, err := action.Exec(ctx, bs.Copy(), nil)
if err != nil {
t.Fatal(err)
}
if _, have := exe.Bs["ephemeral"]; have {
t.Fatal("ephemeral wasn't")
}
if _, have := exe.Bs["permament!"]; !have {
t.Fatal("permament wasn't")
}
}
func TestInterpretersMap(t *testing.T) {
goodInterpreter := &testInterpreter{}
iMap := NewInterpretersMap()
if iMap == nil {
t.Fatalf("NewInterpretersMap should return non-nil value")
}
iMap["good"] = goodInterpreter
iMap["bad"] = nil
tests := []struct {
description string
name string
expectedResult Interpreter
}{
{
description: "Success",
name: "good",
expectedResult: goodInterpreter,
},
{
description: "Nil key",
name: "bad",
expectedResult: nil,
},
{
description: "Missing key",
name: "missing",
expectedResult: nil,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
r := iMap.Find(tc.name)
if r != tc.expectedResult {
t.Fatalf("expected %v interpreter but received %v",
tc.expectedResult, r)
}
})
}
}
func TestActionSourceCopy(t *testing.T) {
var (
nilAS *ActionSource = nil
)
tests := []struct {
description string
actionsource *ActionSource
expectedCopy *ActionSource
}{
{
description: "Success",
actionsource: goodAS,
expectedCopy: goodAS,
},
{
description: "Empty values",
actionsource: &ActionSource{},
expectedCopy: &ActionSource{},
},
{
description: "Nil action source",
actionsource: nilAS,
expectedCopy: nil,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
copy := tc.actionsource.Copy()
// deal with anything nil first.
if tc.expectedCopy == nil && copy != nil ||
tc.expectedCopy != nil && copy == nil {
t.Fatalf("expected %v copy but received %v", tc.expectedCopy,
copy)
}
if tc.expectedCopy == nil || copy == nil {
return
}
if &tc.expectedCopy == © {
t.Fatalf("expected new address for copy")
}
if tc.expectedCopy.Interpreter != copy.Interpreter ||
tc.expectedCopy.Source != copy.Source ||
len(tc.expectedCopy.Binds) != len(copy.Binds) {
t.Fatalf("copies don't match; expected %v but received %v",
tc.expectedCopy, copy)
}
// is this too much? we can remove it.
for i, b := range tc.expectedCopy.Binds {
if len(b) != len(copy.Binds[i]) {
t.Fatalf("copies don't match; expected %v but received %v",
tc.expectedCopy, copy)
}
for k, v := range b {
if v != copy.Binds[i][k] {
t.Fatalf("copies don't match; expected %v but received %v",
tc.expectedCopy, copy)
}
}
}
})
}
}
func TestActionSourceCompile(t *testing.T) {
interpreters := InterpretersMap{
"good": &testInterpreter{},
"compile issues": &testInterpreter{errOnCompile: true},
}
tests := []struct {
description string
actionsource *ActionSource
interpreters Interpreters
expectedErr error
}{
{
description: "Success",
actionsource: &ActionSource{
Interpreter: "good",
Binds: goodAS.Binds,
},
interpreters: interpreters,
},
{
description: "Not found error",
actionsource: &ActionSource{Interpreter: "nope"},
interpreters: interpreters,
expectedErr: InterpreterNotFound,
},
{
description: "Not found error with default interpreters",
actionsource: &ActionSource{Interpreter: "good"},
expectedErr: InterpreterNotFound,
},
{
description: "Compile error",
actionsource: &ActionSource{Interpreter: "compile issues"},
interpreters: interpreters,
expectedErr: errTestCompile,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
action, err := tc.actionsource.Compile(context.Background(), tc.interpreters)
if tc.expectedErr != err {
t.Fatalf("expected error %v but received %v", tc.expectedErr, err)
}
if tc.expectedErr != nil {
if action != nil {
t.Fatalf("expected nil action but received %v", action)
}
return
}
if action == nil {
t.Fatalf("expected non-nil action")
}
if len(tc.actionsource.Binds) != len(action.Binds()) {
t.Errorf("expected binds %v but received %v", tc.actionsource.Binds, action.Binds())
}
})
}
}