testMakeHtml.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // Compare
  39. html.should.equal(expectedHtml);
  40. });
  41. });
  42. function filter() {
  43. return function (file) {
  44. var ext = file.slice(-3);
  45. return (ext === '.md');
  46. };
  47. }
  48. function map(dir) {
  49. return function (file) {
  50. var name = file.replace('.md', ''),
  51. htmlPath = dir + name + '.html',
  52. html = fs.readFileSync(htmlPath, 'utf8'),
  53. mdPath = dir + name + '.md',
  54. md = fs.readFileSync(mdPath, 'utf8');
  55. return {
  56. name: name,
  57. input: md,
  58. expected: html
  59. };
  60. };
  61. }
  62. //Normalize input/output
  63. function normalize(testCase) {
  64. // Normalize line returns
  65. testCase.expected = testCase.expected.replace(/\r/g, '');
  66. testCase.actual = testCase.actual.replace(/\r/g, '');
  67. // Ignore all leading/trailing whitespace
  68. testCase.expected = testCase.expected.split('\n').map(function (x) {
  69. return x.trim();
  70. }).join('\n');
  71. testCase.actual = testCase.actual.split('\n').map(function (x) {
  72. return x.trim();
  73. }).join('\n');
  74. // Remove extra lines
  75. testCase.expected = testCase.expected.trim();
  76. // Convert whitespace to a visible character so that it shows up on error reports
  77. testCase.expected = testCase.expected.replace(/ /g, '·');
  78. testCase.expected = testCase.expected.replace(/\n/g, '•\n');
  79. testCase.actual = testCase.actual.replace(/ /g, '·');
  80. testCase.actual = testCase.actual.replace(/\n/g, '•\n');
  81. return testCase;
  82. }
  83. function assertion(testCase, converter) {
  84. return function () {
  85. testCase.actual = converter.makeHtml(testCase.input);
  86. testCase = normalize(testCase);
  87. // Compare
  88. testCase.actual.should.equal(testCase.expected);
  89. };
  90. }
  91. })();