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