-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.test.js
More file actions
25 lines (18 loc) · 853 Bytes
/
ex2.test.js
File metadata and controls
25 lines (18 loc) · 853 Bytes
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
'use strict';
const { makeIndexFns } = require('./ex2');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
const fns = makeIndexFns(3);
assert(fns.length === 3, 'should return 3 functions');
assert(fns[0]() === 0, 'fn[0] should return 0');
assert(fns[1]() === 1, 'fn[1] should return 1');
assert(fns[2]() === 2, 'fn[2] should return 2');
assert(fns[2]() === 2 && fns[0]() === 0, 'order of calls should not matter');
const empty = makeIndexFns(0);
assert(Array.isArray(empty) && empty.length === 0, 'n=0 should return empty array');
for (const fn of fns) {
assert(!Object.prototype.hasOwnProperty.call(fn, 'index'), 'should not store index on function');
assert(!Object.prototype.hasOwnProperty.call(fn, 'i'), 'should not store loop var on function');
}
console.log('ex2 tests passed');