@@ -184,7 +184,50 @@ func TestMergePatchFailRFCCases(t *testing.T) {
184184
185185}
186186
187- func TestMergeReplaceKey (t * testing.T ) {
187+ func TestResembleJSONArray (t * testing.T ) {
188+ testCases := []struct {
189+ input []byte
190+ expected bool
191+ }{
192+ // Failure cases
193+ {input : []byte (`` ), expected : false },
194+ {input : []byte (`not an array` ), expected : false },
195+ {input : []byte (`{"foo": "bar"}` ), expected : false },
196+ {input : []byte (`{"fizz": ["buzz"]}` ), expected : false },
197+ {input : []byte (`[bad suffix` ), expected : false },
198+ {input : []byte (`bad prefix]` ), expected : false },
199+ {input : []byte (`][` ), expected : false },
200+
201+ // Valid cases
202+ {input : []byte (`[]` ), expected : true },
203+ {input : []byte (`["foo", "bar"]` ), expected : true },
204+ {input : []byte (`[["foo", "bar"]]` ), expected : true },
205+ {input : []byte (`[not valid syntax]` ), expected : true },
206+
207+ // Valid cases with whitespace
208+ {input : []byte (` []` ), expected : true },
209+ {input : []byte (`[] ` ), expected : true },
210+ {input : []byte (` [] ` ), expected : true },
211+ {input : []byte (` [ ] ` ), expected : true },
212+ {input : []byte ("\t []" ), expected : true },
213+ {input : []byte ("[]\n " ), expected : true },
214+ {input : []byte ("\n \t \r []" ), expected : true },
215+ }
216+
217+ for _ , test := range testCases {
218+ result := resemblesJSONArray (test .input )
219+ if result != test .expected {
220+ t .Errorf (
221+ `expected "%t" but received "%t" for case: "%s"` ,
222+ test .expected ,
223+ result ,
224+ string (test .input ),
225+ )
226+ }
227+ }
228+ }
229+
230+ func TestCreateMergePatchReplaceKey (t * testing.T ) {
188231 doc := `{ "title": "hello", "nested": {"one": 1, "two": 2} }`
189232 pat := `{ "title": "goodbye", "nested": {"one": 2, "two": 2} }`
190233
@@ -201,7 +244,7 @@ func TestMergeReplaceKey(t *testing.T) {
201244 }
202245}
203246
204- func TestMergeGetArray (t * testing.T ) {
247+ func TestCreateMergePatchGetArray (t * testing.T ) {
205248 doc := `{ "title": "hello", "array": ["one", "two"], "notmatch": [1, 2, 3] }`
206249 pat := `{ "title": "hello", "array": ["one", "two", "three"], "notmatch": [1, 2, 3] }`
207250
@@ -218,7 +261,7 @@ func TestMergeGetArray(t *testing.T) {
218261 }
219262}
220263
221- func TestMergeGetObjArray (t * testing.T ) {
264+ func TestCreateMergePatchGetObjArray (t * testing.T ) {
222265 doc := `{ "title": "hello", "array": [{"banana": true}, {"evil": false}], "notmatch": [{"one":1}, {"two":2}, {"three":3}] }`
223266 pat := `{ "title": "hello", "array": [{"banana": false}, {"evil": true}], "notmatch": [{"one":1}, {"two":2}, {"three":3}] }`
224267
@@ -235,7 +278,7 @@ func TestMergeGetObjArray(t *testing.T) {
235278 }
236279}
237280
238- func TestMergeDeleteKey (t * testing.T ) {
281+ func TestCreateMergePatchDeleteKey (t * testing.T ) {
239282 doc := `{ "title": "hello", "nested": {"one": 1, "two": 2} }`
240283 pat := `{ "title": "hello", "nested": {"one": 1} }`
241284
@@ -253,7 +296,7 @@ func TestMergeDeleteKey(t *testing.T) {
253296 }
254297}
255298
256- func TestMergeEmptyArray (t * testing.T ) {
299+ func TestCreateMergePatchEmptyArray (t * testing.T ) {
257300 doc := `{ "array": null }`
258301 pat := `{ "array": [] }`
259302
@@ -288,7 +331,7 @@ func TestCreateMergePatchNil(t *testing.T) {
288331 }
289332}
290333
291- func TestMergeObjArray (t * testing.T ) {
334+ func TestCreateMergePatchObjArray (t * testing.T ) {
292335 doc := `{ "array": [ {"a": {"b": 2}}, {"a": {"b": 3}} ]}`
293336 exp := `{}`
294337
@@ -304,7 +347,78 @@ func TestMergeObjArray(t *testing.T) {
304347 }
305348}
306349
307- func TestMergeComplexMatch (t * testing.T ) {
350+ func TestCreateMergePatchSameOuterArray (t * testing.T ) {
351+ doc := `[{"foo": "bar"}]`
352+ pat := doc
353+ exp := `[{}]`
354+
355+ res , err := CreateMergePatch ([]byte (doc ), []byte (pat ))
356+
357+ if err != nil {
358+ t .Errorf ("Unexpected error: %s, %s" , err , string (res ))
359+ }
360+
361+ if ! compareJSON (exp , string (res )) {
362+ t .Fatalf ("Outer array was not unmodified" )
363+ }
364+ }
365+
366+ func TestCreateMergePatchModifiedOuterArray (t * testing.T ) {
367+ doc := `[{"name": "John"}, {"name": "Will"}]`
368+ pat := `[{"name": "Jane"}, {"name": "Will"}]`
369+ exp := `[{"name": "Jane"}, {}]`
370+
371+ res , err := CreateMergePatch ([]byte (doc ), []byte (pat ))
372+
373+ if err != nil {
374+ t .Errorf ("Unexpected error: %s, %s" , err , string (res ))
375+ }
376+
377+ if ! compareJSON (exp , string (res )) {
378+ t .Fatalf ("Expected %s but received %s" , exp , res )
379+ }
380+ }
381+
382+ func TestCreateMergePatchMismatchedOuterArray (t * testing.T ) {
383+ doc := `[{"name": "John"}, {"name": "Will"}]`
384+ pat := `[{"name": "Jane"}]`
385+
386+ _ , err := CreateMergePatch ([]byte (doc ), []byte (pat ))
387+
388+ if err == nil {
389+ t .Errorf ("Expected error due to array length differences but received none" )
390+ }
391+ }
392+
393+ func TestCreateMergePatchMismatchedOuterTypes (t * testing.T ) {
394+ doc := `[{"name": "John"}]`
395+ pat := `{"name": "Jane"}`
396+
397+ _ , err := CreateMergePatch ([]byte (doc ), []byte (pat ))
398+
399+ if err == nil {
400+ t .Errorf ("Expected error due to mismatched types but received none" )
401+ }
402+ }
403+
404+ func TestCreateMergePatchNoDifferences (t * testing.T ) {
405+ doc := `{ "title": "hello", "nested": {"one": 1, "two": 2} }`
406+ pat := doc
407+
408+ exp := `{}`
409+
410+ res , err := CreateMergePatch ([]byte (doc ), []byte (pat ))
411+
412+ if err != nil {
413+ t .Errorf ("Unexpected error: %s, %s" , err , string (res ))
414+ }
415+
416+ if ! compareJSON (exp , string (res )) {
417+ t .Fatalf ("Key was not replaced" )
418+ }
419+ }
420+
421+ func TestCreateMergePatchComplexMatch (t * testing.T ) {
308422 doc := `{"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4], "nested": {"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4]} }`
309423 empty := `{}`
310424 res , err := CreateMergePatch ([]byte (doc ), []byte (doc ))
@@ -319,7 +433,7 @@ func TestMergeComplexMatch(t *testing.T) {
319433 }
320434}
321435
322- func TestMergeComplexAddAll (t * testing.T ) {
436+ func TestCreateMergePatchComplexAddAll (t * testing.T ) {
323437 doc := `{"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4], "nested": {"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4]} }`
324438 empty := `{}`
325439 res , err := CreateMergePatch ([]byte (empty ), []byte (doc ))
@@ -333,7 +447,7 @@ func TestMergeComplexAddAll(t *testing.T) {
333447 }
334448}
335449
336- func TestMergeComplexRemoveAll (t * testing.T ) {
450+ func TestCreateMergePatchComplexRemoveAll (t * testing.T ) {
337451 doc := `{"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4], "nested": {"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4]} }`
338452 exp := `{"a":null,"f":null,"hello":null,"i":null,"n":null,"nested":null,"pi":null,"t":null}`
339453 empty := `{}`
@@ -355,7 +469,7 @@ func TestMergeComplexRemoveAll(t *testing.T) {
355469 */
356470}
357471
358- func TestMergeObjectWithInnerArray (t * testing.T ) {
472+ func TestCreateMergePatchObjectWithInnerArray (t * testing.T ) {
359473 stateString := `{
360474 "OuterArray": [
361475 {
@@ -379,7 +493,7 @@ func TestMergeObjectWithInnerArray(t *testing.T) {
379493 }
380494}
381495
382- func TestMergeReplaceKeyNotEscape (t * testing.T ) {
496+ func TestCreateMergePatchReplaceKeyNotEscape (t * testing.T ) {
383497 doc := `{ "title": "hello", "nested": {"title/escaped": 1, "two": 2} }`
384498 pat := `{ "title": "goodbye", "nested": {"title/escaped": 2, "two": 2} }`
385499
0 commit comments