2015-06-11 08:29:42 +08:00
|
|
|
/**
|
|
|
|
* Created by Estevao on 08-06-2015.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
2015-06-17 08:26:50 +08:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('source-map-support').install();
|
|
|
|
require('chai').should();
|
2022-03-27 08:05:30 +08:00
|
|
|
let fs = require('fs');
|
2015-06-11 08:29:42 +08:00
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function getTestSuite (dir) {
|
2015-06-17 08:26:50 +08:00
|
|
|
return fs.readdirSync(dir)
|
|
|
|
.filter(filter())
|
|
|
|
.map(map(dir));
|
|
|
|
}
|
2015-06-11 08:29:42 +08:00
|
|
|
|
2018-09-25 11:04:59 +08:00
|
|
|
function getJsonTestSuite (file) {
|
2022-03-27 08:05:30 +08:00
|
|
|
let json = JSON.parse(fs.readFileSync(file, 'utf8'));
|
2018-09-25 11:04:59 +08:00
|
|
|
return mapJson(json, file);
|
|
|
|
}
|
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function filter () {
|
2015-06-17 08:26:50 +08:00
|
|
|
return function (file) {
|
2022-03-27 08:05:30 +08:00
|
|
|
let ext = file.slice(-3);
|
2015-06-17 08:26:50 +08:00
|
|
|
return (ext === '.md');
|
2015-06-11 08:29:42 +08:00
|
|
|
};
|
2015-06-17 08:26:50 +08:00
|
|
|
}
|
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function map (dir) {
|
2015-06-17 08:26:50 +08:00
|
|
|
return function (file) {
|
2022-03-27 08:05:30 +08:00
|
|
|
let oFile = 'file://' + process.cwd().replace(/\\/g, '/') + dir + file,
|
2018-09-25 11:04:59 +08:00
|
|
|
name = file.replace('.md', ''),
|
2017-01-31 13:46:25 +08:00
|
|
|
htmlPath = dir + name + '.html',
|
|
|
|
html = fs.readFileSync(htmlPath, 'utf8'),
|
|
|
|
mdPath = dir + name + '.md',
|
|
|
|
md = fs.readFileSync(mdPath, 'utf8');
|
2015-06-17 08:26:50 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
input: md,
|
2018-09-25 11:04:59 +08:00
|
|
|
expected: html,
|
|
|
|
file: oFile
|
2015-06-17 08:26:50 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-25 11:04:59 +08:00
|
|
|
function mapJson (jsonArray, file) {
|
2022-03-27 08:05:30 +08:00
|
|
|
let tcObj = {};
|
|
|
|
for (let i = 0; i < jsonArray.length; ++i) {
|
|
|
|
let section = jsonArray[i].section;
|
|
|
|
let name = jsonArray[i].section + '_' + jsonArray[i].example;
|
|
|
|
let md = jsonArray[i].markdown;
|
|
|
|
let html = jsonArray[i].html;
|
2018-09-25 11:04:59 +08:00
|
|
|
if (!tcObj.hasOwnProperty(section)) {
|
|
|
|
tcObj[section] = [];
|
|
|
|
}
|
|
|
|
tcObj[section].push({
|
|
|
|
name: name,
|
|
|
|
input: md,
|
|
|
|
expected: html,
|
|
|
|
file: process.cwd().replace(/\\/g, '/') + file
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return tcObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function assertion (testCase, converter) {
|
2015-06-17 08:26:50 +08:00
|
|
|
return function () {
|
|
|
|
testCase.actual = converter.makeHtml(testCase.input);
|
|
|
|
testCase = normalize(testCase);
|
|
|
|
|
|
|
|
// Compare
|
2018-09-25 11:04:59 +08:00
|
|
|
testCase.actual.should.equal(testCase.expected, testCase.file);
|
2015-06-17 08:26:50 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//Normalize input/output
|
2017-01-31 13:46:25 +08:00
|
|
|
function normalize (testCase) {
|
2015-06-17 08:26:50 +08:00
|
|
|
|
|
|
|
// Normalize line returns
|
|
|
|
testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
|
|
|
|
testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
|
|
|
|
|
|
|
|
// 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');
|
2015-06-11 08:29:42 +08:00
|
|
|
|
2015-06-17 08:26:50 +08:00
|
|
|
// Remove extra lines
|
|
|
|
testCase.expected = testCase.expected.trim();
|
|
|
|
testCase.actual = testCase.actual.trim();
|
2015-06-11 08:29:42 +08:00
|
|
|
|
2015-06-17 08:26:50 +08:00
|
|
|
//Beautify
|
2016-02-02 08:29:06 +08:00
|
|
|
//testCase.expected = beautify(testCase.expected, beauOptions);
|
|
|
|
//testCase.actual = beautify(testCase.actual, beauOptions);
|
2015-06-17 08:26:50 +08:00
|
|
|
|
|
|
|
// Normalize line returns
|
2015-07-15 03:53:28 +08:00
|
|
|
testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
|
|
|
|
testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
|
2015-06-17 08:26:50 +08:00
|
|
|
|
|
|
|
return testCase;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getTestSuite: getTestSuite,
|
2018-09-25 11:04:59 +08:00
|
|
|
getJsonTestSuite: getJsonTestSuite,
|
2015-06-17 08:26:50 +08:00
|
|
|
assertion: assertion,
|
|
|
|
normalize: normalize,
|
2017-12-22 23:28:41 +08:00
|
|
|
showdown: require('../../../.build/showdown.js')
|
2015-06-11 08:29:42 +08:00
|
|
|
};
|
2015-06-17 08:26:50 +08:00
|
|
|
})();
|
|
|
|
|