forked from mixi-inc/JavaScriptTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.js
More file actions
399 lines (267 loc) · 9.18 KB
/
tests.js
File metadata and controls
399 lines (267 loc) · 9.18 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
describe('ステージ7(よくあるJSのイディオムを読める)', function() {
'use strict';
describe('クロージャー', function() {
var createCounter = function() {
var i = 0;
return function() {
return i++;
};
};
var counter = createCounter();
it('1回目の値がわかる', function() {
expect(counter()).to.equal(0);
});
it('2回目の値がわかる', function() {
expect(counter()).to.equal(1);
});
it('3回目の値がわかる', function() {
expect(counter()).to.equal(2);
});
});
describe('ショートサーキット演算', function() {
it("true && 'default' の結果がわかる", function() {
expect(true && 'default').to.equal('default');
});
it("false || 'default' の結果がわかる", function() {
expect(false || 'default').to.equal('default');
});
it("0 || 'default' の結果がわかる", function() {
expect(0 || 'default').to.equal('default');
});
it("{} || 'default' の結果がわかる", function() {
expect({} || 'default').to.deep.equal({});
});
it('default 引数コードが読める1', function() {
var func = function(arg) {
return arg || { foo: 'foo' };
};
expect(func({ foo: 'bar' })).to.deep.equal({ foo: 'bar' });
});
it('default 引数コードが読める2', function() {
var func = function(arg) {
return arg || { foo: 'foo' };
};
expect(func()).to.deep.equal({ foo: 'foo' });
});
});
it('無名即時実行関数が読める', function() {
var num = 0;
(function() {
num = 1;
})();
expect(num).to.equal(1);
});
it('setTimeout 0 パターンが読める', function() {
var num = 0;
setTimeout(function() {
num = 1;
}, 0);
expect(num).to.equal(0);
});
describe('真偽値変換', function() {
var truthy = 1;
var falsey = 0;
it('!!truthy の結果がわかる', function() {
expect(!!truthy).to.equal(true);
});
it('!!falsey の結果がわかる', function() {
expect(!!falsey).to.equal(false);
});
it('Boolean(truthy) の結果がわかる', function() {
expect(Boolean(truthy)).to.equal(true);
});
it('Boolean(falsey) の結果がわかる', function() {
expect(Boolean(falsey)).to.equal(false);
});
});
describe('prototype 継承', function() {
var GrandParent = function() {
this.grandParent = true;
};
var Parent = function() {
this.parent = true;
};
Parent.prototype = new GrandParent();
var Child = function() {
this.child = true;
};
Child.prototype = new Parent();
var parent = new Parent();
var child = new Child();
it('parent.grandParent の値がわかる', function() {
expect(parent.grandParent).to.equal(true);
});
it('parent.child の値がわかる', function() {
expect(parent.child).to.equal(undefined);
});
it('child.grandParent の値がわかる', function() {
expect(child.grandParent).to.equal(true);
});
it('child.parent の値がわかる', function() {
expect(child.parent).to.equal(true);
});
});
});
describe('あなたの闇のJS力', function() {
// .skip を外せば始められます
describe('ゆるふわ == 演算子', function() {
it("'10' == 10 の振る舞いがわかる", function() {
expect('10' == 10)
.to.equal(true);
});
it('null == undefined の振る舞いがわかる', function() {
expect(null == undefined)
.to.equal(true);
});
it('null == false の振る舞いがわかる', function() {
expect(null == false)
.to.equal(false);
});
it('true == 1 の振る舞いがわかる', function() {
expect(true == 1)
.to.equal(true);
});
it('true == 10 の振る舞いがわかる', function() {
expect(true == 10)
.to.equal(false);
});
it('[0, 1] == 0 の振る舞いがわかる', function() {
expect([0, 1] == 0)
.to.equal(false);
});
it('[1] == 1 の振る舞いがわかる', function() {
expect([1] == 1)
.to.equal(true);
});
});
describe('意図しない truthy/falsey', function() {
it('Boolean(false) が truthy/falsey のどちらなのかわかる', function() {
expect(Boolean(false) ? true : false)
.to.equal(false);
});
it('Boolean(0) が truthy/falsey のどちらなのかわかる', function() {
expect(Boolean(0) ? true : false)
.to.equal(false);
});
it('new Boolean(0) が truthy/falsey のどちらなのかわかる', function() {
expect(new Boolean(0) ? true : false)
.to.equal(true);
});
it("'abc' が truthy/falsey のどちらなのかわかる", function() {
expect('abc' ? true : false)
.to.equal(true);
});
it("'' が truthy/falsey のどちらなのかわかる", function() {
expect('' ? true : false)
.to.equal(false);
});
it('String(0) が truthy/falsey のどちらなのかわかる', function() {
expect(String(0) ? true : false)
.to.equal(true);
});
it("String('') が truthy/falsey のどちらなのかわかる", function() {
expect(String('') ? true : false)
.to.equal(false);
});
it('new String(0) が truthy/falsey のどちらなのかわかる', function() {
expect(new String(0) ? true : false)
.to.equal(true);
});
it("new String('') が truthy/falsey のどちらなのかわかる", function() {
expect(new String('') ? true : false)
.to.equal(true);
});
});
describe('読めないキャスト万歳!', function() {
it("10 + '' の値がわかる", function() {
expect(10 + '').to.equal('10');
});
it("+'10' の値がわかる", function() {
expect(+'10').to.equal(10);
});
it("'10.1'|0 の値がわかる", function() {
expect('10.1'|0).to.equal(10);
});
it("Array.prototype.slice.call({ length: 2, 0: 'foo', 1: 'bar' }) の値がわかる", function() {
var obj = { length: 2, 0: 'foo', 1: 'bar' };
expect(Array.prototype.slice.call(obj))
.to.deep.equal([ 'foo', 'bar' ]);
});
it('+(new Date()) の型がわかる', function() {
expect(typeof +(new Date()))
.to.equal('number');
});
it('(new Date()) + 0 の型がわかる', function() {
expect(typeof ((new Date()) + 0))
.to.equal('string');
});
});
describe('ダブルスタンダード Array コンストラクタ', function() {
it('Array(3) が作成する配列の長さがわかる', function() {
expect(Array(3)).to.have.length(3);
});
it('Array(3) が作成する配列の0番目の要素がわかる', function() {
expect(Array(3)[0]).to.equal(undefined);
});
it('Array(3) が作成する配列がわかる', function() {
var array = [];
array.length = 3;
expect(Array(3)).to.deep.equal(array);
});
it('Array(1, 2, 3) が作成する配列の長さがわかる', function() {
expect(Array(1, 2, 3)).to.have.length(3);
});
it('Array(1, 2, 3) が作成する配列の0番目の要素がわかる', function() {
expect(Array(1, 2, 3)[0]).to.equal(1);
});
it('Array(1, 2, 3) が作成する配列がわかる', function() {
expect(Array(1, 2, 3)).to.deep.equal([1, 2, 3]);
});
});
describe('静的検査スレイヤー with 文', function() {
it("with ({ 'foo': 'bar' }) のもとで foo の値がわかる", function() {
var obj = { foo: 'bar' };
with (obj) {
expect(foo).to.equal('bar');
}
});
it("with ({ 'foo': 'bar', 'undefined': 'bar' }) のもとで foo === undefined の結果がわかる", function() {
var obj = { 'foo': 'bar', 'undefined': 'bar' };
with (obj) {
expect(foo === undefined).to.equal(true);
}
});
it("with ({ 'foo': 'bar', 'null': 'bar' }) のもとで foo === null の結果がわかる", function() {
var obj = { 'foo': 'bar', 'null': 'bar' };
with (obj) {
expect(foo === null).to.equal(false);
}
});
});
describe('JavaScript のゆるふわ型判定', function() {
it('typeof 0 の結果がわかる', function() {
expect(typeof 0).to.equal('number');
});
it('typeof true の結果がわかる', function() {
expect(typeof true).to.equal('boolean');
});
it("typeof 'string' の結果がわかる", function() {
expect(typeof 'string').to.equal('string');
});
it('typeof function() {} の結果がわかる', function() {
expect(typeof function() {}).to.equal('function');
});
it('typeof {} の結果がわかる', function() {
expect(typeof {}).to.equal('object');
});
it('typeof [] の結果がわかる', function() {
expect(typeof []).to.equal('object');
});
it('typeof undefined の結果がわかる', function() {
expect(typeof undefined).to.equal('undefined');
});
it('typeof null の結果がわかる', function() {
expect(typeof null).to.equal('object');
});
});
});