UNPKG

3.07 kBJavaScriptView Raw
1/**
2 * Created by Estevao on 08-06-2015.
3 */
4
5//jscs:disable requireCamelCaseOrUpperCaseIdentifiers
6(function () {
7 'use strict';
8
9 require('source-map-support').install();
10 require('chai').should();
11 var fs = require('fs');
12 var jsdom = require('jsdom');
13 var document = new jsdom.JSDOM('', {}).window.document; // jshint ignore:line
14
15 function getTestSuite (dir) {
16 return fs.readdirSync(dir)
17 .filter(filter())
18 .map(map(dir));
19 }
20
21 function getHtmlToMdTestSuite (dir) {
22 return fs.readdirSync(dir)
23 .filter(filter())
24 .map(map2(dir));
25 }
26
27 function filter () {
28 return function (file) {
29 var ext = file.slice(-3);
30 return (ext === '.md');
31 };
32 }
33
34 function map (dir) {
35 return function (file) {
36 var name = file.replace('.md', ''),
37 htmlPath = dir + name + '.html',
38 html = fs.readFileSync(htmlPath, 'utf8'),
39 mdPath = dir + name + '.md',
40 md = fs.readFileSync(mdPath, 'utf8');
41
42 return {
43 name: name,
44 input: md,
45 expected: html
46 };
47 };
48 }
49
50 function map2 (dir) {
51 return function (file) {
52 var name = file.replace('.md', ''),
53 htmlPath = dir + name + '.html',
54 html = fs.readFileSync(htmlPath, 'utf8'),
55 mdPath = dir + name + '.md',
56 md = fs.readFileSync(mdPath, 'utf8');
57
58 return {
59 name: name,
60 input: html,
61 expected: md
62 };
63 };
64 }
65
66 function assertion (testCase, converter, type) {
67 return function () {
68 //var conv = (type === 'makeMd') ? converter.makeMd : converter.makeHtml;
69
70 testCase.actual = (type === 'makeMd') ? converter.makeMd(testCase.input, document) : converter.makeHtml(testCase.input);
71 testCase = normalize(testCase);
72
73 // Compare
74 testCase.actual.should.equal(testCase.expected);
75 };
76 }
77
78 //Normalize input/output
79 function normalize (testCase) {
80
81 // Normalize line returns
82 testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
83 testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
84
85 // Ignore all leading/trailing whitespace
86 testCase.expected = testCase.expected.split('\n').map(function (x) {
87 return x.trim();
88 }).join('\n');
89 testCase.actual = testCase.actual.split('\n').map(function (x) {
90 return x.trim();
91 }).join('\n');
92
93 // Remove extra lines
94 testCase.expected = testCase.expected.trim();
95 testCase.actual = testCase.actual.trim();
96
97 //Beautify
98 //testCase.expected = beautify(testCase.expected, beauOptions);
99 //testCase.actual = beautify(testCase.actual, beauOptions);
100
101 // Normalize line returns
102 testCase.expected = testCase.expected.replace(/(\r\n)|\n|\r/g, '\n');
103 testCase.actual = testCase.actual.replace(/(\r\n)|\n|\r/g, '\n');
104
105 return testCase;
106 }
107
108 module.exports = {
109 getTestSuite: getTestSuite,
110 getHtmlToMdTestSuite: getHtmlToMdTestSuite,
111 assertion: assertion,
112 normalize: normalize,
113 showdown: require('../.build/showdown.js')
114 };
115})();
116