Skip to content

Commit eb77f79

Browse files
authored
Merge pull request #31 from marklogic-community/issue-28
Issue 28
2 parents 3520fbf + 4e6f93a commit eb77f79

File tree

4 files changed

+174
-14
lines changed

4 files changed

+174
-14
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
// Given two JSON objects or arrays, determine whether they are the same.
3+
4+
const test = require("/test/test-helper.xqy");
5+
6+
let j0 =
7+
{
8+
"PersonNameType":{
9+
"PersonSurName":"SMITH",
10+
"PersonGivenName":"LINDSEY"
11+
}
12+
};
13+
14+
let j1 =
15+
{
16+
"PersonNameType":{
17+
"PersonSurName":"JONES",
18+
"PersonGivenName":"LINDSEY"
19+
},
20+
"charges": [1,true,"a",null]
21+
};
22+
23+
let j2 =
24+
{
25+
"PersonNameType":{
26+
"PersonSurName":"JONES",
27+
"PersonGivenName":"LINDSEY"
28+
},
29+
"charges": [1,true,"a",null]
30+
};
31+
32+
let j3 =
33+
{
34+
"PersonNameType":{
35+
"PersonGivenName":"LINDSEY",
36+
"PersonSurName":"JONES"
37+
},
38+
"charges": [1,true,"a",null]
39+
};
40+
41+
let j4 =
42+
{
43+
"PersonNameType":{
44+
"PersonGivenName": "LINDSEY",
45+
"PersonSurName": "JONES"
46+
},
47+
"charges": [1, true, "a", null]
48+
};
49+
50+
let j5 =
51+
{
52+
"PersonNameType":{
53+
"PersonGivenName": "LINDSEY",
54+
"PersonSurName": "JONES"
55+
},
56+
"charges": [1, true, "a", null]
57+
};
58+
59+
/**
60+
* test.assertEqualJson expects an xdmp:function, but a JS function is not one of those.
61+
* @param f
62+
* @param msg
63+
*/
64+
function assertThrowsError(f, msg)
65+
{
66+
try {
67+
f();
68+
test.fail(msg);
69+
}
70+
catch (e) {
71+
test.success();
72+
}
73+
}
74+
75+
[
76+
test.assertEqualJson(j1, j2),
77+
test.assertEqualJson(j1, j3),
78+
test.assertEqualJson(j2, j3),
79+
test.assertEqualJson(j4, j5),
80+
test.assertEqualJson(j2, j4),
81+
test.assertEqualJson(j2, j5),
82+
83+
test.assertEqualJson(1, 1),
84+
test.assertEqualJson('a', 'a'),
85+
86+
assertThrowsError(
87+
function() {
88+
test.assertEqualJson(j0, j5)
89+
},
90+
"ASSERT-EQUAL-JSON-FAILED"
91+
)
92+
]

ml-unit-test-client/src/test/ml-modules/root/test/suites/Unit Test Tests/assert-equal-json.xqy

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ declare option xdmp:mapping "false";
44

55
let $j0 := xdmp:to-json(xdmp:from-json-string(
66
'{"PersonNameType":{"PersonSurName":"SMITH","PersonGivenName":"LINDSEY"}}'
7-
))
7+
))/node()
88

99
let $j1 := xdmp:to-json(xdmp:from-json-string(
1010
'{"PersonNameType":{"PersonSurName":"JONES","PersonGivenName":"LINDSEY"},"charges":[1,true,"a",null]}'
11-
))
11+
))/node()
1212

1313
let $j2 := xdmp:to-json(xdmp:from-json-string(
1414
'{"PersonNameType":{"PersonSurName":"JONES","PersonGivenName":"LINDSEY"},"charges":[1,true,"a",null]}'
15-
))
15+
))/node()
1616

1717
let $j3 := xdmp:to-json(xdmp:from-json-string(
1818
'{"PersonNameType":{"PersonGivenName":"LINDSEY", "PersonSurName":"JONES"},"charges":[1,true,"a",null]}'
19-
))
19+
))/node()
2020

