Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort tests #1004

Merged
merged 5 commits into from Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -27,6 +27,7 @@
"showdown": "*",
"markdown-it": "*",
"front-matter": "^2.3.0",
"glob-to-regexp": "0.3.0",
"gulp": "^3.8.11",
"gulp-uglify": "^1.1.0",
"gulp-concat": "^2.5.2"
Expand Down
108 changes: 61 additions & 47 deletions test/index.js
Expand Up @@ -13,44 +13,70 @@
var fs = require('fs')
, path = require('path')
, fm = require('front-matter')
, g2r = require('glob-to-regexp')
, marked = require('../');

/**
* Load Tests
*/

function load() {
function load(options) {
var dir = __dirname + '/compiled_tests'
, files = {}
, list
, file
, name
, content
, regex
, skip
, glob = g2r(options.glob || "*", { extended: true })
, i
, j
, l;

list = fs
.readdirSync(dir)
.filter(function(file) {
return path.extname(file) !== '.html';
})
.sort(function(a, b) {
a = path.basename(a).toLowerCase().charCodeAt(0);
b = path.basename(b).toLowerCase().charCodeAt(0);
return a > b ? 1 : (a < b ? -1 : 0);
});
.sort();

i = 0;
l = list.length;

for (; i < l; i++) {
file = path.join(dir, list[i]);
content = fm(fs.readFileSync(file, 'utf8'));
for (i = 0; i < l; i++) {
name = path.basename(list[i], ".md");
if (glob.test(name)) {
file = path.join(dir, list[i]);
content = fm(fs.readFileSync(file, 'utf8'));

files[name] = {
options: content.attributes,
text: content.body,
html: fs.readFileSync(file.replace(/[^.]+$/, 'html'), 'utf8')
};
}
}

if (options.bench || options.time) {
if (!options.glob) {
// Change certain tests to allow
// comparison to older benchmark times.
fs.readdirSync(__dirname + '/new').forEach(function(name) {
if (path.extname(name) === '.html') return;
if (name === 'main.md') return;
delete files[name];
});
}

if (files['backslash_escapes.md']) {
files['backslash_escapes.md'] = {
text: 'hello world \\[how](are you) today'
};
}

files[path.basename(file)] = {
options: content.attributes,
text: content.body,
html: fs.readFileSync(file.replace(/[^.]+$/, 'html'), 'utf8')
};
if (files['main.md']) {
files['main.md'].text = files['main.md'].text.replace('* * *\n\n', '');
}
}

return files;
Expand All @@ -68,7 +94,7 @@ function runTests(engine, options) {

var engine = engine || marked
, options = options || {}
, files = options.files || load()
, files = options.files || load(options)
, complete = 0
, failed = 0
, failures = []
Expand Down Expand Up @@ -164,27 +190,7 @@ main:
* Benchmark a function
*/

function bench(name, func) {
var files = bench.files || load();

if (!bench.files) {
bench.files = files;

// Change certain tests to allow
// comparison to older benchmark times.
fs.readdirSync(__dirname + '/new').forEach(function(name) {
if (path.extname(name) === '.html') return;
if (name === 'main.md') return;
delete files[name];
});

files['backslash_escapes.md'] = {
text: 'hello world \\[how](are you) today'
};

files['main.md'].text = files['main.md'].text.replace('* * *\n\n', '');
}

function bench(name, files, func) {
var start = Date.now()
, times = 1000
, keys = Object.keys(files)
Expand All @@ -209,7 +215,8 @@ function bench(name, func) {
*/

function runBench(options) {
var options = options || {};
var options = options || {}
, files = load(options);

// Non-GFM, Non-pedantic
marked.setOptions({
Expand All @@ -223,7 +230,7 @@ function runBench(options) {
if (options.marked) {
marked.setOptions(options.marked);
}
bench('marked', marked);
bench('marked', files, marked);

// GFM
marked.setOptions({
Expand All @@ -237,7 +244,7 @@ function runBench(options) {
if (options.marked) {
marked.setOptions(options.marked);
}
bench('marked (gfm)', marked);
bench('marked (gfm)', files, marked);

// Pedantic
marked.setOptions({
Expand All @@ -251,18 +258,18 @@ function runBench(options) {
if (options.marked) {
marked.setOptions(options.marked);
}
bench('marked (pedantic)', marked);
bench('marked (pedantic)', files, marked);

// showdown
try {
bench('showdown (reuse converter)', (function() {
bench('showdown (reuse converter)', files, (function() {
var Showdown = require('showdown');
var convert = new Showdown.Converter();
return function(text) {
return convert.makeHtml(text);
};
})());
bench('showdown (new converter)', (function() {
bench('showdown (new converter)', files, (function() {
var Showdown = require('showdown');
return function(text) {
var convert = new Showdown.Converter();
Expand All @@ -275,7 +282,7 @@ function runBench(options) {

// markdown-it
try {
bench('markdown-it', (function() {
bench('markdown-it', files, (function() {
var MarkdownIt = require('markdown-it');
var md = new MarkdownIt();
return function(text) {
Expand All @@ -288,7 +295,7 @@ function runBench(options) {

// markdown.js
try {
bench('markdown.js', (function() {
bench('markdown.js', files, (function() {
var markdown = require('markdown').markdown;
return function(text) {
return markdown.toHTML(text);
Expand All @@ -306,11 +313,14 @@ function runBench(options) {
*/

function time(options) {
var options = options || {};
var options = options || {}
, files = load(options);
if (options.marked) {
marked.setOptions(options.marked);
}
bench('marked', marked);
bench('marked', files, marked);

return true;
}

/**
Expand Down Expand Up @@ -475,6 +485,10 @@ function parseArg(argv) {
case '--time':
options.time = true;
break;
case '--glob':
arg = argv.shift();
options.glob = arg.replace(/^=/, '');
break;
default:
if (arg.indexOf('--') === 0) {
opt = camelize(arg.replace(/^--(no-)?/, ''));
Expand Down