diff --git a/package-lock.json b/package-lock.json index 0e80c2b576..47df7524bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -598,6 +598,12 @@ } } }, + "glob-to-regexp": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz", + "integrity": "sha1-jFoUlNIGbFcMw7/kSWF1rMTVAqs=", + "dev": true + }, "glob-watcher": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/glob-watcher/-/glob-watcher-0.0.6.tgz", diff --git a/package.json b/package.json index 83ec18d84d..3a2177996a 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/index.js b/test/index.js index 26f3952296..defd7f3b53 100644 --- a/test/index.js +++ b/test/index.js @@ -13,19 +13,25 @@ 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 @@ -33,24 +39,44 @@ function load() { .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; @@ -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 = [] @@ -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) @@ -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({ @@ -223,7 +230,7 @@ function runBench(options) { if (options.marked) { marked.setOptions(options.marked); } - bench('marked', marked); + bench('marked', files, marked); // GFM marked.setOptions({ @@ -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({ @@ -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(); @@ -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) { @@ -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); @@ -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; } /** @@ -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-)?/, ''));