diff --git a/test/specs/run-spec.js b/test/specs/run-spec.js index 3af0aa4565..94080aae00 100644 --- a/test/specs/run-spec.js +++ b/test/specs/run-spec.js @@ -1,4 +1,60 @@ +'use strict'; + +function node4Polyfills() { + // https://github.com/uxitten/polyfill/blob/master/string.polyfill.js + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd + if (!String.prototype.padEnd) { + // eslint-disable-next-line no-extend-native + String.prototype.padEnd = function padEnd(targetLength, padString) { + targetLength = targetLength >> 0; // floor if number or convert non-number to 0; + padString = String((typeof padString !== 'undefined' ? padString : ' ')); + if (this.length > targetLength) { + return String(this); + } else { + targetLength = targetLength - this.length; + if (targetLength > padString.length) { + padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed + } + return String(this) + padString.slice(0, targetLength); + } + }; + } + + // https://github.com/uxitten/polyfill/blob/master/string.polyfill.js + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart + if (!String.prototype.padStart) { + // eslint-disable-next-line no-extend-native + String.prototype.padStart = function padStart(targetLength, padString) { + targetLength = targetLength >> 0; // truncate if number, or convert non-number to 0; + padString = String(typeof padString !== 'undefined' ? padString : ' '); + if (this.length >= targetLength) { + return String(this); + } else { + targetLength = targetLength - this.length; + if (targetLength > padString.length) { + padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed + } + return padString.slice(0, targetLength) + String(this); + } + }; + } +} + +function outputCompletionTable(title, specs, longestName, maxSpecs) { + const maxSpecsLen = ('' + maxSpecs).length; + const spaces = maxSpecsLen * 2 + longestName + 11; + console.log('-'.padEnd(spaces + 4, '-')); + console.log(`| ${title.padStart(Math.ceil((spaces + title.length) / 2)).padEnd(spaces)} |`); + console.log(`| ${' '.padEnd(spaces)} |`); + for (const section in specs) { + 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)}% |`); + } + console.log('-'.padEnd(spaces + 4, '-')); + console.log(); +} + function runSpecs(title, file, options) { + options = options || {}; const json = require(file); let longestName = 0; let maxSpecs = 0; @@ -20,19 +76,13 @@ function runSpecs(title, file, options) { return obj; }, {}); + outputCompletionTable(title, specs, longestName, maxSpecs); + describe(title, () => { - const maxSpecsLen = ('' + maxSpecs).length; - const spaces = maxSpecsLen * 2 + longestName + 11; - console.log('-'.padEnd(spaces + 4, '-')); - console.log(`| ${title.padStart(Math.ceil((spaces + title.length) / 2)).padEnd(spaces)} |`); - console.log(`| ${' '.padEnd(spaces)} |`); Object.keys(specs).forEach(section => { - 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)}% |`); describe(section, () => { specs[section].specs.forEach((spec) => { - if (options) { - spec.options = Object.assign({}, options, (spec.options || {})); - } + spec.options = Object.assign({}, options, (spec.options || {})); (spec.only ? fit : it)('should ' + (spec.shouldFail ? 'fail' : 'pass') + ' example ' + spec.example, () => { if (spec.shouldFail) { expect(spec).not.toRender(spec.html); @@ -43,10 +93,10 @@ function runSpecs(title, file, options) { }); }); }); - console.log('-'.padEnd(spaces + 4, '-')); - console.log(); }); }; +node4Polyfills(); + runSpecs('GFM 0.29', './gfm/gfm.0.29.json', {gfm: true}); runSpecs('CommonMark 0.29', './commonmark/commonmark.0.29.json', {headerIds: false});