Skip to content

Commit a92a78c

Browse files
authored
Merge pull request #8 from SlicingDice/feature/improve-json-comparison
Improve JSON comparison on tests
2 parents 7d820f4 + e1340b4 commit a92a78c

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

tests_and_examples/runQueryTests.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class SlicingDiceTester {
253253
* @param (array) test - the data expected
254254
* @param (array) result - the data received from Slicing Dice API
255255
*/
256-
compareResult(test, result){
256+
compareResult(test, result) {
257257
let expected = this._translateFieldNames(test['expected']);
258258
let dataExpected = test['expected'];
259259

@@ -263,7 +263,7 @@ class SlicingDiceTester {
263263
continue;
264264
}
265265

266-
if (JSON.stringify(expected[key]) != JSON.stringify(result[key])){
266+
if (!this.compareJson(expected[key], result[key])){
267267
this.numFails += 1;
268268
this.failedTests.push(test['name']);
269269

@@ -281,6 +281,77 @@ class SlicingDiceTester {
281281
}
282282
}
283283

284+
/* Compare two JSON's
285+
*
286+
* @param (object) expected - the data expected
287+
* @param (object) result - the data received from Slicing Dice API
288+
*/
289+
compareJson(expected, result) {
290+
if (typeof expected !== typeof result) return false;
291+
if (expected.constructor !== result.constructor) return false;
292+
293+
if (expected instanceof Array) {
294+
return this.compareJsonValue(expected, result);
295+
}
296+
297+
if(typeof expected === "object") {
298+
return this.arrayEqual(expected, result);
299+
}
300+
301+
return expected === result;
302+
}
303+
304+
/* Compare two JSON's values
305+
*
306+
* @param (object) expected - the data expected
307+
* @param (object) result - the data received from Slicing Dice API
308+
*/
309+
compareJsonValue(expected, result) {
310+
for (let key in expected) {
311+
if (expected.hasOwnProperty(key)) {
312+
if (!result.hasOwnProperty(key)) {
313+
return false;
314+
}
315+
316+
if (!this.compareJson(expected[key], result[key])) {
317+
return false;
318+
}
319+
}
320+
}
321+
322+
return true;
323+
}
324+
325+
/* Compare two JSON's arrays
326+
*
327+
* @param (array) expected - the data expected
328+
* @param (array) result - the data received from Slicing Dice API
329+
*/
330+
arrayEqual(expected, result) {
331+
if (expected.length !== result.length) {
332+
return false;
333+
}
334+
335+
let i = expected.length;
336+
337+
while (i--) {
338+
let j = result.length;
339+
let found = false;
340+
341+
while (!found && j--) {
342+
if (this.compareJson(expected[i], result[j])) {
343+
found = true;
344+
}
345+
}
346+
347+
if (!found) {
348+
return false;
349+
}
350+
}
351+
352+
return true;
353+
}
354+
284355
// Write a file testResult.tmp with the result of the tests
285356
updateResult() {
286357
if (process.platform == "win32") {
@@ -290,7 +361,7 @@ class SlicingDiceTester {
290361
let failedTestsStr = "";
291362

292363
if (this.failedTests.length > 0){
293-
for(var item in this.failedTests) {
364+
for(let item in this.failedTests) {
294365
failedTestsStr += " - {0}\n".format(this.failedTests[item]);
295366
}
296367
}

0 commit comments

Comments
 (0)