PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 57ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/tests/json-cmp.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 65 lines | 45 code | 2 blank | 18 comment | 19 complexity | 5b9bf029a4415af01acd7e32f8bb96bd MD5 | raw file
 1/*
 2    json-cmp.js - compare JSON files
 3    Copyright (C) 2008 siliconforks.com
 4
 5    This program is free software; you can redistribute it and/or modify
 6    it under the terms of the GNU General Public License as published by
 7    the Free Software Foundation; either version 2 of the License, or
 8    (at your option) any later version.
 9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20function json_equals(json1, json2) {
21  if (json1 === null || json2 === null) {
22    return json1 === json2;
23  }
24  else if (json1.constructor === Array && json2.constructor === Array) {
25    if (json1.length !== json2.length) {
26      return false;
27    }
28    var length = json1.length;
29    for (var i = 0; i < length; i++) {
30      if (! json_equals(json1[i], json2[i])) {
31        return false;
32      }
33    }
34    return true;
35  }
36  else if (typeof(json1) === 'object' && typeof(json2) === 'object') {
37    var i;
38    for (i in json1) {
39      if (! (i in json2)) {
40        return false;
41      }
42      if (! json_equals(json1[i], json2[i])) {
43        return false;
44      }
45    }
46    for (i in json2) {
47      if (! (i in json1)) {
48        return false;
49      }
50    }
51    return true;
52  }
53  else {
54    return json1 === json2;
55  }
56}
57
58if (json_equals(EXPECTED, ACTUAL)) {
59  quit(0);
60}
61else {
62  print(EXPECTED.toSource());
63  print(ACTUAL.toSource());
64  quit(1);
65}