showdown/test/functional/makehtml/makehtml.bootstrap.js

141 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

/**
* 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-05-08 01:43:27 +08:00
//const htmlPrettify = require('html-prettify');
const htmlPrettify = require('diffable-html');
let fs = require('fs');
function getTestSuite (dir) {
2015-06-17 08:26:50 +08:00
return fs.readdirSync(dir)
.filter(filter())
.map(map(dir));
}
function getJsonTestSuite (file) {
2022-05-08 01:43:27 +08:00
let json = JSON.parse(fs.readFileSync(file, 'utf8').toString());
return mapJson(json, file);
}
function filter () {
2015-06-17 08:26:50 +08:00
return function (file) {
let ext = file.slice(-3);
2015-06-17 08:26:50 +08:00
return (ext === '.md');
};
2015-06-17 08:26:50 +08:00
}
function map (dir) {
2015-06-17 08:26:50 +08:00
return function (file) {
let oFile = 'file://' + process.cwd().replace(/\\/g, '/') + dir + file,
name = file.replace('.md', ''),
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,
expected: html,
file: oFile
2015-06-17 08:26:50 +08:00
};
};
}
function mapJson (jsonArray, file) {
let tcObj = {};
for (let i = 0; i < jsonArray.length; ++i) {
let section = jsonArray[i].section;
let name = jsonArray[i].section + '_' + (jsonArray[i].example || jsonArray[i].number);
let md = jsonArray[i].markdown;
2022-04-28 15:38:48 +08:00
// transformations
md = md.replace(/→/g, '\t'); // replace → with tabs
let html = jsonArray[i].html;
2022-04-28 15:38:48 +08:00
// transformations
html = html.replace(/→/g, '\t'); // replace → with tabs
if (!tcObj.hasOwnProperty(section)) {
tcObj[section] = [];
}
tcObj[section].push({
name: name,
input: md,
expected: html,
file: process.cwd().replace(/\\/g, '/') + file
});
}
return tcObj;
}
2022-04-28 15:38:48 +08:00
function assertion (testCase, converter, prettify) {
prettify = prettify || false;
2015-06-17 08:26:50 +08:00
return function () {
testCase.actual = converter.makeHtml(testCase.input);
2022-04-28 15:38:48 +08:00
// transformations for readability
//testCase.expected = testCase.expected.replace(/\t/g, '→');
//testCase.actual = testCase.actual.replace(/\t/g, '→');
testCase = normalize(testCase, prettify);
2015-06-17 08:26:50 +08:00
// Compare
testCase.actual.should.equal(testCase.expected, testCase.file);
2015-06-17 08:26:50 +08:00
};
}
//Normalize input/output
2022-04-28 15:38:48 +08:00
function normalize (testCase, prettify) {
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-17 08:26:50 +08:00
// Remove extra lines
testCase.expected = testCase.expected.trim();
testCase.actual = testCase.actual.trim();
2022-04-28 15:38:48 +08:00
//prettify
if (prettify) {
2022-05-09 11:00:29 +08:00
try {
testCase.expected = htmlPrettify(testCase.expected);
testCase.actual = htmlPrettify(testCase.actual);
} catch (e) {
// some weird html in testcase breaks prettifier so we skip it and do some manual stuff
testCase.expected = testCase.expected.trim().replace('\n', '');
testCase.actual = testCase.actual.trim().replace('\n', '');
}
2022-04-28 15:38:48 +08:00
}
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,
getJsonTestSuite: getJsonTestSuite,
2015-06-17 08:26:50 +08:00
assertion: assertion,
2017-12-22 23:28:41 +08:00
showdown: require('../../../.build/showdown.js')
};
2015-06-17 08:26:50 +08:00
})();