bootstrap.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Created by Estevao on 08-06-2015.
  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. var jsdom = require('jsdom');
  11. var document = new jsdom.JSDOM('', {}).window.document; // jshint ignore:line
  12. function getTestSuite (dir) {
  13. return fs.readdirSync(dir)
  14. .filter(filter())
  15. .map(map(dir));
  16. }
  17. function getHtmlToMdTestSuite (dir) {
  18. return fs.readdirSync(dir)
  19. .filter(filter())
  20. .map(map2(dir));
  21. }
  22. function filter () {
  23. return function (file) {
  24. var ext = file.slice(-3);
  25. return (ext === '.md');
  26. };
  27. }
  28. function map (dir) {
  29. return function (file) {
  30. var name = file.replace('.md', ''),
  31. htmlPath = dir + name + '.html',
  32. html = fs.readFileSync(htmlPath, 'utf8'),
  33. mdPath = dir + name + '.md',
  34. md = fs.readFileSync(mdPath, 'utf8');
  35. return {
  36. name: name,
  37. input: md,
  38. expected: html
  39. };
  40. };
  41. }
  42. function map2 (dir) {
  43. return function (file) {
  44. var name = file.replace('.md', ''),
  45. htmlPath = dir + name + '.html',
  46. html = fs.readFileSync(htmlPath, 'utf8'),
  47. mdPath = dir + name + '.md',
  48. md = fs.readFileSync(mdPath, 'utf8');
  49. return {
  50. name: name,
  51. input: html,
  52. expected: md
  53. };
  54. };
  55. }
  56. function assertion (testCase, converter, type) {
  57. return function () {
  58. //var conv = (type === 'makeMd') ? converter.makeMd : converter.makeHtml;
  59. testCase.actual = (type === 'makeMd') ? converter.makeMd(testCase.input, document) : converter.makeHtml(testCase.input);
  60. testCase = normalize(testCase);
  61. // Compare
  62. testCase.actual.should.equal(testCase.expected);
  63. };
  64. }
  65. //Normalize input/output
  66. function normalize (testCase) {
  67. // Normalize line returns
  68. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
  69. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
  70. // Ignore all leading/trailing whitespace
  71. testCase.expected = testCase.expected.split('\n').map(function (x) {
  72. return x.trim();
  73. }).join('\n');
  74. testCase.actual = testCase.actual.split('\n').map(function (x) {
  75. return x.trim();
  76. }).join('\n');
  77. // Remove extra lines
  78. testCase.expected = testCase.expected.trim();
  79. testCase.actual = testCase.actual.trim();
  80. //Beautify
  81. //testCase.expected = beautify(testCase.expected, beauOptions);
  82. //testCase.actual = beautify(testCase.actual, beauOptions);
  83. // Normalize line returns
  84. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
  85. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
  86. return testCase;
  87. }
  88. module.exports = {
  89. getTestSuite: getTestSuite,
  90. getHtmlToMdTestSuite: getHtmlToMdTestSuite,
  91. assertion: assertion,
  92. normalize: normalize,
  93. showdown: require('../.build/showdown.js')
  94. };
  95. })();