run.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var showdown = new require('../src/showdown'),
  2. fs = require('fs'),
  3. path = require('path'),
  4. should = require('should');
  5. var runTestsInDir = function(dir, converter) {
  6. // Load test cases from disk
  7. var cases = fs.readdirSync(dir).filter(function(file){
  8. return ~file.indexOf('.md');
  9. }).map(function(file){
  10. return file.replace('.md', '');
  11. });
  12. // Run each test case (markdown -> html)
  13. showdown.forEach(cases, function(test){
  14. var name = test.replace(/[-.]/g, ' ');
  15. it (name, function(){
  16. var mdpath = path.join(dir, test + '.md'),
  17. htmlpath = path.join(dir, test + '.html'),
  18. md = fs.readFileSync(mdpath, 'utf8'),
  19. expected = fs.readFileSync(htmlpath, 'utf8').trim(),
  20. actual = converter.makeHtml(md).trim();
  21. // Normalize line returns
  22. expected = expected.replace(/\r/g, '');
  23. // Ignore all leading/trailing whitespace
  24. expected = expected.split('\n').map(function(x){
  25. return x.trim();
  26. }).join('\n');
  27. actual = actual.split('\n').map(function(x){
  28. return x.trim();
  29. }).join('\n');
  30. // Convert whitespace to a visible character so that it shows up on error reports
  31. expected = expected.replace(/ /g, '·');
  32. expected = expected.replace(/\n/g, '•\n');
  33. actual = actual.replace(/ /g, '·');
  34. actual = actual.replace(/\n/g, '•\n');
  35. // Compare
  36. actual.should.equal(expected);
  37. });
  38. });
  39. };
  40. //
  41. // :: Markdown to HTML testing ::
  42. //
  43. describe('Markdown', function() {
  44. var converter = new showdown.converter();
  45. runTestsInDir('test/cases', converter);
  46. });
  47. //
  48. // :: Extensions Testing ::
  49. //
  50. if (fs.existsSync('test/extensions')) {
  51. describe('extensions', function() {
  52. // Search all sub-folders looking for directory-specific tests
  53. var extensions = fs.readdirSync('test/extensions').filter(function(file){
  54. return fs.lstatSync('test/extensions/' + file).isDirectory();
  55. });
  56. // Run tests in each extension sub-folder
  57. showdown.forEach(extensions, function(ext){
  58. // Make sure extension exists
  59. var src = 'src/extensions/' + ext + '.js';
  60. if (!fs.existsSync(src)) {
  61. throw "Attempting tests for '" + ext + "' but source file (" + src + ") was not found.";
  62. }
  63. var converter = new showdown.converter({ extensions: [ ext ] });
  64. var dir = 'test/extensions/' + ext;
  65. runTestsInDir(dir, converter);
  66. });
  67. });
  68. }