UNPKG

10.1 kBJavaScriptView Raw
1(function (root, factory) {
2 'use strict';
3 if (typeof define === 'function' && define.amd) {
4 define(['power-assert-formatter', 'empower', 'espower', 'esprima', 'escodegen', 'assert'], factory);
5 } else if (typeof exports === 'object') {
6 factory(require('..'), require('empower'), require('espower'), require('esprima'), require('escodegen'), require('assert'));
7 } else {
8 factory(root.powerAssertFormatter, root.empower, root.espower, root.esprima, root.escodegen, root.assert);
9 }
10}(this, function (
11 createFormatter,
12 empower,
13 espower,
14 esprima,
15 escodegen,
16 baseAssert
17) {
18
19 function weave (line) {
20 var filepath = '/absolute/path/to/project/test/some_test.js';
21 var sourceRoot = '/absolute/path/to/project';
22 var options = {sourceType: 'module', tolerant: true, loc: true, tokens: true, raw: true};
23 var jsAST = esprima.parse(line, options);
24 var espoweredAST = espower(jsAST, {source: line, path: filepath, sourceRoot: sourceRoot});
25 return escodegen.generate(espoweredAST, {format: {compact: true}});
26 }
27
28suite('lineSeparator option', function () {
29 function lineSeparatorTest (name, option, expectedSeparator) {
30 var assert = empower(baseAssert, createFormatter(option));
31 test(name, function () {
32 var falsyNum = 0;
33 try {
34 eval(weave('assert(falsyNum);'));
35 } catch (e) {
36 baseAssert.equal(e.message, [
37 ' # test/some_test.js:1',
38 ' ',
39 ' assert(falsyNum)',
40 ' | ',
41 ' 0 ',
42 ' '
43 ].join(expectedSeparator));
44 baseAssert.equal(e.name, 'AssertionError');
45 }
46 });
47 }
48 lineSeparatorTest('default is LF', {}, '\n');
49 lineSeparatorTest('LF', {lineSeparator: '\n'}, '\n');
50 lineSeparatorTest('CR', {lineSeparator: '\r'}, '\r');
51 lineSeparatorTest('CRLF', {lineSeparator: '\r\n'}, '\r\n');
52});
53
54
55suite('outputOffset option', function () {
56 function outputOffsetCustomizationTest (option, expectedLines) {
57 var assert = empower(baseAssert, createFormatter(option));
58 test(JSON.stringify(option), function () {
59 var hoge = 'foo';
60 var fuga = 'bar';
61 try {
62 eval(weave('assert.ok(hoge === fuga, "comment");'));
63 } catch (e) {
64 var actual = e.message.split(createFormatter.defaultOptions().lineSeparator);
65 baseAssert.deepEqual(actual, expectedLines);
66 baseAssert.equal(e.name, 'AssertionError');
67 }
68 });
69 }
70 outputOffsetCustomizationTest({outputOffset: 1}, [
71 'comment # test/some_test.js:1',
72 ' ',
73 ' assert.ok(hoge === fuga, "comment")',
74 ' | | | ',
75 ' | | "bar" ',
76 ' | false ',
77 ' "foo" ',
78 ' ',
79 ' --- [string] fuga',
80 ' +++ [string] hoge',
81 ' @@ -1,3 +1,3 @@',
82 ' -bar',
83 ' +foo',
84 ' ',
85 ' '
86 ]);
87 outputOffsetCustomizationTest({outputOffset: 3}, [
88 'comment # test/some_test.js:1',
89 ' ',
90 ' assert.ok(hoge === fuga, "comment")',
91 ' | | | ',
92 ' | | "bar" ',
93 ' | false ',
94 ' "foo" ',
95 ' ',
96 ' --- [string] fuga',
97 ' +++ [string] hoge',
98 ' @@ -1,3 +1,3 @@',
99 ' -bar',
100 ' +foo',
101 ' ',
102 ' '
103 ]);
104});
105
106
107suite('renderers customization', function () {
108 function rendererCustomizationTest (name, option, expectedLines) {
109 var assert = empower(baseAssert, createFormatter(option));
110 test(name, function () {
111 var hoge = 'foo';
112 var fuga = 'bar';
113 try {
114 eval(weave('assert.ok(hoge === fuga, "comment");'));
115 } catch (e) {
116 baseAssert.equal(e.message, expectedLines.join('\n'));
117 baseAssert.equal(e.name, 'AssertionError');
118 }
119 });
120 }
121
122 rendererCustomizationTest('default', null, [
123 'comment # test/some_test.js:1',
124 ' ',
125 ' assert.ok(hoge === fuga, "comment")',
126 ' | | | ',
127 ' | | "bar" ',
128 ' | false ',
129 ' "foo" ',
130 ' ',
131 ' --- [string] fuga',
132 ' +++ [string] hoge',
133 ' @@ -1,3 +1,3 @@',
134 ' -bar',
135 ' +foo',
136 ' ',
137 ' '
138 ]);
139
140 rendererCustomizationTest('without file renderer', {
141 renderers: [
142 './built-in/assertion',
143 './built-in/diagram',
144 './built-in/binary-expression'
145 ]
146 }, [
147 'comment ',
148 ' assert.ok(hoge === fuga, "comment")',
149 ' | | | ',
150 ' | | "bar" ',
151 ' | false ',
152 ' "foo" ',
153 ' ',
154 ' --- [string] fuga',
155 ' +++ [string] hoge',
156 ' @@ -1,3 +1,3 @@',
157 ' -bar',
158 ' +foo',
159 ' ',
160 ' '
161 ]);
162
163
164 rendererCustomizationTest('without assertion renderer (though it is nonsense)', {
165 renderers: [
166 './built-in/file',
167 './built-in/diagram',
168 './built-in/binary-expression'
169 ]
170 }, [
171 'comment # test/some_test.js:1',
172 ' | | | ',
173 ' | | "bar" ',
174 ' | false ',
175 ' "foo" ',
176 ' ',
177 ' --- [string] fuga',
178 ' +++ [string] hoge',
179 ' @@ -1,3 +1,3 @@',
180 ' -bar',
181 ' +foo',
182 ' ',
183 ' '
184 ]);
185
186 rendererCustomizationTest('without diagram renderer', {
187 renderers: [
188 './built-in/file',
189 './built-in/assertion',
190 './built-in/binary-expression'
191 ]
192 }, [
193 'comment # test/some_test.js:1',
194 ' ',
195 ' assert.ok(hoge === fuga, "comment")',
196 ' ',
197 ' --- [string] fuga',
198 ' +++ [string] hoge',
199 ' @@ -1,3 +1,3 @@',
200 ' -bar',
201 ' +foo',
202 ' ',
203 ' '
204 ]);
205
206 rendererCustomizationTest('without binary-expression renderer', {
207 renderers: [
208 './built-in/file',
209 './built-in/assertion',
210 './built-in/diagram'
211 ]
212 }, [
213 'comment # test/some_test.js:1',
214 ' ',
215 ' assert.ok(hoge === fuga, "comment")',
216 ' | | | ',
217 ' | | "bar" ',
218 ' | false ',
219 ' "foo" ',
220 ' '
221 ]);
222
223
224 (function () {
225 function CustomRenderer (traversal, config) {
226 var assertionLine;
227 traversal.on('start', function (context) {
228 assertionLine = context.source.content;
229 });
230 traversal.on('render', function (writer) {
231 writer.write('');
232 writer.write('## ' + assertionLine + ' ##');
233 });
234 }
235 rendererCustomizationTest('with old-style custom renderer', {
236 renderers: [
237 './built-in/file',
238 CustomRenderer,
239 './built-in/binary-expression'
240 ]
241 }, [
242 'comment # test/some_test.js:1',
243 ' ',
244 ' ## assert.ok(hoge === fuga, "comment") ##',
245 ' ',
246 ' --- [string] fuga',
247 ' +++ [string] hoge',
248 ' @@ -1,3 +1,3 @@',
249 ' -bar',
250 ' +foo',
251 ' ',
252 ' '
253 ]);
254 })();
255
256
257 (function () {
258 function NewCustomRenderer (config) {
259 }
260 NewCustomRenderer.prototype.init = function (traversal) {
261 var assertionLine;
262 traversal.on('start', function (context) {
263 assertionLine = context.source.content;
264 });
265 traversal.on('render', function (writer) {
266 writer.write('');
267 writer.write('$$ ' + assertionLine + ' $$');
268 });
269 };
270 rendererCustomizationTest('with new-style custom renderer', {
271 renderers: [
272 './built-in/file',
273 NewCustomRenderer,
274 './built-in/binary-expression'
275 ]
276 }, [
277 'comment # test/some_test.js:1',
278 ' ',
279 ' $$ assert.ok(hoge === fuga, "comment") $$',
280 ' ',
281 ' --- [string] fuga',
282 ' +++ [string] hoge',
283 ' @@ -1,3 +1,3 @@',
284 ' -bar',
285 ' +foo',
286 ' ',
287 ' '
288 ]);
289 })();
290
291
292 rendererCustomizationTest('with exposed Renderer classes', {
293 renderers: [
294 createFormatter.renderers.FileRenderer,
295 createFormatter.renderers.AssertionRenderer,
296 createFormatter.renderers.DiagramRenderer,
297 createFormatter.renderers.BinaryExpressionRenderer
298 ]
299 }, [
300 'comment # test/some_test.js:1',
301 ' ',
302 ' assert.ok(hoge === fuga, "comment")',
303 ' | | | ',
304 ' | | "bar" ',
305 ' | false ',
306 ' "foo" ',
307 ' ',
308 ' --- [string] fuga',
309 ' +++ [string] hoge',
310 ' @@ -1,3 +1,3 @@',
311 ' -bar',
312 ' +foo',
313 ' ',
314 ' '
315 ]);
316});
317
318}));