UNPKG

5.9 kBJavaScriptView Raw
1(function (root, factory) {
2 'use strict';
3 if (typeof define === 'function' && define.amd) {
4 define(['power-assert-formatter', 'empower', 'espower-source', 'assert'], factory);
5 } else if (typeof exports === 'object') {
6 factory(require('..'), require('empower'), require('espower-source'), require('assert'));
7 } else {
8 factory(root.powerAssertFormatter, root.empower, root.espowerSource, root.assert);
9 }
10}(this, function (
11 createFormatter,
12 empower,
13 espowerSource,
14 baseAssert
15) {
16 function weave (line) {
17 return espowerSource(line, '/path/to/some_test.js');
18 }
19
20suite('lineSeparator option', function () {
21 function lineSeparatorTest (name, option, expectedSeparator) {
22 var assert = empower(baseAssert, createFormatter(option));
23 test(name, function () {
24 var falsyNum = 0;
25 try {
26 eval(weave('assert(falsyNum);'));
27 } catch (e) {
28 baseAssert.equal(e.name, 'AssertionError');
29 baseAssert.equal(e.message, [
30 '# /path/to/some_test.js:1',
31 '',
32 'assert(falsyNum)',
33 ' | ',
34 ' 0 ',
35 ''
36 ].join(expectedSeparator));
37 }
38 });
39 }
40 lineSeparatorTest('default is LF', {}, '\n');
41 lineSeparatorTest('LF', {lineSeparator: '\n'}, '\n');
42 lineSeparatorTest('CR', {lineSeparator: '\r'}, '\r');
43 lineSeparatorTest('CRLF', {lineSeparator: '\r\n'}, '\r\n');
44});
45
46suite('renderers customization', function () {
47 function rendererCustomizationTest (name, option, expectedLines) {
48 var assert = empower(baseAssert, createFormatter(option));
49 test(name, function () {
50 var hoge = 'foo';
51 var fuga = 'bar';
52 try {
53 eval(weave('assert.ok(hoge === fuga, "comment");'));
54 } catch (e) {
55 baseAssert.equal(e.name, 'AssertionError');
56 baseAssert.equal(e.message, expectedLines.join('\n'));
57 }
58 });
59 }
60
61 rendererCustomizationTest('default', null, [
62 'comment # /path/to/some_test.js:1',
63 '',
64 'assert.ok(hoge === fuga, "comment")',
65 ' | | | ',
66 ' | | "bar" ',
67 ' | false ',
68 ' "foo" ',
69 '',
70 '--- [string] fuga',
71 '+++ [string] hoge',
72 '@@ -1,3 +1,3 @@',
73 '-bar',
74 '+foo',
75 '',
76 ''
77 ]);
78
79 rendererCustomizationTest('without file renderer', {
80 renderers: [
81 './built-in/assertion',
82 './built-in/diagram',
83 './built-in/binary-expression'
84 ]
85 }, [
86 'comment ',
87 'assert.ok(hoge === fuga, "comment")',
88 ' | | | ',
89 ' | | "bar" ',
90 ' | false ',
91 ' "foo" ',
92 '',
93 '--- [string] fuga',
94 '+++ [string] hoge',
95 '@@ -1,3 +1,3 @@',
96 '-bar',
97 '+foo',
98 '',
99 ''
100 ]);
101
102
103 rendererCustomizationTest('without assertion renderer (though it is nonsense)', {
104 renderers: [
105 './built-in/file',
106 './built-in/diagram',
107 './built-in/binary-expression'
108 ]
109 }, [
110 'comment # /path/to/some_test.js:1',
111 ' | | | ',
112 ' | | "bar" ',
113 ' | false ',
114 ' "foo" ',
115 '',
116 '--- [string] fuga',
117 '+++ [string] hoge',
118 '@@ -1,3 +1,3 @@',
119 '-bar',
120 '+foo',
121 '',
122 ''
123 ]);
124
125 rendererCustomizationTest('without diagram renderer', {
126 renderers: [
127 './built-in/file',
128 './built-in/assertion',
129 './built-in/binary-expression'
130 ]
131 }, [
132 'comment # /path/to/some_test.js:1',
133 '',
134 'assert.ok(hoge === fuga, "comment")',
135 '',
136 '--- [string] fuga',
137 '+++ [string] hoge',
138 '@@ -1,3 +1,3 @@',
139 '-bar',
140 '+foo',
141 '',
142 ''
143 ]);
144
145 rendererCustomizationTest('without binary-expression renderer', {
146 renderers: [
147 './built-in/file',
148 './built-in/assertion',
149 './built-in/diagram'
150 ]
151 }, [
152 'comment # /path/to/some_test.js:1',
153 '',
154 'assert.ok(hoge === fuga, "comment")',
155 ' | | | ',
156 ' | | "bar" ',
157 ' | false ',
158 ' "foo" ',
159 ''
160 ]);
161
162
163 (function () {
164 function CustomRenderer (traversal, config) {
165 var assertionLine;
166 traversal.on('start', function (context) {
167 assertionLine = context.source.content;
168 });
169 traversal.on('render', function (writer) {
170 writer.write('');
171 writer.write('## ' + assertionLine + ' ##');
172 });
173 }
174 rendererCustomizationTest('without binary-expression renderer', {
175 renderers: [
176 './built-in/file',
177 CustomRenderer,
178 './built-in/binary-expression'
179 ]
180 }, [
181 'comment # /path/to/some_test.js:1',
182 '',
183 '## assert.ok(hoge === fuga, "comment") ##',
184 '',
185 '--- [string] fuga',
186 '+++ [string] hoge',
187 '@@ -1,3 +1,3 @@',
188 '-bar',
189 '+foo',
190 '',
191 ''
192 ]);
193 })();
194
195});
196
197}));