run-spec.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. function runSpecs(title, file, options) {
  2. const json = require(file);
  3. let longestName = 0;
  4. let maxSpecs = 0;
  5. const specs = json.reduce((obj, spec) => {
  6. if (!obj[spec.section]) {
  7. longestName = Math.max(spec.section.length, longestName);
  8. obj[spec.section] = {
  9. specs: [],
  10. pass: 0,
  11. total: 0
  12. };
  13. }
  14. obj[spec.section].total++;
  15. maxSpecs = Math.max(obj[spec.section].total, maxSpecs);
  16. if (!spec.shouldFail) {
  17. obj[spec.section].pass++;
  18. }
  19. obj[spec.section].specs.push(spec);
  20. return obj;
  21. }, {});
  22. describe(title, () => {
  23. const maxSpecsLen = ('' + maxSpecs).length;
  24. const spaces = maxSpecsLen * 2 + longestName + 11;
  25. console.log('-'.padEnd(spaces + 4, '-'));
  26. console.log(`| ${title.padStart(Math.ceil((spaces + title.length) / 2)).padEnd(spaces)} |`);
  27. console.log(`| ${' '.padEnd(spaces)} |`);
  28. Object.keys(specs).forEach(section => {
  29. console.log(`| ${section.padEnd(longestName)} ${('' + specs[section].pass).padStart(maxSpecsLen)} of ${('' + specs[section].total).padStart(maxSpecsLen)} ${(100 * specs[section].pass / specs[section].total).toFixed().padStart(4)}% |`);
  30. describe(section, () => {
  31. specs[section].specs.forEach((spec) => {
  32. if (options) {
  33. spec.options = Object.assign({}, options, (spec.options || {}));
  34. }
  35. (spec.only ? fit : it)('should ' + (spec.shouldFail ? 'fail' : 'pass') + ' example ' + spec.example, () => {
  36. if (spec.shouldFail) {
  37. expect(spec).not.toRender(spec.html);
  38. } else {
  39. expect(spec).toRender(spec.html);
  40. }
  41. });
  42. });
  43. });
  44. });
  45. console.log('-'.padEnd(spaces + 4, '-'));
  46. console.log();
  47. });
  48. };
  49. runSpecs('GFM 0.29', './gfm/gfm.0.29.json', {gfm: true});
  50. runSpecs('CommonMark 0.29', './commonmark/commonmark.0.29.json', {headerIds: false});