marked/test/index.js

225 lines
4.5 KiB
JavaScript
Raw Normal View History

2011-08-22 22:34:20 -05:00
#!/usr/bin/env node
var fs = require('fs')
, path = require('path')
2013-01-06 21:54:44 -06:00
, marked = require('marked');
2013-01-03 08:29:40 -06:00
function load() {
2013-01-06 21:54:44 -06:00
var dir = __dirname + '/tests'
, files = {}
, list
, file
, i
, l;
2011-08-22 22:34:20 -05:00
2013-01-06 21:54:44 -06:00
list = fs
2011-08-22 23:16:25 -05:00
.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);
});
2011-08-22 22:34:20 -05:00
2013-01-06 21:54:44 -06:00
i = 0;
l = list.length;
2011-08-22 23:16:25 -05:00
for (; i < l; i++) {
file = path.join(dir, list[i]);
files[path.basename(file)] = {
text: fs.readFileSync(file, 'utf8'),
html: fs.readFileSync(file.replace(/[^.]+$/, 'html'), 'utf8')
};
}
return files;
2013-01-03 08:29:40 -06:00
}
2011-08-22 23:16:25 -05:00
2013-01-06 21:43:11 -06:00
function runTests(options) {
var options = options || {}
2013-01-06 21:54:44 -06:00
, files = options.files || load()
2013-01-06 21:43:11 -06:00
, complete = 0
2011-08-22 23:16:25 -05:00
, keys = Object.keys(files)
2013-01-06 21:43:11 -06:00
, i = 0
, len = keys.length
2011-08-22 23:16:25 -05:00
, filename
, file
, text
2013-01-06 21:43:11 -06:00
, html
, j
, l;
2011-08-22 22:34:20 -05:00
2011-10-22 08:36:34 -05:00
main:
2013-01-06 21:43:11 -06:00
for (; i < len; i++) {
filename = keys[i];
2011-08-22 23:16:25 -05:00
file = files[filename];
2011-10-22 08:36:34 -05:00
try {
2011-08-22 23:40:55 -05:00
text = marked(file.text).replace(/\s/g, '');
2011-08-22 23:16:25 -05:00
html = file.html.replace(/\s/g, '');
2011-10-22 08:36:34 -05:00
} catch(e) {
console.log('%s failed.', filename);
throw e;
2011-08-22 22:34:20 -05:00
}
2013-01-06 21:43:11 -06:00
j = 0;
l = html.length;
2011-08-22 23:16:25 -05:00
2013-01-06 21:43:11 -06:00
for (; j < l; j++) {
if (text[j] !== html[j]) {
2011-08-22 22:34:20 -05:00
text = text.substring(
2013-01-06 21:43:11 -06:00
Math.max(j - 30, 0),
Math.min(j + 30, text.length));
2011-08-22 22:34:20 -05:00
html = html.substring(
2013-01-06 21:43:11 -06:00
Math.max(j - 30, 0),
Math.min(j + 30, html.length));
2011-08-22 22:34:20 -05:00
console.log(
2011-10-22 08:36:34 -05:00
'\n#%d. %s failed at offset %d. Near: "%s".\n',
2013-01-06 21:43:11 -06:00
i + 1, filename, j, text);
console.log('\nGot:\n%s\n', text.trim() || text);
console.log('\nExpected:\n%s\n', html.trim() || html);
2013-01-06 21:43:11 -06:00
if (options.stop) {
2011-08-22 22:34:20 -05:00
break main;
}
2013-01-06 21:43:11 -06:00
continue main;
2011-08-22 22:34:20 -05:00
}
}
2013-01-06 21:43:11 -06:00
complete++;
console.log('#%d. %s completed.', i + 1, filename);
2011-08-22 22:34:20 -05:00
}
2013-01-06 21:43:11 -06:00
console.log('%d/%d tests completed successfully.', complete, len);
2013-01-03 08:29:40 -06:00
}
2011-08-22 22:34:20 -05:00
2013-01-03 08:29:40 -06:00
function bench(name, func) {
2013-01-06 21:54:44 -06:00
var files = bench.files || load();
if (!bench.files) {
bench.files = files;
2013-01-06 21:43:11 -06:00
// Change certain tests to allow
// comparison to older benchmark times.
2012-01-09 08:18:24 -06:00
fs.readdirSync(__dirname + '/new').forEach(function(name) {
2013-01-06 21:54:44 -06:00
if (path.extname(name) === '.html') return;
2012-01-09 08:18:24 -06:00
if (name === 'main.text') return;
delete files[name];
});
2013-01-06 21:54:44 -06:00
files['backslash_escapes.text'] = {
text: 'hello world \\[how](are you) today'
};
2013-01-06 21:54:44 -06:00
files['main.text'].text = files['main.text'].text.replace('* * *\n\n', '');
}
2011-08-22 23:16:25 -05:00
var start = Date.now()
, times = 1000
, keys = Object.keys(files)
2013-01-06 21:43:11 -06:00
, i
2011-08-22 23:16:25 -05:00
, l = keys.length
, filename
, file;
while (times--) {
for (i = 0; i < l; i++) {
filename = keys[i];
file = files[filename];
func(file.text);
}
}
console.log('%s completed in %dms.', name, Date.now() - start);
2013-01-03 08:29:40 -06:00
}
2011-08-22 23:16:25 -05:00
2013-01-03 08:29:40 -06:00
function runBench() {
2013-01-09 13:43:36 -06:00
marked.setOptions({
gfm: false,
tables: false,
breaks: false,
pedantic: false,
sanitize: false
});
2013-01-03 08:29:40 -06:00
bench('marked', marked);
2011-08-22 23:16:25 -05:00
2013-01-09 13:43:36 -06:00
marked.setOptions({
gfm: true,
tables: false,
breaks: false,
pedantic: false,
sanitize: false
});
2013-01-03 08:29:40 -06:00
bench('marked (gfm)', marked);
2013-01-09 13:43:36 -06:00
marked.setOptions({
gfm: false,
tables: false,
breaks: false,
pedantic: true,
sanitize: false
});
2013-01-03 08:29:40 -06:00
bench('marked (pedantic)', marked);
2012-02-19 20:29:35 -06:00
2012-01-03 02:25:47 -06:00
var discount = require('discount').parse;
2013-01-03 08:29:40 -06:00
bench('discount', discount);
2012-01-03 02:25:47 -06:00
2011-08-22 23:16:25 -05:00
var showdown = (function() {
var Showdown = require('showdown').Showdown;
var convert = new Showdown.converter();
return function(text) {
return convert.makeHtml(text);
};
})();
2013-01-03 08:29:40 -06:00
bench('showdown (reuse converter)', showdown);
2011-10-22 08:36:34 -05:00
var showdown_slow = (function() {
var Showdown = require('showdown').Showdown;
return function(text) {
var convert = new Showdown.converter();
return convert.makeHtml(text);
};
})();
2013-01-03 08:29:40 -06:00
bench('showdown (new converter)', showdown_slow);
2011-08-22 23:16:25 -05:00
var markdownjs = require('markdown');
2013-01-03 08:29:40 -06:00
bench('markdown-js', function(text) {
markdownjs.parse(text);
2011-08-22 23:16:25 -05:00
});
2013-01-03 08:29:40 -06:00
}
2011-08-22 22:34:20 -05:00
2013-01-03 08:29:40 -06:00
function time() {
2011-10-22 08:36:34 -05:00
var marked = require('../');
2013-01-03 08:29:40 -06:00
bench('marked', marked);
}
2011-10-22 08:36:34 -05:00
2013-01-03 08:29:40 -06:00
function main(argv) {
if (~argv.indexOf('--bench')) {
return runBench();
}
if (~argv.indexOf('--time')) {
return time();
2011-08-22 23:16:25 -05:00
}
2013-01-03 08:29:40 -06:00
2013-01-06 21:43:11 -06:00
return runTests({
stop: !!~argv.indexOf('--stop')
});
2013-01-03 08:29:40 -06:00
}
if (!module.parent) {
main(process.argv.slice());
2011-08-22 23:16:25 -05:00
} else {
2013-01-03 08:29:40 -06:00
main = runTests;
main.main = main;
main.load = load;
2013-01-03 08:29:40 -06:00
main.bench = bench;
2011-08-22 23:16:25 -05:00
module.exports = main;
}