makemarkdown.bootstrap.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Created by Estevao on 22-12-2017.
  3. */
  4. //jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  5. (function () {
  6. 'use strict';
  7. require('source-map-support').install();
  8. require('chai').should();
  9. var fs = require('fs');
  10. function getTestSuite (dir) {
  11. return fs.readdirSync(dir)
  12. .filter(filter())
  13. .map(map(dir));
  14. }
  15. function filter () {
  16. return function (file) {
  17. var ext = file.slice(-3);
  18. return (ext === '.md');
  19. };
  20. }
  21. function map (dir) {
  22. return function (file) {
  23. var name = file.replace('.md', ''),
  24. htmlPath = dir + name + '.html',
  25. html = fs.readFileSync(htmlPath, 'utf8'),
  26. mdPath = dir + name + '.md',
  27. md = fs.readFileSync(mdPath, 'utf8');
  28. return {
  29. name: name,
  30. input: html,
  31. expected: md
  32. };
  33. };
  34. }
  35. function assertion (testCase, converter) {
  36. return function () {
  37. testCase.actual = converter.makeMarkdown(testCase.input);
  38. testCase = normalize(testCase);
  39. // Compare
  40. testCase.actual.should.equal(testCase.expected);
  41. };
  42. }
  43. //Normalize input/output
  44. function normalize (testCase) {
  45. // Normalize line returns
  46. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
  47. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
  48. // Ignore all leading/trailing whitespace
  49. testCase.expected = testCase.expected.split('\n').map(function (x) {
  50. return x.trim();
  51. }).join('\n');
  52. testCase.actual = testCase.actual.split('\n').map(function (x) {
  53. return x.trim();
  54. }).join('\n');
  55. // Remove extra lines
  56. testCase.expected = testCase.expected.trim();
  57. testCase.actual = testCase.actual.trim();
  58. // Normalize line returns
  59. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
  60. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
  61. return testCase;
  62. }
  63. module.exports = {
  64. getTestSuite: getTestSuite,
  65. assertion: assertion,
  66. normalize: normalize,
  67. showdown: require('../../../.build/showdown.js')
  68. };
  69. })();