|
| 1 | +package gnieh.diffson |
| 2 | +package test |
| 3 | + |
| 4 | +import org.scalatest._ |
| 5 | + |
| 6 | +abstract class TestSimpleDiff[JsValue, Instance <: DiffsonInstance[JsValue]](val instance: Instance) extends FlatSpec with Matchers { |
| 7 | + |
| 8 | + import instance._ |
| 9 | + import provider._ |
| 10 | + |
| 11 | + implicit def boolMarshaller: Marshaller[Boolean] |
| 12 | + implicit def intMarshaller: Marshaller[Int] |
| 13 | + implicit def stringMarshaller: Marshaller[String] |
| 14 | + |
| 15 | + import JsonDiff._ |
| 16 | + |
| 17 | + "a diff" should "be empty if created between two equal values" in { |
| 18 | + val json = parseJson("true") |
| 19 | + simpleDiff(json, json, false) should be(JsonPatch(Nil)) |
| 20 | + } |
| 21 | + |
| 22 | + it should "be a simple replacement if the two values are completely different" in { |
| 23 | + simpleDiff(parseJson("true"), parseJson("13"), false) should be(JsonPatch(Replace(Pointer.root, marshall(13)))) |
| 24 | + } |
| 25 | + |
| 26 | + it should "contain an add operation for each added field" in { |
| 27 | + val json1 = parseJson("""{"lbl": 32}""") |
| 28 | + val json2 = parseJson("""{"lbl": 32, "new": false}""") |
| 29 | + val json3 = parseJson("""{"lbl": 32, "new1": false, "new2": null}""") |
| 30 | + val json4 = parseJson("""{"a": 3, "b": {"a": true }}""") |
| 31 | + val json5 = parseJson("""{"a": 3, "b": {"a": true, "b": 43}, "c": null}""") |
| 32 | + simpleDiff(json1, json2, false) should be(JsonPatch(Add(Pointer("new"), marshall(false)))) |
| 33 | + simpleDiff(json1, json3, false) should be(JsonPatch(Add(Pointer("new2"), JsNull), Add(Pointer("new1"), marshall(false)))) |
| 34 | + simpleDiff(json4, json5, false) should be(JsonPatch(Add(Pointer("b", "b"), marshall(43)), Add(Pointer("c"), JsNull))) |
| 35 | + } |
| 36 | + |
| 37 | + it should "contain a remove operation for each removed field" in { |
| 38 | + val json1 = parseJson("""{"lbl": 32}""") |
| 39 | + val json2 = parseJson("""{"lbl": 32, "old": false}""") |
| 40 | + val json3 = parseJson("""{"lbl": 32, "old1": false, "old2": null}""") |
| 41 | + val json4 = parseJson("""{"a": 3, "b": {"a": true }}""") |
| 42 | + val json5 = parseJson("""{"a": 3, "b": {"a": true, "b": 43}, "c": null}""") |
| 43 | + simpleDiff(json2, json1, false) should be(JsonPatch(Remove(Pointer("old")))) |
| 44 | + simpleDiff(json3, json1, false) should be(JsonPatch(Remove(Pointer("old2")), Remove(Pointer("old1")))) |
| 45 | + simpleDiff(json5, json4, false) should be(JsonPatch(Remove(Pointer("b", "b")), Remove(Pointer("c")))) |
| 46 | + } |
| 47 | + |
| 48 | + it should "correctly handle array diffs in objects (i.e. just replaced)" in { |
| 49 | + val json1 = parseJson("""{"lbl": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}""") |
| 50 | + val json2 = parseJson("""{"lbl": [1, 4, 5, 11, 6, 7]}""") |
| 51 | + simpleDiff(json1, json2, false) should be(JsonPatch(Replace(Pointer("lbl"), JsArray(Vector(marshall(1), marshall(4), marshall(5), marshall(11), marshall(6), marshall(7)))))) |
| 52 | + } |
| 53 | + |
| 54 | + it should "contain a replace operation for each changed field value" in { |
| 55 | + val json1 = parseJson("""{"lbl": 32}""") |
| 56 | + val json2 = parseJson("""{"lbl": 60}""") |
| 57 | + val json3 = parseJson("""{"lbl": {"a": true}}""") |
| 58 | + val json4 = parseJson("""{"lbl": {"a": null}}""") |
| 59 | + simpleDiff(json1, json2, false) should be(JsonPatch(Replace(Pointer("lbl"), marshall(60)))) |
| 60 | + simpleDiff(json1, json3, false) should be(JsonPatch(Replace(Pointer("lbl"), parseJson("""{"a": true}""")))) |
| 61 | + simpleDiff(json3, json4, false) should be(JsonPatch(Replace(Pointer("lbl", "a"), JsNull))) |
| 62 | + } |
| 63 | + |
| 64 | + it should "contain a replaced operation for the changed array (additions)" in { |
| 65 | + val json1 = parseJson("[]") |
| 66 | + val json2 = parseJson("[1, 2, 3]") |
| 67 | + val json3 = parseJson("[1, 2, 4, 5, 6, 3]") |
| 68 | + simpleDiff(json1, json2, false) should be( |
| 69 | + JsonPatch.parse("""[ |
| 70 | + | {"op": "replace", "path": "", "value": [1, 2, 3]} |
| 71 | + | ]""".stripMargin)) |
| 72 | + simpleDiff(json2, json3, false) should be( |
| 73 | + JsonPatch.parse("""[ |
| 74 | + | {"op": "replace", "path": "", "value": [1, 2, 4, 5, 6, 3]} |
| 75 | + | ]""".stripMargin)) |
| 76 | + } |
| 77 | + |
| 78 | + it should "contain a replaced operation for the changed array (deletions)" in { |
| 79 | + val json1 = parseJson("[]") |
| 80 | + val json2 = parseJson("[1, 2, 3]") |
| 81 | + val json3 = parseJson("[1, 2, 4, 5, 6, 3]") |
| 82 | + simpleDiff(json2, json1, false) should be( |
| 83 | + JsonPatch.parse("""[ |
| 84 | + | {"op": "replace", "path": "", "value": []} |
| 85 | + | ]""".stripMargin)) |
| 86 | + simpleDiff(json3, json2, false) should be( |
| 87 | + JsonPatch.parse("""[ |
| 88 | + | {"op": "replace", "path": "", "value": [1, 2, 3]} |
| 89 | + | ]""".stripMargin)) |
| 90 | + } |
| 91 | + |
| 92 | + it should "contain a replace operation for the entire array if at least one element in it changed" in { |
| 93 | + val json1 = parseJson("[1, 2, 3]") |
| 94 | + val json2 = parseJson("[1, 2, 4]") |
| 95 | + val json3 = parseJson("[1, 6, 3]") |
| 96 | + val json4 = parseJson("""[1, {"a": 2}, 3]""") |
| 97 | + val json5 = parseJson("""[1, {"a": 7}, 3]""") |
| 98 | + simpleDiff(json1, json2, false) should be( |
| 99 | + JsonPatch.parse("""[ |
| 100 | + | {"op": "replace", "path": "", "value": [1, 2, 4]} |
| 101 | + | ]""".stripMargin)) |
| 102 | + simpleDiff(json1, json3, false) should be( |
| 103 | + JsonPatch.parse("""[ |
| 104 | + | {"op": "replace", "path": "", "value": [1, 6, 3]} |
| 105 | + | ]""".stripMargin)) |
| 106 | + simpleDiff(json4, json5, false) should be( |
| 107 | + JsonPatch.parse("""[ |
| 108 | + | {"op": "replace", "path": "", "value": [1, {"a": 7}, 3]} |
| 109 | + | ]""".stripMargin)) |
| 110 | + simpleDiff(json4, json3, false) should be( |
| 111 | + JsonPatch.parse("""[ |
| 112 | + | {"op": "replace", "path": "", "value": [1, 6, 3]} |
| 113 | + | ]""".stripMargin)) |
| 114 | + } |
| 115 | + |
| 116 | + "applying a diff" should "be a fix point when applied to the first object used for the diff" in { |
| 117 | + val json1 = parseJson("""{"lbl": 32, "b": {"c": "gruik"}}""") |
| 118 | + val json2 = parseJson("""{"a": 3, "b": {"a": true, "b": 43}, "c": null}""") |
| 119 | + simpleDiff(json1, json2, false)(json1) should be(json2) |
| 120 | + } |
| 121 | + |
| 122 | + "applying a diff to strings" should "provide a correct string representation" in { |
| 123 | + val json1 = """{ |
| 124 | + | "a": 1, |
| 125 | + | "b": true, |
| 126 | + | "c": "test" |
| 127 | + |}""".stripMargin |
| 128 | + val json2 = """{"a":6,"c":"test2","d":false}""".stripMargin |
| 129 | + val json3 = JsonDiff.simpleDiff(json1, json2, false)(json1) |
| 130 | + json3 should be(json2) |
| 131 | + } |
| 132 | + |
| 133 | + "a remembering diff" should "correctly remember old value array" in { |
| 134 | + val json1 = parseJson("""{"lbl": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}""") |
| 135 | + val json2 = parseJson("""{"lbl": [1, 4, 5, 11, 6, 7]}""") |
| 136 | + simpleDiff(json1, json2, true) should be( |
| 137 | + JsonPatch.parse("""[ |
| 138 | + | {"op": "replace", "path": "/lbl", "value": [1, 4, 5, 11, 6, 7], "old": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} |
| 139 | + | ]""".stripMargin)) |
| 140 | + } |
| 141 | + |
| 142 | + it should "correctly add removed values in object diffs" in { |
| 143 | + val json1 = """{"a": 1, "b": true}""" |
| 144 | + val json2 = """{"a": 1}""" |
| 145 | + simpleDiff(json1, json2, true) should be(JsonPatch(Remove(Pointer("b"), Some(marshall(true))))) |
| 146 | + } |
| 147 | + |
| 148 | + it should "correctly add replaced values in object diffs" in { |
| 149 | + val json1 = """{"a": 1, "b": false}""" |
| 150 | + val json2 = """{"a": 1, "b": "test"}""" |
| 151 | + simpleDiff(json1, json2, true) should be(JsonPatch(Replace(Pointer("b"), marshall("test"), Some(marshall(false))))) |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments