bootstrap.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. os = require('os'),
  11. /*jshint -W106 */
  12. beautify = require('js-beautify').html_beautify,
  13. beauOptions = {
  14. eol: os.EOL,
  15. indent_size: 2,
  16. preserve_newlines: false
  17. };
  18. /*jshint +W106 */
  19. function getTestSuite(dir) {
  20. return fs.readdirSync(dir)
  21. .filter(filter())
  22. .map(map(dir));
  23. }
  24. function filter() {
  25. return function (file) {
  26. var ext = file.slice(-3);
  27. return (ext === '.md');
  28. };
  29. }
  30. function map(dir) {
  31. return function (file) {
  32. var name = file.replace('.md', ''),
  33. htmlPath = dir + name + '.html',
  34. html = fs.readFileSync(htmlPath, 'utf8'),
  35. mdPath = dir + name + '.md',
  36. md = fs.readFileSync(mdPath, 'utf8');
  37. return {
  38. name: name,
  39. input: md,
  40. expected: html
  41. };
  42. };
  43. }
  44. function assertion(testCase, converter) {
  45. return function () {
  46. testCase.actual = converter.makeHtml(testCase.input);
  47. testCase = normalize(testCase);
  48. // Compare
  49. testCase.actual.should.equal(testCase.expected);
  50. };
  51. }
  52. //Normalize input/output
  53. function normalize(testCase) {
  54. // Normalize line returns
  55. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
  56. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
  57. // Ignore all leading/trailing whitespace
  58. testCase.expected = testCase.expected.split('\n').map(function (x) {
  59. return x.trim();
  60. }).join('\n');
  61. testCase.actual = testCase.actual.split('\n').map(function (x) {
  62. return x.trim();
  63. }).join('\n');
  64. // Remove extra lines
  65. testCase.expected = testCase.expected.trim();
  66. testCase.actual = testCase.actual.trim();
  67. //Beautify
  68. testCase.expected = beautify(testCase.expected, beauOptions);
  69. testCase.actual = beautify(testCase.actual, beauOptions);
  70. // Normalize line returns
  71. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, os.EOL);
  72. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, os.EOL);
  73. return testCase;
  74. }
  75. module.exports = {
  76. getTestSuite: getTestSuite,
  77. assertion: assertion,
  78. normalize: normalize,
  79. showdown: require('../.build/showdown.js')
  80. };
  81. })();