testMakeHtml.js 2.5 KB

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