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