testMakeHtml.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. cases = fs.readdirSync('test/cases/')
  11. .filter(filter())
  12. .map(map('test/cases/')),
  13. issues = fs.readdirSync('test/issues/')
  14. .filter(filter())
  15. .map(map('test/issues/'));
  16. //Tests
  17. describe('Converter.makeHtml() simple testcases', function () {
  18. var converter = new showdown.Converter();
  19. for (var i = 0; i < cases.length; ++i) {
  20. if (cases[i].name === 'github-style-at-start') {
  21. console.log(showdown.getOptions());
  22. }
  23. it(cases[i].name, assertion(cases[i], converter));
  24. }
  25. });
  26. describe('Converter.makeHtml() issues testcase', function () {
  27. var converter = new showdown.Converter();
  28. for (var i = 0; i < issues.length; ++i) {
  29. it(issues[i].name, assertion(issues[i], converter));
  30. }
  31. });
  32. describe('Converter.options omitExtraWLInCodeBlocks', function () {
  33. var converter = new showdown.Converter({omitExtraWLInCodeBlocks: true}),
  34. text = 'var foo = bar;',
  35. html = converter.makeHtml(' ' + text);
  36. it('should omit extra line after code tag', function () {
  37. var expectedHtml = '<pre><code>' + text + '</code></pre>';
  38. html.should.equal(expectedHtml);
  39. });
  40. });
  41. describe('Converter.options prefixHeaderId', function () {
  42. var converter = new showdown.Converter(),
  43. text = 'foo header';
  44. it('should prefix header id with "section"', function () {
  45. converter.setOption('prefixHeaderId', true);
  46. var html = converter.makeHtml('# ' + text),
  47. expectedHtml = '<h1 id="sectionfooheader">' + text + '</h1>';
  48. html.should.equal(expectedHtml);
  49. });
  50. it('should prefix header id with custom string', function () {
  51. converter.setOption('prefixHeaderId', 'blabla');
  52. var html = converter.makeHtml('# ' + text),
  53. expectedHtml = '<h1 id="blablafooheader">' + text + '</h1>';
  54. html.should.equal(expectedHtml);
  55. });
  56. });
  57. function filter() {
  58. return function (file) {
  59. var ext = file.slice(-3);
  60. return (ext === '.md');
  61. };
  62. }
  63. function map(dir) {
  64. return function (file) {
  65. var name = file.replace('.md', ''),
  66. htmlPath = dir + name + '.html',
  67. html = fs.readFileSync(htmlPath, 'utf8'),
  68. mdPath = dir + name + '.md',
  69. md = fs.readFileSync(mdPath, 'utf8');
  70. return {
  71. name: name,
  72. input: md,
  73. expected: html
  74. };
  75. };
  76. }
  77. //Normalize input/output
  78. function normalize(testCase) {
  79. // Normalize line returns
  80. testCase.expected = testCase.expected.replace(/\r/g, '');
  81. testCase.actual = testCase.actual.replace(/\r/g, '');
  82. // Ignore all leading/trailing whitespace
  83. testCase.expected = testCase.expected.split('\n').map(function (x) {
  84. return x.trim();
  85. }).join('\n');
  86. testCase.actual = testCase.actual.split('\n').map(function (x) {
  87. return x.trim();
  88. }).join('\n');
  89. // Remove extra lines
  90. testCase.expected = testCase.expected.trim();
  91. // Convert whitespace to a visible character so that it shows up on error reports
  92. testCase.expected = testCase.expected.replace(/ /g, '·');
  93. testCase.expected = testCase.expected.replace(/\n/g, '•\n');
  94. testCase.actual = testCase.actual.replace(/ /g, '·');
  95. testCase.actual = testCase.actual.replace(/\n/g, '•\n');
  96. return testCase;
  97. }
  98. function assertion(testCase, converter) {
  99. return function () {
  100. testCase.actual = converter.makeHtml(testCase.input);
  101. testCase = normalize(testCase);
  102. // Compare
  103. testCase.actual.should.equal(testCase.expected);
  104. };
  105. }
  106. })();