parse.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // parse.js
  2. // Tests parse(). See readme.txt for details.
  3. var assert = require('assert');
  4. var FS = require('fs');
  5. var JSON5 = require('..');
  6. var Path = require('path');
  7. // Test JSON5.parse() by comparing its output for each case with either the
  8. // native JSON.parse() or ES5 strict-mode eval(). See readme.txt for details.
  9. // For eval(), remember to wrap the input in parentheses before eval()'ing,
  10. // since {...} is ambiguous in JavaScript. Also ensure the parentheses are on
  11. // lines of their own, to support inline comments.
  12. // TODO More test cases, and ones that test specific features and edge cases.
  13. // Mozilla's test cases are a great inspiration and reference here:
  14. // http://mxr.mozilla.org/mozilla-central/source/js/src/tests/ecma_5/JSON/
  15. var dirsPath = Path.resolve(__dirname, 'parse-cases');
  16. var dirs = FS.readdirSync(dirsPath);
  17. function createTest(fileName, dir) {
  18. var ext = Path.extname(fileName);
  19. var filePath = Path.join(dirsPath, dir, fileName);
  20. var str = FS.readFileSync(filePath, 'utf8');
  21. function parseJSON5() {
  22. return JSON5.parse(str);
  23. }
  24. function parseJSON() {
  25. return JSON.parse(str);
  26. }
  27. function parseES5() {
  28. return eval('"use strict"; (\n' + str + '\n)');
  29. }
  30. exports[dir][fileName] = function test() {
  31. switch (ext) {
  32. case '.json':
  33. assert.deepEqual(parseJSON5(), parseJSON(),
  34. 'Expected parsed JSON5 to equal parsed JSON.');
  35. break;
  36. case '.json5':
  37. assert.throws(parseJSON, // test validation
  38. 'Test case bug: expected JSON parsing to fail.');
  39. // Need special case for NaN as NaN != NaN
  40. if ( fileName === 'nan.json5' ) {
  41. assert.equal( isNaN( parseJSON5() ), isNaN( parseES5() ),
  42. 'Expected parsed JSON5 to equal parsed ES5.');
  43. }
  44. else {
  45. assert.deepEqual( parseJSON5(), parseES5(),
  46. 'Expected parsed JSON5 to equal parsed ES5.');
  47. }
  48. break;
  49. case '.js':
  50. assert.throws(parseJSON, // test validation
  51. 'Test case bug: expected JSON parsing to fail.');
  52. assert.doesNotThrow(parseES5, // test validation
  53. 'Test case bug: expected ES5 parsing not to fail.');
  54. assert.throws(parseJSON5,
  55. 'Expected JSON5 parsing to fail.');
  56. break;
  57. case '.txt':
  58. assert.throws(parseES5, // test validation
  59. 'Test case bug: expected ES5 parsing to fail.');
  60. assert.throws(parseJSON5,
  61. 'Expected JSON5 parsing to fail.');
  62. break;
  63. }
  64. };
  65. }
  66. dirs.forEach(function (dir) {
  67. // create a test suite for this group of tests:
  68. exports[dir] = {};
  69. // skip the TODO directory -- these tests are expected to fail:
  70. if (dir === 'todo') {
  71. return;
  72. }
  73. // otherwise create a test for each file in this group:
  74. FS.readdirSync(Path.join(dirsPath, dir)).forEach(function (file) {
  75. createTest(file, dir);
  76. });
  77. });