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