Skip to content

Commit

Permalink
enable only certain tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Jan 7, 2018
1 parent bf4022e commit 7747978
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 43 deletions.
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
Original file line number Diff line number Diff line change
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
121 changes: 78 additions & 43 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,32 @@
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
, enabled = options.enabled || []
, i
, l;
, j
, l
, el = enabled.length;


for (i = 0; i < el; i++) {
enabled[i] = g2r(enabled[i]);
}

list = fs
.readdirSync(dir)
Expand All @@ -35,18 +47,53 @@ function load() {
})
.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]);
if (el > 0) {
skip = true;
for (j = 0; j < el; j++) {
if (enabled[j].test(name)) {
skip = false;
break;
}
}
} else {
skip = false;
}
if (!skip) {
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.enabled || options.enabled.length === 0) {
// 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[path.basename(file)] = {
options: content.attributes,
text: content.body,
html: fs.readFileSync(file.replace(/[^.]+$/, 'html'), 'utf8')
};
if (files['backslash_escapes.md']) {
files['backslash_escapes.md'] = {
text: 'hello world \\[how](are you) today'
};
}

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

return files;
Expand All @@ -64,7 +111,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 @@ -160,27 +207,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 @@ -205,7 +232,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 @@ -219,7 +247,7 @@ function runBench(options) {
if (options.marked) {
marked.setOptions(options.marked);
}
bench('marked', marked);
bench('marked', files, marked);

// GFM
marked.setOptions({
Expand All @@ -233,7 +261,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 @@ -247,18 +275,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 @@ -271,7 +299,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 @@ -284,7 +312,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 @@ -302,11 +330,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 @@ -471,6 +502,10 @@ function parseArg(argv) {
case '--time':
options.time = true;
break;
case '--':
options.enabled = argv;
argv = [];
break;
default:
if (arg.indexOf('--') === 0) {
opt = camelize(arg.replace(/^--(no-)?/, ''));
Expand Down

0 comments on commit 7747978

Please sign in to comment.