bootstrap.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Created by Estevao on 08-06-2015.
  3. */
  4. //jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  5. require('source-map-support').install();
  6. require('chai').should();
  7. var fs = require('fs'),
  8. os = require('os'),
  9. beautify = require('js-beautify').html_beautify,
  10. beauOptions = {
  11. eol: os.EOL,
  12. indent_size: 2,
  13. preserve_newlines: false
  14. };
  15. function getTestSuite(dir) {
  16. return fs.readdirSync(dir)
  17. .filter(filter())
  18. .map(map(dir));
  19. }
  20. function filter() {
  21. return function (file) {
  22. var ext = file.slice(-3);
  23. return (ext === '.md');
  24. };
  25. }
  26. function map(dir) {
  27. return function (file) {
  28. var 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. };
  38. };
  39. }
  40. function assertion(testCase, converter) {
  41. return function () {
  42. testCase.actual = converter.makeHtml(testCase.input);
  43. testCase = normalize(testCase);
  44. // Compare
  45. testCase.actual.should.equal(testCase.expected);
  46. };
  47. }
  48. //Normalize input/output
  49. function normalize(testCase) {
  50. // Normalize line returns
  51. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
  52. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
  53. // Ignore all leading/trailing whitespace
  54. testCase.expected = testCase.expected.split('\n').map(function (x) {
  55. return x.trim();
  56. }).join('\n');
  57. testCase.actual = testCase.actual.split('\n').map(function (x) {
  58. return x.trim();
  59. }).join('\n');
  60. // Remove extra lines
  61. testCase.expected = testCase.expected.trim();
  62. testCase.actual = testCase.actual.trim();
  63. //Beautify
  64. testCase.expected = beautify(testCase.expected, beauOptions);
  65. testCase.actual = beautify(testCase.actual, beauOptions);
  66. // Normalize line returns
  67. testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, os.EOL);
  68. testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, os.EOL);
  69. return testCase;
  70. }
  71. module.exports = {
  72. getTestSuite: getTestSuite,
  73. assertion: assertion
  74. };