|
| 1 | +package patch_test |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + |
| 7 | + . "github.com/cppforlife/go-patch/patch" |
| 8 | +) |
| 9 | + |
| 10 | +var _ = Describe("Diff.Calculate", func() { |
| 11 | + testDiff := func(left, right interface{}, expectedOps []Op) { |
| 12 | + Expect(Diff{Left: left, Right: right}.Calculate()).To(Equal(Ops(expectedOps))) |
| 13 | + |
| 14 | + result, err := Ops(expectedOps).Apply(left) |
| 15 | + Expect(err).ToNot(HaveOccurred()) |
| 16 | + |
| 17 | + if right == nil { // gomega does not allow nil==nil comparison |
| 18 | + Expect(result).To(BeNil()) |
| 19 | + } else { |
| 20 | + Expect(result).To(Equal(right)) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + It("returns no ops if both docs are same", func() { |
| 25 | + testDiff(nil, nil, []Op{}) |
| 26 | + |
| 27 | + testDiff( |
| 28 | + map[interface{}]interface{}{"a": 124}, |
| 29 | + map[interface{}]interface{}{"a": 124}, |
| 30 | + []Op{}, |
| 31 | + ) |
| 32 | + |
| 33 | + testDiff( |
| 34 | + []interface{}{"a", 124}, |
| 35 | + []interface{}{"a", 124}, |
| 36 | + []Op{}, |
| 37 | + ) |
| 38 | + }) |
| 39 | + |
| 40 | + It("can replace doc root", func() { |
| 41 | + testDiff(nil, "a", []Op{ReplaceOp{Path: MustNewPointerFromString(""), Value: "a"}}) |
| 42 | + }) |
| 43 | + |
| 44 | + It("can replace doc root with nil", func() { |
| 45 | + testDiff("a", nil, []Op{ReplaceOp{Path: MustNewPointerFromString(""), Value: nil}}) |
| 46 | + }) |
| 47 | + |
| 48 | + It("can diff maps", func() { |
| 49 | + testDiff( |
| 50 | + map[interface{}]interface{}{"a": 123}, |
| 51 | + map[interface{}]interface{}{"a": 124}, |
| 52 | + []Op{ |
| 53 | + ReplaceOp{Path: MustNewPointerFromString("/a"), Value: 124}, |
| 54 | + }, |
| 55 | + ) |
| 56 | + |
| 57 | + testDiff( |
| 58 | + map[interface{}]interface{}{"a": 123, "b": 456}, |
| 59 | + map[interface{}]interface{}{"a": 124, "c": 456}, |
| 60 | + []Op{ |
| 61 | + ReplaceOp{Path: MustNewPointerFromString("/a"), Value: 124}, |
| 62 | + RemoveOp{Path: MustNewPointerFromString("/b")}, |
| 63 | + ReplaceOp{Path: MustNewPointerFromString("/c?"), Value: 456}, |
| 64 | + }, |
| 65 | + ) |
| 66 | + |
| 67 | + testDiff( |
| 68 | + map[interface{}]interface{}{"a": 123, "b": 456}, |
| 69 | + map[interface{}]interface{}{"a": 124}, |
| 70 | + []Op{ |
| 71 | + ReplaceOp{Path: MustNewPointerFromString("/a"), Value: 124}, |
| 72 | + RemoveOp{Path: MustNewPointerFromString("/b")}, |
| 73 | + }, |
| 74 | + ) |
| 75 | + |
| 76 | + testDiff( |
| 77 | + map[interface{}]interface{}{"a": 123, "b": 456}, |
| 78 | + map[interface{}]interface{}{}, |
| 79 | + []Op{ |
| 80 | + RemoveOp{Path: MustNewPointerFromString("/a")}, |
| 81 | + RemoveOp{Path: MustNewPointerFromString("/b")}, |
| 82 | + }, |
| 83 | + ) |
| 84 | + |
| 85 | + testDiff( |
| 86 | + map[interface{}]interface{}{"a": 123}, |
| 87 | + map[interface{}]interface{}{"a": nil}, |
| 88 | + []Op{ |
| 89 | + ReplaceOp{Path: MustNewPointerFromString("/a"), Value: nil}, |
| 90 | + }, |
| 91 | + ) |
| 92 | + |
| 93 | + testDiff( |
| 94 | + map[interface{}]interface{}{"a": 123, "b": map[interface{}]interface{}{"a": 1024, "b": 4056}}, |
| 95 | + map[interface{}]interface{}{"a": 124, "b": map[interface{}]interface{}{"a": 1024, "c": 4056}}, |
| 96 | + []Op{ |
| 97 | + ReplaceOp{Path: MustNewPointerFromString("/a"), Value: 124}, |
| 98 | + RemoveOp{Path: MustNewPointerFromString("/b/b")}, |
| 99 | + ReplaceOp{Path: MustNewPointerFromString("/b/c?"), Value: 4056}, |
| 100 | + }, |
| 101 | + ) |
| 102 | + |
| 103 | + testDiff( |
| 104 | + map[interface{}]interface{}{"a": 123}, |
| 105 | + "a", |
| 106 | + []Op{ |
| 107 | + ReplaceOp{Path: MustNewPointerFromString(""), Value: "a"}, |
| 108 | + }, |
| 109 | + ) |
| 110 | + |
| 111 | + testDiff( |
| 112 | + "a", |
| 113 | + map[interface{}]interface{}{"a": 123}, |
| 114 | + []Op{ |
| 115 | + ReplaceOp{Path: MustNewPointerFromString(""), Value: map[interface{}]interface{}{"a": 123}}, |
| 116 | + }, |
| 117 | + ) |
| 118 | + }) |
| 119 | + |
| 120 | + It("can diff arrays", func() { |
| 121 | + testDiff( |
| 122 | + []interface{}{"a", 123}, |
| 123 | + []interface{}{"b", 123}, |
| 124 | + []Op{ |
| 125 | + ReplaceOp{Path: MustNewPointerFromString("/0"), Value: "b"}, |
| 126 | + }, |
| 127 | + ) |
| 128 | + |
| 129 | + testDiff( |
| 130 | + []interface{}{"a"}, |
| 131 | + []interface{}{"b", 123, 456}, |
| 132 | + []Op{ |
| 133 | + ReplaceOp{Path: MustNewPointerFromString("/0"), Value: "b"}, |
| 134 | + ReplaceOp{Path: MustNewPointerFromString("/-"), Value: 123}, |
| 135 | + ReplaceOp{Path: MustNewPointerFromString("/-"), Value: 456}, |
| 136 | + }, |
| 137 | + ) |
| 138 | + |
| 139 | + testDiff( |
| 140 | + []interface{}{"a", 123, 456}, |
| 141 | + []interface{}{"b"}, |
| 142 | + []Op{ |
| 143 | + ReplaceOp{Path: MustNewPointerFromString("/0"), Value: "b"}, |
| 144 | + RemoveOp{Path: MustNewPointerFromString("/1")}, |
| 145 | + RemoveOp{Path: MustNewPointerFromString("/1")}, |
| 146 | + }, |
| 147 | + ) |
| 148 | + |
| 149 | + testDiff( |
| 150 | + []interface{}{123, 456}, |
| 151 | + []interface{}{}, |
| 152 | + []Op{ |
| 153 | + RemoveOp{Path: MustNewPointerFromString("/0")}, |
| 154 | + RemoveOp{Path: MustNewPointerFromString("/0")}, |
| 155 | + }, |
| 156 | + ) |
| 157 | + |
| 158 | + testDiff( |
| 159 | + []interface{}{123, 456}, |
| 160 | + []interface{}{123, "a", 456}, // TODO unoptimized insertion |
| 161 | + []Op{ |
| 162 | + ReplaceOp{Path: MustNewPointerFromString("/1"), Value: "a"}, |
| 163 | + ReplaceOp{Path: MustNewPointerFromString("/-"), Value: 456}, |
| 164 | + }, |
| 165 | + ) |
| 166 | + |
| 167 | + testDiff( |
| 168 | + []interface{}{[]interface{}{456, 789}}, |
| 169 | + []interface{}{[]interface{}{789}}, |
| 170 | + []Op{ |
| 171 | + ReplaceOp{Path: MustNewPointerFromString("/0/0"), Value: 789}, |
| 172 | + RemoveOp{Path: MustNewPointerFromString("/0/1")}, |
| 173 | + }, |
| 174 | + ) |
| 175 | + |
| 176 | + testDiff( |
| 177 | + []interface{}{"a", 123}, |
| 178 | + "a", |
| 179 | + []Op{ |
| 180 | + ReplaceOp{Path: MustNewPointerFromString(""), Value: "a"}, |
| 181 | + }, |
| 182 | + ) |
| 183 | + |
| 184 | + testDiff( |
| 185 | + "a", |
| 186 | + []interface{}{"a", 123}, |
| 187 | + []Op{ |
| 188 | + ReplaceOp{Path: MustNewPointerFromString(""), Value: []interface{}{"a", 123}}, |
| 189 | + }, |
| 190 | + ) |
| 191 | + }) |
| 192 | +}) |
0 commit comments