makehtml.bootstrap.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. function getTestSuite (dir) {
  11. return fs.readdirSync(dir)
  12. .filter(filter())
  13. .map(map(dir));
  14. }
  15. function getJsonTestSuite (file) {
  16. var json = JSON.parse(fs.readFileSync(file, 'utf8'));
  17. return mapJson(json, file);
  18. }
  19. function filter () {
  20. return function (file) {
  21. var ext = file.slice(-3);
  22. return (ext === '.md');
  23. };
  24. }
  25. function map (dir) {
  26. return function (file) {
  27. var oFile = 'file://' + process.cwd().replace(/\\/g, '/') + dir + file,
  28. name = file.replace('.md', ''),
  29. htmlPath = dir + name + '.html',
  30. html = fs.readFileSync(htmlPath, 'utf8'),
  31. mdPath = dir + name + '.md',
  32. md = fs.readFileSync(mdPath, 'utf8');
  33. return {
  34. name: name,
  35. input: md,
  36. expected: html,
  37. file: oFile
  38. };
  39. };
  40. }
  41. function mapJson (jsonArray, file) {
  42. var tcObj = {};
  43. for (var i = 0; i < jsonArray.length; ++i) {
  44. var section = jsonArray[i].section;
  45. var name = jsonArray[i].section + '_' + jsonArray[i].example;
  46. var md = jsonArray[i].markdown;
  47. var html = jsonArray[i].html;
  48. if (!tcObj.hasOwnProperty(section)) {
  49. tcObj[section] = [];
  50. }
  51. tcObj[section].push({
  52. name: name,
  53. input: md,
  54. expected: html,
  55. file: process.cwd().replace(/\\/g, '/') + file
  56. });
  57. }
  58. return tcObj;
  59. }
  60. function assertion (testCase, converter) {
  61. return function () {
  62. testCase.actual = converter.makeHtml(testCase.input);
  63. testCase = normalize(testCase);
  64. // Compare
  65. testCase.actual.should.equal(testCase.expected, testCase.file);
  66. };
  67. }
  68. //Normalize input/output
  69. function normalize (testCase) {
  70. // Normalize line returns
  71. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
  72. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
  73. // Ignore all leading/trailing whitespace
  74. testCase.expected = testCase.expected.split('\n').map(function (x) {
  75. return x.trim();
  76. }).join('\n');
  77. testCase.actual = testCase.actual.split('\n').map(function (x) {
  78. return x.trim();
  79. }).join('\n');
  80. // Remove extra lines
  81. testCase.expected = testCase.expected.trim();
  82. testCase.actual = testCase.actual.trim();
  83. //Beautify
  84. //testCase.expected = beautify(testCase.expected, beauOptions);
  85. //testCase.actual = beautify(testCase.actual, beauOptions);
  86. // Normalize line returns
  87. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
  88. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
  89. return testCase;
  90. }
  91. module.exports = {
  92. getTestSuite: getTestSuite,
  93. getJsonTestSuite: getJsonTestSuite,
  94. assertion: assertion,
  95. normalize: normalize,
  96. showdown: require('../../../.build/showdown.js')
  97. };
  98. })();