testMakeHtml.js 2.8 KB

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