aboutsummaryrefslogtreecommitdiff
path: root/js/test.js
blob: 5427bbf6c34ba05a16cda41da63ab77c6ca39fcf (plain)
  1. #!/usr/bin/env node
  2. var fs = require('fs');
  3. var commonmark = require('./lib/index.js');
  4. var ansi = require('./ansi/ansi');
  5. var cursor = ansi(process.stdout);
  6. var writer = new commonmark.HtmlRenderer();
  7. var reader = new commonmark.DocParser();
  8. var passed = 0;
  9. var failed = 0;
  10. var showSpaces = function(s) {
  11. var t = s;
  12. return t.replace(/\t/g,'→')
  13. .replace(/ /g,'␣');
  14. };
  15. fs.readFile('spec.txt', 'utf8', function(err, data) {
  16. if (err) {
  17. return console.log(err);
  18. }
  19. var i;
  20. var examples = [];
  21. var current_section = "";
  22. var example_number = 0;
  23. var tests = data
  24. .replace(/\r\n?/g, "\n") // Normalize newlines for platform independence
  25. .replace(/^<!-- END TESTS -->(.|[\n])*/m, '');
  26. tests.replace(/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$|^#{1,6} *(.*)$/gm,
  27. function(_,markdownSubmatch,htmlSubmatch,sectionSubmatch){
  28. if (sectionSubmatch) {
  29. current_section = sectionSubmatch;
  30. } else {
  31. example_number++;
  32. examples.push({markdown: markdownSubmatch,
  33. html: htmlSubmatch,
  34. section: current_section,
  35. number: example_number});
  36. }
  37. });
  38. current_section = "";
  39. console.time("Elapsed time");
  40. for (i = 0; i < examples.length; i++) {
  41. var example = examples[i];
  42. if (example.section !== current_section) {
  43. if (current_section !== '') {
  44. cursor.write('\n');
  45. }
  46. current_section = example.section;
  47. cursor.reset().write(current_section).reset().write(' ');
  48. }
  49. var actual = writer.renderBlock(reader.parse(example.markdown.replace(/→/g, '\t')));
  50. if (actual === example.html) {
  51. passed++;
  52. cursor.green().write('✓').reset();
  53. } else {
  54. failed++;
  55. cursor.write('\n');
  56. cursor.red().write('✘ Example ' + example.number + '\n');
  57. cursor.cyan();
  58. cursor.write('=== markdown ===============\n');
  59. cursor.write(showSpaces(example.markdown));
  60. cursor.write('=== expected ===============\n');
  61. cursor.write(showSpaces(example.html));
  62. cursor.write('=== got ====================\n');
  63. cursor.write(showSpaces(actual));
  64. cursor.reset();
  65. }
  66. }
  67. cursor.write('\n' + passed.toString() + ' tests passed, ' +
  68. failed.toString() + ' failed.\n');
  69. console.timeEnd("Elapsed time");
  70. });