2121
let $j4 :=
2222
let $o := json:object()
@@ -51,8 +51,12 @@ return xdmp:eager((
5151
test:assert-equal-json($j1, $j3),
5252
test:assert-equal-json($j2, $j3),
5353
test:assert-equal-json($j4, $j5),
54-
test:assert-equal-json($j2, $j4),
55-
test:assert-equal-json($j2, $j5),
54+
test:assert-throws-error(function() {
55+
test:assert-equal-json($j2, $j4)
56+
}, "ASSERT-EQUAL-JSON-FAILED"),
57+
test:assert-throws-error(function() {
58+
test:assert-equal-json($j2, $j5)
59+
}, "ASSERT-EQUAL-JSON-FAILED"),
5660
test:assert-throws-error(function() {
5761
test:assert-equal-json($j0, $j5)
5862
}, "ASSERT-EQUAL-JSON-FAILED")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
const helper = require('/test/test-helper.xqy');
3+
4+
helper.assertEqual([1, 'b'],[1, 'b']);

ml-unit-test-modules/src/main/ml-modules/root/test/test-helper.xqy

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,13 @@ declare function helper:assert-at-least-one-equal($expected as item()*, $actual
218218

219219
declare private function helper:are-these-equal($expected as item()*, $actual as item()*) {
220220
if (fn:count($expected) eq fn:count($actual)) then
221-
fn:count((for $item at $i in $expected
222-
return
223-
fn:deep-equal($item, $actual[$i]))[. = fn:true()]) eq fn:count($expected)
221+
if ($expected instance of json:array and $actual instance of json:array) then
222+
helper:assert-equal-json-recursive($expected, $actual)
223+
else
224+
fn:count((
225+
for $item at $i in $expected
226+
return
227+
fn:deep-equal($item, $actual[$i]))[. = fn:true()]) eq fn:count($expected)
224228
else
225229
fn:false()
226230
};
@@ -303,12 +307,55 @@ declare function helper:assert-equal-xml($expected, $actual) {
303307
helper:assert-true(fn:false(), ("unsupported type in $actual : ", xdmp:path($actual)))
304308
};
305309

306-
declare function helper:assert-equal-json($expected as map:map, $actual as map:map) {
307-
let $equal := helper:assert-equal-json-recursive($expected, $actual)
308-
return
309-
if ($equal) then helper:success()
310+
declare function helper:assert-equal-json($expected, $actual) {
311+
if ($expected instance of object-node()*) then
312+
if ($actual instance of object-node()*) then
313+
if (fn:count($expected) = fn:count($actual)) then
314+
if (helper:assert-equal-json-recursive($expected, $actual)) then
315+
helper:success()
316+
else
317+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed", ($expected, $actual))
318+
else
319+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed (different counts of objects)", ($expected, $actual))
310320
else
311-
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed", ($expected, $actual))
321+
(: $actual is not object-node()* :)
322+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed ($actual does not consist of objects)", ($expected, $actual))
323+
else if ($expected instance of map:map*) then
324+
if ($actual instance of map:map*) then
325+
if (fn:count($expected) = fn:count($actual)) then
326+
if (helper:assert-equal-json-recursive($expected, $actual)) then
327+
helper:success()
328+
else
329+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed", ($expected, $actual))
330+
else
331+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed (different counts of objects)", ($expected, $actual))
332+
else
333+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed ($actual does not consist of objects)", ($expected, $actual))
334+
else if ($expected instance of array-node()*) then
335+
if ($actual instance of array-node()*) then
336+
if (fn:count($expected) = fn:count($actual)) then
337+
if (helper:assert-equal-json-recursive($expected, $actual)) then
338+
helper:success()
339+
else
340+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed", ($expected, $actual))
341+
else
342+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed (different counts of arrays)", ($expected, $actual))
343+
else
344+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed ($actual does not consist of arrays)", ($expected, $actual))
345+
else if ($expected instance of document-node()) then
346+
if ($actual instance of document-node()) then
347+
if (fn:count($expected) = fn:count($actual)) then
348+
if (helper:assert-equal-json-recursive($expected/node(), $actual/node())) then
349+
helper:success()
350+
else
351+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed (documents not equal)", ($expected, $actual))
352+
else
353+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed (different counts of documents)", ($expected, $actual))
354+
else
355+
fn:error(xs:QName("ASSERT-EQUAL-JSON-FAILED"), "Assert Equal Json failed ($actual is not a document)", ($expected, $actual))
356+
else
357+
(: scalar values :)
358+
helper:assert-equal($expected, $actual)
312359
};
313360

314361
declare function helper:assert-equal-json-recursive($object1, $object2) as xs:boolean
@@ -335,6 +382,19 @@ declare function helper:assert-equal-json-recursive($object1, $object2) as xs:bo
335382
helper:assert-equal-json-recursive($item, $o2[$i])
336383
return
337384
$counts-equal and fn:not($items-equal = fn:false())
385+
case object-node() return
386+
let $m1 := fn:data($object1)
387+
let $m2 := fn:data($object2)
388+
let $k1 := map:keys($m1)
389+
let $k2 := map:keys($m2)
390+
let $counts-equal := fn:count($k1) eq fn:count($k2)
391+
let $maps-equal :=
392+
for $key in map:keys($m1)
393+
let $v1 := map:get($m1, $key)
394+
let $v2 := map:get($m2, $key)
395+
return
396+
helper:assert-equal-json-recursive($v1, $v2)
397+
return $counts-equal and fn:not($maps-equal = fn:false())
338398
default return
339399
$object1 = $object2
340400
};

0 commit comments

Comments
 (0)