|
| 1 | +package patch_test |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + |
| 7 | + . "github.com/cppforlife/go-patch" |
| 8 | +) |
| 9 | + |
| 10 | +var _ = Describe("NewOpsFromDefinitions", func() { |
| 11 | + var ( |
| 12 | + path = "/abc" |
| 13 | + invalidPath = "abc" |
| 14 | + val interface{} = 123 |
| 15 | + ) |
| 16 | + |
| 17 | + It("supports 'replace' and 'remove' operations", func() { |
| 18 | + opDefs := []OpDefinition{ |
| 19 | + {Type: "replace", Path: &path, Value: &val}, |
| 20 | + {Type: "remove", Path: &path}, |
| 21 | + } |
| 22 | + |
| 23 | + ops, err := NewOpsFromDefinitions(opDefs) |
| 24 | + Expect(err).ToNot(HaveOccurred()) |
| 25 | + |
| 26 | + Expect(ops).To(Equal(Ops([]Op{ |
| 27 | + ReplaceOp{Path: MustNewPointerFromString("/abc"), Value: 123}, |
| 28 | + RemoveOp{Path: MustNewPointerFromString("/abc")}, |
| 29 | + }))) |
| 30 | + }) |
| 31 | + |
| 32 | + It("returns error if operation type is unknown", func() { |
| 33 | + _, err := NewOpsFromDefinitions([]OpDefinition{{Type: "test"}}) |
| 34 | + Expect(err).To(HaveOccurred()) |
| 35 | + Expect(err.Error()).To(Equal("Unknown operation [0] with type 'test'")) |
| 36 | + }) |
| 37 | + |
| 38 | + Describe("replace", func() { |
| 39 | + It("requires path", func() { |
| 40 | + _, err := NewOpsFromDefinitions([]OpDefinition{{Type: "replace"}}) |
| 41 | + Expect(err).To(HaveOccurred()) |
| 42 | + Expect(err.Error()).To(Equal("Replace operation [0]: Missing path")) |
| 43 | + }) |
| 44 | + |
| 45 | + It("requires value", func() { |
| 46 | + _, err := NewOpsFromDefinitions([]OpDefinition{{Type: "replace", Path: &path}}) |
| 47 | + Expect(err).To(HaveOccurred()) |
| 48 | + Expect(err.Error()).To(Equal("Replace operation [0]: Missing value")) |
| 49 | + }) |
| 50 | + |
| 51 | + It("requires valid path", func() { |
| 52 | + _, err := NewOpsFromDefinitions([]OpDefinition{{Type: "replace", Path: &invalidPath, Value: &val}}) |
| 53 | + Expect(err).To(HaveOccurred()) |
| 54 | + Expect(err.Error()).To(ContainSubstring("Replace operation [0]: Invalid path: Expected to start with '/'")) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + Describe("remove", func() { |
| 59 | + It("requires path", func() { |
| 60 | + _, err := NewOpsFromDefinitions([]OpDefinition{{Type: "remove"}}) |
| 61 | + Expect(err).To(HaveOccurred()) |
| 62 | + Expect(err.Error()).To(Equal("Remove operation [0]: Missing path")) |
| 63 | + }) |
| 64 | + |
| 65 | + It("does not allow value", func() { |
| 66 | + _, err := NewOpsFromDefinitions([]OpDefinition{{Type: "remove", Path: &path, Value: &val}}) |
| 67 | + Expect(err).To(HaveOccurred()) |
| 68 | + Expect(err.Error()).To(Equal("Remove operation [0]: Cannot specify value")) |
| 69 | + }) |
| 70 | + |
| 71 | + It("requires valid path", func() { |
| 72 | + _, err := NewOpsFromDefinitions([]OpDefinition{{Type: "remove", Path: &invalidPath}}) |
| 73 | + Expect(err).To(HaveOccurred()) |
| 74 | + Expect(err.Error()).To(ContainSubstring("Remove operation [0]: Invalid path: Expected to start with '/'")) |
| 75 | + }) |
| 76 | + }) |
| 77 | +}) |
0 commit comments