testMakeHtml.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Created by Estevao on 15-01-2015.
  3. */
  4. (function () {
  5. 'use strict';
  6. require('source-map-support').install();
  7. require('chai').should();
  8. var fs = require('fs'), showdown = require('../../../dist/showdown.js'), converter = new showdown.Converter(), cases = fs.readdirSync('test/cases/').filter(filter()).map(map('test/cases/')), issues = fs.readdirSync('test/issues/').filter(filter()).map(map('test/issues/'));
  9. //Tests
  10. describe('Converter.makeHtml() simple testcases', function () {
  11. for (var i = 0; i < cases.length; ++i) {
  12. it(cases[i].name, assertion(cases[i]));
  13. }
  14. });
  15. describe('Converter.makeHtml() issues testcase', function () {
  16. for (var i = 0; i < issues.length; ++i) {
  17. it(issues[i].name, assertion(issues[i]));
  18. }
  19. });
  20. function filter() {
  21. return function (file) {
  22. var ext = file.slice(-3);
  23. return (ext === '.md');
  24. };
  25. }
  26. function map(dir) {
  27. return function (file) {
  28. var name = file.replace('.md', ''), htmlPath = dir + name + '.html', html = fs.readFileSync(htmlPath,
  29. 'utf8'), mdPath = dir + name + '.md', md = fs.readFileSync(mdPath,
  30. 'utf8');
  31. return {
  32. name: name,
  33. input: md,
  34. expected: html
  35. };
  36. };
  37. }
  38. //Normalize input/output
  39. function normalize(testCase) {
  40. // Normalize line returns
  41. testCase.expected = testCase.expected.replace(/\r/g, '');
  42. testCase.actual = testCase.actual.replace(/\r/g, '');
  43. // Ignore all leading/trailing whitespace
  44. testCase.expected = testCase.expected.split('\n').map(function (x) {
  45. return x.trim();
  46. }).join('\n');
  47. testCase.actual = testCase.actual.split('\n').map(function (x) {
  48. return x.trim();
  49. }).join('\n');
  50. // Remove extra lines
  51. testCase.expected = testCase.expected.trim();
  52. // Convert whitespace to a visible character so that it shows up on error reports
  53. testCase.expected = testCase.expected.replace(/ /g, '·');
  54. testCase.expected = testCase.expected.replace(/\n/g, '•\n');
  55. testCase.actual = testCase.actual.replace(/ /g, '·');
  56. testCase.actual = testCase.actual.replace(/\n/g, '•\n');
  57. return testCase;
  58. }
  59. function assertion(testCase) {
  60. return function () {
  61. testCase.actual = converter.makeHtml(testCase.input);
  62. testCase = normalize(testCase);
  63. // Compare
  64. testCase.actual.should.equal(testCase.expected);
  65. };
  66. }
  67. })();