/
function-type.spec.js
214 lines (190 loc) · 7.28 KB
/
function-type.spec.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*global expect*/
describe('function type', function () {
it('should inspect an empty anonymous function correctly', function () {
expect(
function () {},
'to inspect as',
'function () {}'
);
});
it('should inspect an empty named function correctly', function () {
expect(
function foo() {},
'to inspect as',
'function foo() {}'
);
});
var isNodeJs3OrBelow = typeof process === 'object' && /^v[0123]\./.test(process.version);
var isPhantomJs = typeof navigator !== 'undefined' && /phantom/i.test(navigator.userAgent);
if (!isNodeJs3OrBelow && !isPhantomJs) {
// Node.js 3 and below and phantom.js don't include "bound " and the
// old Phantom.js version that's currently available on Travis doesn't
// even have Function#bind, which we then polyfill via es5-shim, leading
// to even more different output.
// For now let's just disable these tests in those environments
it('should inspect an anonymous bound function correctly', function () {
expect(
function () {}.bind({}),
'to inspect as',
'function bound () { /* native code */ }'
);
});
it('should inspect a named bound function correctly', function () {
expect(
function foo() {}.bind({}),
'to inspect as',
'function bound foo() { /* native code */ }'
);
});
}
it('should inspect an function with just a newline correctly', function () {
expect(function () {}, 'to inspect as', 'function () {}');
});
it('should inspect a one-line function correctly', function () {
/* eslint-disable no-unused-vars */
expect(function () {
var a = 123; a = 456;
}, 'to inspect as', 'function () { var a = 123; a = 456; }');
/* eslint-enable no-unused-vars */
});
it('should inspect a short one-line function with leading and trailing newline correctly', function () {
/* eslint-disable no-unused-vars */
expect(function () {
var a = 123; a = 456;
}, 'to inspect as',
'function () { var a = 123; a = 456; }'
);
/* eslint-enable no-unused-vars */
});
it('should inspect a long one-line function with leading and trailing newline correctly', function () {
/* eslint-disable no-unused-vars */
expect(function () {
var a = 123 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2; a = 456;
}, 'to inspect as',
'function () {\n' +
' var a = 123 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2; a = 456;\n' +
'}'
);
/* eslint-enable no-unused-vars */
});
/* eslint-disable no-unused-vars */
function singleLineWithComment() {
var a = 123; a = 456; // foo
}
/* eslint-enable no-unused-vars */
var phantomJsBug = singleLineWithComment.toString().indexOf('// foo;') !== -1;
if (!phantomJsBug) {
it('should inspect a short one-line function with leading and trailing newline correctly and a C++-style comment correctly', function () {
/* eslint-disable no-unused-vars */
expect(function () {
var a = 123; a = 456; // foo
}, 'to inspect as',
'function () {\n' +
' var a = 123; a = 456; // foo\n' +
'}'
);
/* eslint-enable no-unused-vars */
});
}
it('should reindent a function with an indentation size of 4', function () {
expect(function () {
var a = 4;
if (a === 1) {
a();
}
}, 'to inspect as',
'function () {\n' +
' var a = 4;\n' +
' if (a === 1) {\n' +
' a();\n' +
' }\n' +
'}'
);
});
it('should reindent a function with an indentation size of 3', function () {
// jscs:disable
expect(function () {
var a = 4;
if (a === 1) {
a();
}
}, 'to inspect as',
'function () {\n' +
' var a = 4;\n' +
' if (a === 1) {\n' +
' a();\n' +
' }\n' +
'}');
// jscs:enable
});
it('should reindent a function with an indentation size of 1', function () {
// jscs:disable
expect(function () {
var a = 4;
if (a === 1) {
a();
}
}, 'to inspect as',
'function () {\n' +
' var a = 4;\n' +
' if (a === 1) {\n' +
' a();\n' +
' }\n' +
'}');
// jscs:enable
});
// We can't complete this test if the runtime doesn't support arrow functions:
var singleParamArrowFunction;
try {
singleParamArrowFunction = new Function('return a => a + 1;')();
} catch (e) {}
if (singleParamArrowFunction) {
it('should render a single param arrow function', function () {
expect(singleParamArrowFunction, 'to inspect as', 'a => a + 1');
});
}
// We can't complete this test if the runtime doesn't support arrow functions:
var implicitReturnMultilineArrowFunction;
try {
implicitReturnMultilineArrowFunction = new Function(
'return a => \n a + 1;'
)();
} catch (e) {}
if (implicitReturnMultilineArrowFunction) {
it('should render an implicit return multiline arrow function', function () {
expect(implicitReturnMultilineArrowFunction, 'to inspect as', 'a => \n a + 1');
});
}
// We can't complete this test if the runtime doesn't support arrow functions:
var evilImplicitReturnMultilineArrowFunction;
try {
evilImplicitReturnMultilineArrowFunction = new Function(
'return a => \n a || {};'
)();
} catch (e) {}
if (evilImplicitReturnMultilineArrowFunction) {
it('should render an implicit return multiline arrow function with an evil alternation', function () {
expect(evilImplicitReturnMultilineArrowFunction, 'to inspect as', 'a => \n a || {}');
});
}
// We can't complete this test if the runtime doesn't support arrow functions:
var multiParamArrowFunction;
try {
multiParamArrowFunction = new Function('return (a, b) => a + b;')();
} catch (e) {}
if (multiParamArrowFunction) {
it('should render a multi param arrow function', function () {
expect(multiParamArrowFunction, 'to inspect as', '(a, b) => a + b');
});
}
// We can't complete this test if the runtime doesn't support the async keyword:
var asyncFunction;
try {
asyncFunction = new Function('return async function foo(a) {return a + 1;}')();
} catch (e) {}
if (asyncFunction) {
it('should render "async" before an AsyncFunction instance', function () {
expect(asyncFunction, 'to inspect as', 'async function foo(a) { return a + 1; }');
});
}
});