]>
Commit | Line | Data |
---|---|---|
1 | function shouldBe(actual, expected) { | |
2 | if (actual !== expected) | |
3 | throw new Error('bad value: ' + actual); | |
4 | } | |
5 | ||
6 | function shouldThrow(func, message) { | |
7 | var error = null; | |
8 | try { | |
9 | func(); | |
10 | } catch (e) { | |
11 | error = e; | |
12 | } | |
13 | if (!error) | |
14 | throw new Error("not thrown."); | |
15 | if (String(error) !== message) | |
16 | throw new Error("bad error: " + String(error)); | |
17 | } | |
18 | ||
19 | var array = new Array(10); | |
20 | ||
21 | for (var i = 0; i < 10; ++i) { | |
22 | (function (index) { | |
23 | var seenOnce = false; | |
24 | Object.defineProperty(array, index, { | |
25 | get() { | |
26 | if (seenOnce) | |
27 | throw new Error('get is called.' + index); | |
28 | seenOnce = true; | |
29 | return index; | |
30 | } | |
31 | }); | |
32 | }(i)); | |
33 | } | |
34 | ||
35 | shouldBe(array.length, 10); | |
36 | ||
37 | // Doesn't throw any errors. | |
38 | var findValue = array.find(function (value) { | |
39 | return value === 9; | |
40 | }); | |
41 | shouldBe(findValue, 9); | |
42 | ||
43 | for (var i = 0; i < 10; ++i) { | |
44 | shouldThrow(function () { | |
45 | var value = array[i]; | |
46 | }, "Error: get is called." + i); | |
47 | } |