testMakeHtml.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. describe('Converter.options extensions', function () {
  58. showdown.extensions.testext = function () {
  59. return [{
  60. type: 'output',
  61. filter: function (text) {
  62. runCount = runCount + 1;
  63. return text;
  64. }
  65. }];
  66. };
  67. var runCount,
  68. converter = new showdown.Converter({extensions: ['testext']});
  69. it('output extensions should run once', function () {
  70. runCount = 0;
  71. converter.makeHtml('# testext');
  72. runCount.should.equal(1);
  73. });
  74. });
  75. function filter() {
  76. return function (file) {
  77. var ext = file.slice(-3);
  78. return (ext === '.md');
  79. };
  80. }
  81. function map(dir) {
  82. return function (file) {
  83. var name = file.replace('.md', ''),
  84. htmlPath = dir + name + '.html',
  85. html = fs.readFileSync(htmlPath, 'utf8'),
  86. mdPath = dir + name + '.md',
  87. md = fs.readFileSync(mdPath, 'utf8');
  88. return {
  89. name: name,
  90. input: md,
  91. expected: html
  92. };
  93. };
  94. }
  95. //Normalize input/output
  96. function normalize(testCase) {
  97. // Normalize line returns
  98. testCase.expected = testCase.expected.replace(/\r/g, '');
  99. testCase.actual = testCase.actual.replace(/\r/g, '');
  100. // Ignore all leading/trailing whitespace
  101. testCase.expected = testCase.expected.split('\n').map(function (x) {
  102. return x.trim();
  103. }).join('\n');
  104. testCase.actual = testCase.actual.split('\n').map(function (x) {
  105. return x.trim();
  106. }).join('\n');
  107. // Remove extra lines
  108. testCase.expected = testCase.expected.trim();
  109. // Convert whitespace to a visible character so that it shows up on error reports
  110. testCase.expected = testCase.expected.replace(/ /g, '·');
  111. testCase.expected = testCase.expected.replace(/\n/g, '•\n');
  112. testCase.actual = testCase.actual.replace(/ /g, '·');
  113. testCase.actual = testCase.actual.replace(/\n/g, '•\n');
  114. return testCase;
  115. }
  116. function assertion(testCase, converter) {
  117. return function () {
  118. testCase.actual = converter.makeHtml(testCase.input);
  119. testCase = normalize(testCase);
  120. // Compare
  121. testCase.actual.should.equal(testCase.expected);
  122. };
  123. }
  124. })();