2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Created by Estevao on 15-01-2015.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function () {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
require('source-map-support').install();
|
|
|
|
require('chai').should();
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 22:57:43 +08:00
|
|
|
var fs = require('fs'),
|
|
|
|
showdown = require('../../../dist/showdown.js'),
|
|
|
|
cases = fs.readdirSync('test/cases/')
|
|
|
|
.filter(filter())
|
|
|
|
.map(map('test/cases/')),
|
|
|
|
issues = fs.readdirSync('test/issues/')
|
|
|
|
.filter(filter())
|
2015-05-30 07:10:21 +08:00
|
|
|
.map(map('test/issues/'));
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
//Tests
|
|
|
|
describe('Converter.makeHtml() simple testcases', function () {
|
2015-05-27 02:58:05 +08:00
|
|
|
var converter = new showdown.Converter();
|
2015-01-19 19:37:21 +08:00
|
|
|
for (var i = 0; i < cases.length; ++i) {
|
2015-05-30 07:10:21 +08:00
|
|
|
if (cases[i].name === 'github-style-at-start') {
|
|
|
|
console.log(showdown.getOptions());
|
|
|
|
}
|
2015-05-27 02:58:05 +08:00
|
|
|
it(cases[i].name, assertion(cases[i], converter));
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 19:37:21 +08:00
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
describe('Converter.makeHtml() issues testcase', function () {
|
2015-05-27 02:58:05 +08:00
|
|
|
var converter = new showdown.Converter();
|
2015-01-19 19:37:21 +08:00
|
|
|
for (var i = 0; i < issues.length; ++i) {
|
2015-05-27 02:58:05 +08:00
|
|
|
it(issues[i].name, assertion(issues[i], converter));
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 19:37:21 +08:00
|
|
|
});
|
|
|
|
|
2015-05-27 02:58:05 +08:00
|
|
|
describe('Converter.options omitExtraWLInCodeBlocks', function () {
|
|
|
|
var converter = new showdown.Converter({omitExtraWLInCodeBlocks: true}),
|
|
|
|
text = 'var foo = bar;',
|
|
|
|
html = converter.makeHtml(' ' + text);
|
|
|
|
it('should omit extra line after code tag', function () {
|
|
|
|
var expectedHtml = '<pre><code>' + text + '</code></pre>';
|
|
|
|
html.should.equal(expectedHtml);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-27 08:48:49 +08:00
|
|
|
describe('Converter.options prefixHeaderId', function () {
|
|
|
|
var converter = new showdown.Converter(),
|
|
|
|
text = 'foo header';
|
|
|
|
|
|
|
|
it('should prefix header id with "section"', function () {
|
|
|
|
converter.setOption('prefixHeaderId', true);
|
|
|
|
var html = converter.makeHtml('# ' + text),
|
|
|
|
expectedHtml = '<h1 id="sectionfooheader">' + text + '</h1>';
|
|
|
|
html.should.equal(expectedHtml);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should prefix header id with custom string', function () {
|
|
|
|
converter.setOption('prefixHeaderId', 'blabla');
|
|
|
|
var html = converter.makeHtml('# ' + text),
|
|
|
|
expectedHtml = '<h1 id="blablafooheader">' + text + '</h1>';
|
|
|
|
html.should.equal(expectedHtml);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
function filter() {
|
|
|
|
return function (file) {
|
|
|
|
var ext = file.slice(-3);
|
|
|
|
return (ext === '.md');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function map(dir) {
|
|
|
|
return function (file) {
|
2015-01-19 22:57:43 +08:00
|
|
|
var name = file.replace('.md', ''),
|
|
|
|
htmlPath = dir + name + '.html',
|
|
|
|
html = fs.readFileSync(htmlPath, 'utf8'),
|
|
|
|
mdPath = dir + name + '.md',
|
|
|
|
md = fs.readFileSync(mdPath, 'utf8');
|
2015-01-19 19:37:21 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
input: md,
|
|
|
|
expected: html
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//Normalize input/output
|
|
|
|
function normalize(testCase) {
|
|
|
|
|
|
|
|
// Normalize line returns
|
|
|
|
testCase.expected = testCase.expected.replace(/\r/g, '');
|
|
|
|
testCase.actual = testCase.actual.replace(/\r/g, '');
|
|
|
|
|
|
|
|
// Ignore all leading/trailing whitespace
|
|
|
|
testCase.expected = testCase.expected.split('\n').map(function (x) {
|
|
|
|
return x.trim();
|
|
|
|
}).join('\n');
|
|
|
|
testCase.actual = testCase.actual.split('\n').map(function (x) {
|
|
|
|
return x.trim();
|
|
|
|
}).join('\n');
|
|
|
|
|
|
|
|
// Remove extra lines
|
|
|
|
testCase.expected = testCase.expected.trim();
|
|
|
|
|
|
|
|
// Convert whitespace to a visible character so that it shows up on error reports
|
|
|
|
testCase.expected = testCase.expected.replace(/ /g, '·');
|
|
|
|
testCase.expected = testCase.expected.replace(/\n/g, '•\n');
|
|
|
|
testCase.actual = testCase.actual.replace(/ /g, '·');
|
|
|
|
testCase.actual = testCase.actual.replace(/\n/g, '•\n');
|
|
|
|
|
|
|
|
return testCase;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-27 02:58:05 +08:00
|
|
|
function assertion(testCase, converter) {
|
2015-01-19 19:37:21 +08:00
|
|
|
return function () {
|
|
|
|
testCase.actual = converter.makeHtml(testCase.input);
|
|
|
|
testCase = normalize(testCase);
|
|
|
|
|
|
|
|
// Compare
|
|
|
|
testCase.actual.should.equal(testCase.expected);
|
|
|
|
};
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
})();
|