Skip to content

Commit f52ca8f

Browse files
committed
#5: Fix cheapDiff's handling of list addition, new test case, better test errors.
1 parent b98bed0 commit f52ca8f

File tree

6 files changed

+47
-30
lines changed

6 files changed

+47
-30
lines changed

src/Json/Diff.elm

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,21 @@ cheapDiffList root ( a, b ) =
146146
let
147147
diffIndex index =
148148
diffField internalCheapDiff root (String.fromInt index) (get index a) (get index b)
149+
150+
aLen =
151+
List.length a
152+
153+
bLen =
154+
List.length b
155+
156+
range =
157+
if aLen >= bLen then
158+
List.range 0 (aLen - 1) |> List.reverse
159+
160+
else
161+
List.range 0 (bLen - 1)
149162
in
150-
List.range 0 (max (List.length a - 1) (List.length b - 1))
151-
|> List.reverse
152-
|> List.concatMap diffIndex
163+
range |> List.concatMap diffIndex
153164

154165

155166
get : Int -> List a -> Maybe a

tests/Cases.elm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ cases =
365365
"a": {"a": 1},
366366
"b": {"a": 1, "b": {"c": 2}},
367367
"patch": [{"op": "add", "path": "/b", "value": {"c": 2}}]
368+
},
369+
{
370+
"description": "add values to list",
371+
"a": [1, 2, 3, 4],
372+
"b": [1, 2, 3, 4, 5, 6],
373+
"patch": [{"op": "add", "path": "/4", "value": 5}, {"op": "add", "path": "/5", "value": 6}]
368374
}
369375
]
370376
"""

tests/CheapDiff.elm

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Json.Encode as JsonE
77
import Json.Patch as Json
88
import Json.Patch.Invertible as Invertible
99
import Test exposing (..)
10+
import Util.Expect as Expect
1011

1112

1213
suite : Test
@@ -21,21 +22,16 @@ toTest { description, a, b, patch } =
2122
let
2223
applyPatchToA p =
2324
Json.apply p a
25+
26+
diffedPatch =
27+
Diff.cheapDiff a b |> Invertible.toMinimalPatch
2428
in
25-
case Diff.cheapDiff a b |> Invertible.toMinimalPatch |> applyPatchToA of
29+
case diffedPatch |> applyPatchToA of
2630
Ok hopefullyB ->
27-
expectJsonEqual b hopefullyB
31+
Expect.jsonEqual b hopefullyB
2832

2933
Err err ->
30-
Expect.fail ("Couldn't apply patch: " ++ err)
34+
Expect.fail ("Couldn't apply patch:\n\tPatch: " ++ (diffedPatch |> Json.encoder |> JsonE.encode 0) ++ "\n\tError: " ++ err)
3135
in
3236
test (description ++ " | a: " ++ (a |> JsonE.encode 0) ++ " | b: " ++ (b |> JsonE.encode 0))
3337
doTest
34-
35-
36-
expectJsonEqual : JsonE.Value -> JsonE.Value -> Expectation
37-
expectJsonEqual a =
38-
Expect.all
39-
[ Expect.equal a
40-
, \b -> Expect.equal b a
41-
]

tests/Diff.elm

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Json.Diff as Diff
66
import Json.Encode as JsonE
77
import Json.Patch as Json
88
import Test exposing (..)
9+
import Util.Expect as Expect
910

1011

1112
suite : Test
@@ -20,8 +21,5 @@ toTest { description, a, b, patch } =
2021

2122

2223
expectPatchEqual : Json.Patch -> Json.Patch -> Expectation
23-
expectPatchEqual a =
24-
Expect.all
25-
[ Expect.equal a
26-
, \b -> Expect.equal b a
27-
]
24+
expectPatchEqual a b =
25+
Expect.jsonEqual (Json.encoder a) (Json.encoder b)

tests/Invertible.elm

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ module Invertible exposing (invertSuite, mergeSuite)
22

33
import Cases exposing (TestCase, suiteUsingTestCases)
44
import Expect exposing (Expectation)
5-
import Json.Decode as Json
65
import Json.Diff as Diff
76
import Json.Encode as JsonE
87
import Json.Patch.Invertible as Invertable
98
import Test exposing (..)
9+
import Util.Expect as Expect
1010

1111

1212
invertSuite : Test
@@ -27,7 +27,7 @@ invertTest { a, b } =
2727
in
2828
case a |> Invertable.apply diff |> Result.andThen (Invertable.apply inverted) of
2929
Ok aAgain ->
30-
expectJsonEqual a aAgain
30+
Expect.jsonEqual a aAgain
3131

3232
Err err ->
3333
Expect.fail err
@@ -54,19 +54,11 @@ mergeTest { a, b } =
5454
Ok patched ->
5555
case a |> Invertable.apply mergedDiff of
5656
Ok mergePatched ->
57-
expectJsonEqual patched mergePatched
57+
Expect.jsonEqual patched mergePatched
5858

5959
Err err ->
6060
Expect.fail err
6161

6262
Err err ->
6363
Expect.fail err
6464
)
65-
66-
67-
expectJsonEqual : Json.Value -> Json.Value -> Expectation
68-
expectJsonEqual a =
69-
Expect.all
70-
[ Expect.equal a
71-
, \b -> Expect.equal b a
72-
]

tests/Util/Expect.elm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Util.Expect exposing (jsonEqual)
2+
3+
import Expect exposing (Expectation)
4+
import Json.Encode as JsonE
5+
6+
7+
jsonEqual : JsonE.Value -> JsonE.Value -> Expectation
8+
jsonEqual a b =
9+
Expect.all
10+
[ Expect.equal a
11+
, \c -> Expect.equal c a
12+
]
13+
b
14+
|> Expect.onFail ("Expect.jsonEqual\nExpected: " ++ JsonE.encode 0 a ++ "\nActual: " ++ JsonE.encode 0 b)

0 commit comments

Comments
 (0)