utatane.tea@gmail.com | fdd9bf4 | 2015-12-02 03:16:28 +0000 | [diff] [blame] | 1 | function shouldBe(actual, expected) { |
| 2 | if (actual !== expected) |
| 3 | throw new Error('bad value: ' + actual); |
| 4 | } |
| 5 | |
| 6 | function shouldThrow(func, errorMessage) { |
| 7 | var errorThrown = false; |
| 8 | var error = null; |
| 9 | try { |
| 10 | func(); |
| 11 | } catch (e) { |
| 12 | errorThrown = true; |
| 13 | error = e; |
| 14 | } |
| 15 | if (!errorThrown) |
| 16 | throw new Error('not thrown'); |
| 17 | if (String(error) !== errorMessage) |
| 18 | throw new Error(`bad error: ${String(error)}`); |
| 19 | } |
| 20 | |
| 21 | function *gen() { |
| 22 | yield eval("this"); |
| 23 | } |
| 24 | |
utatane.tea@gmail.com | fdd9bf4 | 2015-12-02 03:16:28 +0000 | [diff] [blame] | 25 | shouldThrow(() => { |
utatane.tea@gmail.com | 552595b | 2015-12-29 11:49:35 +0000 | [diff] [blame] | 26 | var g = new gen(); |
utatane.tea@gmail.com | fdd9bf4 | 2015-12-02 03:16:28 +0000 | [diff] [blame] | 27 | g.next().value; |
utatane.tea@gmail.com | 552595b | 2015-12-29 11:49:35 +0000 | [diff] [blame] | 28 | }, `TypeError: function is not a constructor (evaluating 'new gen()')`); |
utatane.tea@gmail.com | fdd9bf4 | 2015-12-02 03:16:28 +0000 | [diff] [blame] | 29 | |
| 30 | class B { } |
| 31 | |
| 32 | (function() { |
| 33 | eval('this'); |
| 34 | eval('this'); |
| 35 | }()); |
| 36 | |
| 37 | class A extends B { |
| 38 | constructor() |
| 39 | { |
| 40 | return eval('this'); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | shouldThrow(() => { |
| 45 | new A(); |
| 46 | }, `ReferenceError: Cannot access uninitialized variable.`); |
| 47 | |
| 48 | class C { |
| 49 | *generator() |
| 50 | { |
| 51 | yield eval('this'); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | shouldThrow(() => { |
| 56 | let c = new C(); |
| 57 | let g = new c.generator(); |
| 58 | g.next(); |
| 59 | }, `TypeError: function is not a constructor (evaluating 'new c.generator()')`); |
| 60 | |
| 61 | (function () { |
| 62 | let c = new C(); |
| 63 | let g = c.generator(); |
| 64 | shouldBe(g.next().value, c); |
| 65 | }()); |