1
1
package patch_test
2
2
3
3
import (
4
+ "encoding/json"
5
+
4
6
. "github.com/onsi/ginkgo"
5
7
. "github.com/onsi/gomega"
8
+ "gopkg.in/yaml.v2"
6
9
7
10
. "github.com/cppforlife/go-patch/patch"
8
11
)
@@ -14,12 +17,15 @@ var _ = Describe("NewOpsFromDefinitions", func() {
14
17
errorMsg = "error"
15
18
val interface {} = 123
16
19
complexVal interface {} = map [interface {}]interface {}{123 : 123 }
20
+ trueBool = true
17
21
)
18
22
19
- It ("supports 'replace' and 'remove' operations" , func () {
23
+ It ("supports 'replace', 'remove', 'test ' operations" , func () {
20
24
opDefs := []OpDefinition {
21
25
{Type : "replace" , Path : & path , Value : & val },
22
26
{Type : "remove" , Path : & path },
27
+ {Type : "test" , Path : & path , Value : & val },
28
+ {Type : "test" , Path : & path , Absent : & trueBool },
23
29
}
24
30
25
31
ops , err := NewOpsFromDefinitions (opDefs )
@@ -28,15 +34,17 @@ var _ = Describe("NewOpsFromDefinitions", func() {
28
34
Expect (ops ).To (Equal (Ops ([]Op {
29
35
ReplaceOp {Path : MustNewPointerFromString ("/abc" ), Value : 123 },
30
36
RemoveOp {Path : MustNewPointerFromString ("/abc" )},
37
+ TestOp {Path : MustNewPointerFromString ("/abc" ), Value : 123 },
38
+ TestOp {Path : MustNewPointerFromString ("/abc" ), Absent : true },
31
39
})))
32
40
})
33
41
34
42
It ("returns error if operation type is unknown" , func () {
35
- _ , err := NewOpsFromDefinitions ([]OpDefinition {{Type : "test " }})
43
+ _ , err := NewOpsFromDefinitions ([]OpDefinition {{Type : "op " }})
36
44
Expect (err ).To (HaveOccurred ())
37
- Expect (err .Error ()).To (Equal (`Unknown operation [0] with type 'test ' within
45
+ Expect (err .Error ()).To (Equal (`Unknown operation [0] with type 'op ' within
38
46
{
39
- "Type": "test "
47
+ "Type": "op "
40
48
}` ))
41
49
})
42
50
@@ -47,11 +55,11 @@ var _ = Describe("NewOpsFromDefinitions", func() {
47
55
})
48
56
49
57
It ("allows values to be complex in error messages" , func () {
50
- _ , err := NewOpsFromDefinitions ([]OpDefinition {{Type : "test " , Path : & invalidPath , Value : & complexVal }})
58
+ _ , err := NewOpsFromDefinitions ([]OpDefinition {{Type : "op " , Path : & invalidPath , Value : & complexVal }})
51
59
Expect (err ).To (HaveOccurred ())
52
- Expect (err .Error ()).To (Equal (`Unknown operation [0] with type 'test ' within
60
+ Expect (err .Error ()).To (Equal (`Unknown operation [0] with type 'op ' within
53
61
{
54
- "Type": "test ",
62
+ "Type": "op ",
55
63
"Path": "abc",
56
64
"Value": "<redacted>"
57
65
}` ))
@@ -148,4 +156,104 @@ var _ = Describe("NewOpsFromDefinitions", func() {
148
156
}` ))
149
157
})
150
158
})
159
+
160
+ Describe ("test" , func () {
161
+ It ("allows error description" , func () {
162
+ opDefs := []OpDefinition {{Type : "test" , Path : & path , Value : & val , Error : & errorMsg }}
163
+
164
+ ops , err := NewOpsFromDefinitions (opDefs )
165
+ Expect (err ).ToNot (HaveOccurred ())
166
+
167
+ Expect (ops ).To (Equal (Ops ([]Op {
168
+ DescriptiveOp {
169
+ Op : TestOp {Path : MustNewPointerFromString ("/abc" ), Value : 123 },
170
+ ErrorMsg : errorMsg ,
171
+ },
172
+ })))
173
+ })
174
+
175
+ It ("requires path" , func () {
176
+ _ , err := NewOpsFromDefinitions ([]OpDefinition {{Type : "test" }})
177
+ Expect (err ).To (HaveOccurred ())
178
+ Expect (err .Error ()).To (Equal (`Test operation [0]: Missing path within
179
+ {
180
+ "Type": "test"
181
+ }` ))
182
+ })
183
+
184
+ It ("requires value or absent flag" , func () {
185
+ _ , err := NewOpsFromDefinitions ([]OpDefinition {{Type : "test" , Path : & path }})
186
+ Expect (err ).To (HaveOccurred ())
187
+ Expect (err .Error ()).To (Equal (`Test operation [0]: Missing value or absent within
188
+ {
189
+ "Type": "test",
190
+ "Path": "/abc"
191
+ }` ))
192
+ })
193
+
194
+ It ("requires valid path" , func () {
195
+ _ , err := NewOpsFromDefinitions ([]OpDefinition {{Type : "test" , Path : & invalidPath , Value : & val }})
196
+ Expect (err ).To (HaveOccurred ())
197
+ Expect (err .Error ()).To (Equal (`Test operation [0]: Invalid path: Expected to start with '/' within
198
+ {
199
+ "Type": "test",
200
+ "Path": "abc",
201
+ "Value": "<redacted>"
202
+ }` ))
203
+ })
204
+ })
205
+ })
206
+
207
+ var _ = Describe ("NewOpDefinitionsFromOps" , func () {
208
+ It ("supports 'replace', 'remove', 'test' operations serialized" , func () {
209
+ ops := Ops ([]Op {
210
+ ReplaceOp {Path : MustNewPointerFromString ("/abc" ), Value : 123 },
211
+ RemoveOp {Path : MustNewPointerFromString ("/abc" )},
212
+ TestOp {Path : MustNewPointerFromString ("/abc" ), Value : 123 },
213
+ TestOp {Path : MustNewPointerFromString ("/abc" ), Absent : true },
214
+ })
215
+
216
+ opDefs , err := NewOpDefinitionsFromOps (ops )
217
+ Expect (err ).ToNot (HaveOccurred ())
218
+
219
+ bs , err := yaml .Marshal (opDefs )
220
+ Expect (err ).ToNot (HaveOccurred ())
221
+
222
+ Expect ("\n " + string (bs )).To (Equal (`
223
+ - type: replace
224
+ path: /abc
225
+ value: 123
226
+ - type: remove
227
+ path: /abc
228
+ - type: test
229
+ path: /abc
230
+ value: 123
231
+ - type: test
232
+ path: /abc
233
+ absent: true
234
+ ` ))
235
+
236
+ bs , err = json .MarshalIndent (opDefs , "" , " " )
237
+ Expect (string (bs )).To (Equal (`[
238
+ {
239
+ "Type": "replace",
240
+ "Path": "/abc",
241
+ "Value": 123
242
+ },
243
+ {
244
+ "Type": "remove",
245
+ "Path": "/abc"
246
+ },
247
+ {
248
+ "Type": "test",
249
+ "Path": "/abc",
250
+ "Value": 123
251
+ },
252
+ {
253
+ "Type": "test",
254
+ "Path": "/abc",
255
+ "Absent": true
256
+ }
257
+ ]` ))
258
+ })
151
259
})
0 commit comments