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