testMakeHtml.js 2.3 KB

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