-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunitTests.js
176 lines (137 loc) · 5.7 KB
/
unitTests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* jshint expr: true */
var fs = require('fs');
var rewire = require('rewire');
require('should');
var sinon = require('sinon');
var mdlint = rewire('../index.js');
describe('mdlint', function () {
describe('lintMarkdown', function () {
it('should log the filename of passing files when the verbose flag is set', function () {
var consoleSpy = sinon.stub(console, 'log');
var file = 'goodsyntax.md';
mdlint.__set__('program', { verbose: true });
mdlint.__get__('lintMarkdown')('', file);
consoleSpy.firstCall.args[0].should.include(file);
console.log.restore();
});
it('should log a message that the linting passed if all code blocks pass validation', function () {
var consoleSpy = sinon.stub(console, 'log');
var file = 'goodsyntax.md';
mdlint.__set__('program', { verbose: false });
mdlint.__set__('numFilesToParse', 1);
mdlint.__get__('lintMarkdown')(fs.readFileSync('test/fixtures/' + file, 'utf8'), file);
consoleSpy.firstCall.args[0].should.include('All markdown files passed linting');
console.log.restore();
});
it('should log a message that the linting failed if any code block fails validation', function () {
var consoleSpy = sinon.stub(console, 'log');
mdlint.__get__('lintMarkdown')(fs.readFileSync('test/fixtures/syntaxerror.md', 'utf8'), 'filename.md');
consoleSpy.lastCall.args[0].should.eql('');
console.log.restore();
});
});
describe('logFileBreak', function () {
it('should log yellow filename blocks on even failing markdown files', function () {
var consoleSpy = sinon.stub(console, 'log');
mdlint.__set__('numFailedFiles', 0);
mdlint.__get__('logFileBreak')('README.md');
consoleSpy.lastCall.args[0].should.eql('\u001b[7m\u001b[33mREADME.md\u001b[39m\u001b[27m');
console.log.restore();
});
it('should log blue filename blocks on odd failing markdown files', function () {
var consoleSpy = sinon.stub(console, 'log');
mdlint.__set__('numFailedFiles', 1);
mdlint.__get__('logFileBreak')('README.md');
consoleSpy.lastCall.args[0].should.eql('\u001b[7m\u001b[34mREADME.md\u001b[39m\u001b[27m');
console.log.restore();
});
});
describe('parseMarkdown', function () {
it('should extract the language and code from a markdown code block', function () {
var codeBlocks = mdlint.__get__('parseMarkdown')(fs.readFileSync('test/fixtures/goodsyntax.md', 'utf8'));
codeBlocks.should.eql([{
lang: 'js',
code: 'var x = {\n key: \'value\',\n key2: \'value2\'\n}\n'
}, {
lang: 'javascript',
code: 'console.log(\'Captain\\\'s log\');\n'
}, {
lang: 'json',
code: '{\n "key": "prop"\n}'
}]);
});
});
describe('validateCodeBlock', function () {
describe('when parsing JSON code blocks', function () {
describe('if the JSON cannot be parsed', function () {
it('should return false', function () {
sinon.stub(console, 'log');
mdlint.__get__('validateCodeBlock')({
lang: 'json',
code: 'aahkkh{key: a\'value\'}'
}).should.not.be.ok;
console.log.restore();
});
it('should log an error with the error output from JSON.parse', function () {
var consoleSpy = sinon.stub(console, 'log');
mdlint.__get__('validateCodeBlock')({
lang: 'json',
code: '{key: "value"}'
});
consoleSpy.lastCall.args[0].should.include('{key: "value"}');
console.log.restore();
});
});
it('should return true if the JSON is parsed successfully', function () {
mdlint.__get__('validateCodeBlock')({
lang: 'json',
code: '{"key": "value"}'
}).should.be.ok;
});
});
describe('when parsing JavaScript code blocks', function () {
describe('if the JavaScript cannot be parsed', function () {
it('should return false', function () {
sinon.stub(console, 'log');
mdlint.__get__('validateCodeBlock')({
lang: 'js',
code: 'var x = ;\'test\';'
}).should.not.be.ok;
console.log.restore();
});
it('should log an error including the code block', function () {
var consoleSpy = sinon.stub(console, 'log');
mdlint.__get__('validateCodeBlock')({
lang: 'js',
code: 'var x = ;\'test\';'
});
consoleSpy.lastCall.args[0].should.include('var x');
console.log.restore();
});
});
it('should return true if the JavaScript is parsed successfully', function () {
mdlint.__get__('validateCodeBlock')({
lang: 'javascript',
code: 'var x = \'test\';'
}).should.be.ok;
});
});
});
describe('preprocessCode', function () {
it('should remove comments from the beginning of code blocks', function () {
mdlint.__get__('preprocessCode')('//\nvar x = 1;').should.eql('\nvar x = 1;');
});
it('should add a variable declaration to object literals', function () {
mdlint.__get__('preprocessCode')('{}').should.eql('var json = {}');
});
it('should wrap a lone object property with a valid object', function () {
mdlint.__get__('preprocessCode')('gruntplugin: {}').should.eql('var json = {gruntplugin: {}}');
});
it('should add a variable declaration to anonymous functions', function () {
mdlint.__get__('preprocessCode')('function () {}').should.eql('var func = function () {}');
});
it('should replace ... with an empty string', function () {
mdlint.__get__('preprocessCode')('...').should.eql('');
});
});
});