2012-06-16 02:46:38 +08:00
|
|
|
var showdown = new require('../src/showdown'),
|
2012-06-14 13:05:31 +08:00
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
should = require('should');
|
|
|
|
|
2012-06-21 07:03:36 +08:00
|
|
|
|
|
|
|
var runTestsInDir = function(dir, converter) {
|
|
|
|
|
|
|
|
// Load test cases from disk
|
|
|
|
var cases = fs.readdirSync(dir).filter(function(file){
|
|
|
|
return ~file.indexOf('.md');
|
|
|
|
}).map(function(file){
|
|
|
|
return file.replace('.md', '');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Run each test case (markdown -> html)
|
2012-11-01 15:50:03 +08:00
|
|
|
showdown.forEach(cases, function(test){
|
2012-06-21 07:03:36 +08:00
|
|
|
var name = test.replace(/[-.]/g, ' ');
|
|
|
|
it (name, function(){
|
|
|
|
var mdpath = path.join(dir, test + '.md'),
|
|
|
|
htmlpath = path.join(dir, test + '.html'),
|
|
|
|
md = fs.readFileSync(mdpath, 'utf8'),
|
|
|
|
expected = fs.readFileSync(htmlpath, 'utf8').trim(),
|
|
|
|
actual = converter.makeHtml(md).trim();
|
|
|
|
|
|
|
|
// Normalize line returns
|
|
|
|
expected = expected.replace(/\r/g, '');
|
|
|
|
|
|
|
|
// Ignore all leading/trailing whitespace
|
|
|
|
expected = expected.split('\n').map(function(x){
|
|
|
|
return x.trim();
|
|
|
|
}).join('\n');
|
|
|
|
actual = actual.split('\n').map(function(x){
|
|
|
|
return x.trim();
|
|
|
|
}).join('\n');
|
|
|
|
|
|
|
|
// Convert whitespace to a visible character so that it shows up on error reports
|
|
|
|
expected = expected.replace(/ /g, '·');
|
|
|
|
expected = expected.replace(/\n/g, '•\n');
|
|
|
|
actual = actual.replace(/ /g, '·');
|
|
|
|
actual = actual.replace(/\n/g, '•\n');
|
|
|
|
|
|
|
|
// Compare
|
|
|
|
actual.should.equal(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// :: Markdown to HTML testing ::
|
|
|
|
//
|
|
|
|
|
|
|
|
describe('Markdown', function() {
|
|
|
|
var converter = new showdown.converter();
|
|
|
|
runTestsInDir('test/cases', converter);
|
2015-04-28 00:50:48 +08:00
|
|
|
runTestsInDir('test/issues', converter);
|
2012-06-14 13:05:31 +08:00
|
|
|
});
|
|
|
|
|
2012-06-21 07:03:36 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// :: Extensions Testing ::
|
|
|
|
//
|
|
|
|
|
2015-04-22 22:21:15 +08:00
|
|
|
if (fs.existsSync('test/extensions')) {
|
2012-06-21 07:03:36 +08:00
|
|
|
|
|
|
|
describe('extensions', function() {
|
|
|
|
// Search all sub-folders looking for directory-specific tests
|
|
|
|
var extensions = fs.readdirSync('test/extensions').filter(function(file){
|
|
|
|
return fs.lstatSync('test/extensions/' + file).isDirectory();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Run tests in each extension sub-folder
|
2012-11-01 15:50:03 +08:00
|
|
|
showdown.forEach(extensions, function(ext){
|
2012-06-21 07:03:36 +08:00
|
|
|
// Make sure extension exists
|
|
|
|
var src = 'src/extensions/' + ext + '.js';
|
2015-04-22 22:21:15 +08:00
|
|
|
if (!fs.existsSync(src)) {
|
2012-11-02 02:09:12 +08:00
|
|
|
throw "Attempting tests for '" + ext + "' but source file (" + src + ") was not found.";
|
2012-06-21 07:03:36 +08:00
|
|
|
}
|
|
|
|
|
2012-06-21 11:17:58 +08:00
|
|
|
var converter = new showdown.converter({ extensions: [ ext ] });
|
2012-06-21 07:03:36 +08:00
|
|
|
var dir = 'test/extensions/' + ext;
|
|
|
|
runTestsInDir(dir, converter);
|
|
|
|
});
|
2012-06-14 13:05:31 +08:00
|
|
|
});
|
2012-06-21 07:03:36 +08:00
|
|
|
|
2012-11-02 02:09:12 +08:00
|
|
|
}
|