testMakeHtml.js 3.0 KB

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