UNPKG

7.93 kBJavaScriptView Raw
1var createFormatter = require('..');
2var empower = require('empower');
3var baseAssert = require('assert');
4var assert = empower(baseAssert, createFormatter());
5var babel = require('babel-core');
6require('babel-core/polyfill');
7var createEspowerPlugin = require('babel-plugin-espower/create');
8
9function weave (line) {
10 return babel.transform(line, {
11 filename: '/absolute/path/to/project/test/some_test.js',
12 plugins: [
13 createEspowerPlugin(babel, {
14 sourceRoot: '/absolute/path/to/project'
15 })
16 ]
17 }).code;
18}
19
20function assertPowerAssertContextFormatting (body, expectedLines, done) {
21 if (done) {
22 baseAssert.equal(body.length, 1, 'body should accept a "done" callback');
23 body(function (e) {
24 try {
25 if (!e) {
26 baseAssert.fail('AssertionError should be thrown');
27 }
28 baseAssert.equal(e.message, expectedLines.join('\n'));
29 } catch (err) {
30 return done(err);
31 }
32 done();
33 });
34 } else {
35 baseAssert.equal(body.length, 0, 'assertPowerAssertContextFormatting must be passed a "done" callback if "body" needs to run async');
36 try {
37 body();
38 baseAssert.fail('AssertionError should be thrown');
39 } catch (e) {
40 baseAssert.equal(e.message, expectedLines.join('\n'));
41 }
42 }
43}
44
45suite('ES6 features', function () {
46
47 test('TemplateLiteral', function () {
48 var alice = { name: 'alice' };
49 var bob = { name: 'bob' };
50 assertPowerAssertContextFormatting(function () {
51 eval(weave('assert(`${alice.name} and ${bob.name}` === `bob and alice`);'));
52 }, [
53 ' # test/some_test.js:1',
54 ' ',
55 ' assert(`${ alice.name } and ${ bob.name }` === `bob and alice`)',
56 ' | | | | | | | ',
57 ' | | | | | | "bob and alice" ',
58 ' | | | | "bob" false ',
59 ' | | "alice" Object{name:"bob"} ',
60 ' | Object{name:"alice"} ',
61 ' "alice and bob" ',
62 ' ',
63 ' --- [string] `bob and alice`',
64 ' +++ [string] `${ alice.name } and ${ bob.name }`',
65 ' @@ -1,13 +1,13 @@',
66 ' -bob and alice',
67 ' +alice and bob',
68 ' ',
69 ' '
70 ]);
71 });
72
73 test('ArrowFunctionExpression and SpreadElement', function () {
74 var seven = 7, ary = [4, 5];
75 assertPowerAssertContextFormatting(function () {
76 eval(weave('assert(seven === ((v, i) => v + i)(...[...ary]));'));
77 }, [
78 ' # test/some_test.js:1',
79 ' ',
80 ' assert(seven === ((v, i) => v + i)(...[...ary]))',
81 ' | | | | | ',
82 ' | | | | [4,5] ',
83 ' | | 9 [4,5] ',
84 ' 7 false ',
85 ' ',
86 ' [number] ((v, i) => v + i)(...[...ary])',
87 ' => 9',
88 ' [number] seven',
89 ' => 7',
90 ' '
91 ]);
92 });
93
94 test('Enhanced Object Literals', function () {
95 var name = 'bobby';
96 assertPowerAssertContextFormatting(function () {
97 eval(weave('assert.deepEqual({ name, [ `${name} greet` ]: `Hello, I am ${name}` }, null);'));
98 }, [
99 ' # test/some_test.js:1',
100 ' ',
101 ' assert.deepEqual({name,[`${ name } greet`]: `Hello, I am ${ name }`}, null)',
102 ' | | | | | ',
103 ' | | | | "bobby" ',
104 ' | | "bobby" "Hello, I am bobby" ',
105 ' | "bobby greet" ',
106 ' Object{name:"bobby","bobby greet":"Hello, I am bobby"} ',
107 ' '
108 ]);
109 });
110
111 test('Yield Statements', function (done) {
112 assertPowerAssertContextFormatting(function (done) {
113 var big = 'big';
114 eval(weave([
115 'function bigOrSmall(size) {',
116 ' return Promise.resolve(size > 100 ? "big" : "small");',
117 '}',
118 '',
119 'function *myGenerator (input) {',
120 ' assert((yield bigOrSmall(input)) === big);',
121 '}',
122 '',
123 'var gen = myGenerator(3);',
124 'gen.next().value.then((val) => gen.next(val)).catch(done);'
125 ].join('\n')));
126 }, [
127 ' # test/some_test.js:6',
128 ' ',
129 ' assert((yield bigOrSmall(input)) === big)',
130 ' | | | | ',
131 ' | | | "big"',
132 ' "small" 3 false ',
133 ' ',
134 ' --- [string] big',
135 ' +++ [string] yield bigOrSmall(input)',
136 ' @@ -1,3 +1,5 @@',
137 ' -big',
138 ' +small',
139 ' ',
140 ' '
141 ], done);
142 });
143
144 test('Async/Await Statements', function (done) {
145 assertPowerAssertContextFormatting(function (done) {
146 var big = 'big';
147
148 eval(weave([
149 'function bigOrSmall(size) {',
150 ' return Promise.resolve(size > 100 ? "big" : "small");',
151 '}',
152 '',
153 'async function isBig (input) {',
154 ' assert((await (bigOrSmall(input))) === big);',
155 '}',
156 '',
157 'isBig(4).catch(done);'
158 ].join('\n')));
159 }, [
160 ' # test/some_test.js:6',
161 ' ',
162 ' assert((await bigOrSmall(input)) === big)',
163 ' | | | | ',
164 ' | | | "big"',
165 ' "small" 4 false ',
166 ' ',
167 ' --- [string] big',
168 ' +++ [string] await bigOrSmall(input)',
169 ' @@ -1,3 +1,5 @@',
170 ' -big',
171 ' +small',
172 ' ',
173 ' '
174 ], done);
175 });
176
177 test('await() - function call disambiguation', function () {
178 assertPowerAssertContextFormatting(function () {
179 var big = 'big';
180
181 function await(val) {
182 return '...' + val;
183 }
184
185 eval(weave([
186 'function bigOrSmall(size) {',
187 ' return size > 100 ? "big" : "small";',
188 '}',
189 '',
190 'function isBig (input) {',
191 ' assert((await (bigOrSmall(input))) === big);',
192 '}',
193 '',
194 'isBig(4);'
195 ].join('\n')));
196 }, [
197 ' # test/some_test.js:6',
198 ' ',
199 ' assert(await(bigOrSmall(input)) === big)',
200 ' | | | | | ',
201 ' | | | | "big"',
202 ' | "small" 4 false ',
203 ' "...small" ',
204 ' ',
205 ' --- [string] big',
206 ' +++ [string] await(bigOrSmall(input))',
207 ' @@ -1,3 +1,8 @@',
208 ' -big',
209 ' +...small',
210 ' ',
211 ' '
212 ]);
213 });
214});