aboutsummaryrefslogtreecommitdiff
path: root/js/test.js
blob: b16b2f103233d168114506521a4d7c8aad717b2b (plain)
  1. #!/usr/bin/env node
  2. var fs = require('fs');
  3. var util = require('util');
  4. var stmd = require('./stmd');
  5. var ansi = require('./ansi/ansi')
  6. var cursor = ansi(process.stdout);
  7. var writer = new stmd.HtmlRenderer();
  8. var reader = new stmd.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 examples = [];
  21. var current_section = "";
  22. var example_number = 0;
  23. tests = data.replace(/^<!-- END TESTS -->(.|[\n])*/m,'');
  24. tests.replace(/^\.\n([\s\S]*?)^\.\n([\s\S]*?)^\.$|^#{1,6} *(.*)$/gm,
  25. function(_,x,y,z,w){
  26. if (z) {
  27. current_section = z;
  28. } else {
  29. example_number++;
  30. examples.push({markdown: x,
  31. html: y,
  32. section: current_section,
  33. number: example_number});
  34. }
  35. });
  36. current_section = "";
  37. console.time("Elapsed time");
  38. for (i = 0; i < examples.length; i++) {
  39. var example = examples[i];
  40. if (example.section != current_section) {
  41. if (current_section !== '') {
  42. cursor.write('\n');
  43. }
  44. current_section = example.section;
  45. cursor.reset().write(current_section).reset().write(' ');
  46. }
  47. var actual = writer.renderBlock(reader.parse(example.markdown.replace(/→/g, '\t')));
  48. if (actual == example.html) {
  49. passed++;
  50. cursor.green().write('✓').reset();
  51. } else {
  52. failed++;
  53. cursor.write('\n');
  54. cursor.red().write('✘ Example ' + example.number + '\n');
  55. cursor.cyan();
  56. cursor.write('=== markdown ===============\n');
  57. cursor.write(showSpaces(example.markdown));
  58. cursor.write('=== expected ===============\n');
  59. cursor.write(showSpaces(example.html));
  60. cursor.write('=== got ====================\n');
  61. cursor.write(showSpaces(actual));
  62. cursor.reset();
  63. }
  64. }
  65. cursor.write('\n' + passed.toString() + ' tests passed, ' +
  66. failed.toString() + ' failed.\n');
  67. console.timeEnd("Elapsed time");
  68. });