UNPKG

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