marked/test/specs/run-spec.js

103 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-04-25 07:52:11 -05:00
'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();
}
2019-03-09 23:37:33 -06:00
function runSpecs(title, file, options) {
2019-04-25 07:52:11 -05:00
options = options || {};
2019-03-09 23:37:33 -06:00
const json = require(file);
2019-04-09 13:19:50 -05:00
let longestName = 0;
let maxSpecs = 0;
2019-03-09 23:37:33 -06:00
const specs = json.reduce((obj, spec) => {
if (!obj[spec.section]) {
2019-04-09 13:19:50 -05:00
longestName = Math.max(spec.section.length, longestName);
obj[spec.section] = {
specs: [],
pass: 0,
total: 0
};
2019-03-09 23:37:33 -06:00
}
2019-04-09 13:19:50 -05:00
obj[spec.section].total++;
maxSpecs = Math.max(obj[spec.section].total, maxSpecs);
if (!spec.shouldFail) {
obj[spec.section].pass++;
}
obj[spec.section].specs.push(spec);
2019-03-09 23:37:33 -06:00
return obj;
}, {});
2019-04-25 07:52:11 -05:00
outputCompletionTable(title, specs, longestName, maxSpecs);
2019-03-11 11:12:46 -05:00
describe(title, () => {
2019-03-09 23:37:33 -06:00
Object.keys(specs).forEach(section => {
2019-03-11 11:12:46 -05:00
describe(section, () => {
2019-04-09 13:19:50 -05:00
specs[section].specs.forEach((spec) => {
2019-04-25 07:52:11 -05:00
spec.options = Object.assign({}, options, (spec.options || {}));
2019-03-11 11:12:46 -05:00
(spec.only ? fit : it)('should ' + (spec.shouldFail ? 'fail' : 'pass') + ' example ' + spec.example, () => {
2019-03-09 23:37:33 -06:00
if (spec.shouldFail) {
expect(spec).not.toRender(spec.html);
} else {
expect(spec).toRender(spec.html);
}
});
});
});
});
});
};
2019-04-25 07:52:11 -05:00
node4Polyfills();
2019-04-12 10:29:53 -05:00
runSpecs('GFM 0.29', './gfm/gfm.0.29.json', {gfm: true});
2019-04-09 13:21:44 -05:00
runSpecs('CommonMark 0.29', './commonmark/commonmark.0.29.json', {headerIds: false});