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: ${String(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 | class A { |
| 22 | *gen() |
| 23 | { |
| 24 | yield this; |
| 25 | yield this; |
| 26 | return 42; |
| 27 | } |
| 28 | |
| 29 | static *staticGen() |
| 30 | { |
| 31 | yield this; |
| 32 | yield this; |
| 33 | return 42; |
| 34 | } |
| 35 | } |
| 36 | { |
| 37 | let a = new A(); |
| 38 | let g = a.gen(); |
| 39 | shouldBe(g.next().value, a); |
| 40 | shouldBe(g.next().value, a); |
| 41 | shouldBe(g.next().value, 42); |
| 42 | shouldBe(g.next().done, true); |
| 43 | } |
| 44 | { |
| 45 | let a = new A(); |
| 46 | shouldThrow(() => { |
| 47 | let g = new a.gen(); |
| 48 | }, `TypeError: function is not a constructor (evaluating 'new a.gen()')`); |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | let g = A.staticGen(); |
| 53 | shouldBe(g.next().value, A); |
| 54 | shouldBe(g.next().value, A); |
| 55 | shouldBe(g.next().value, 42); |
| 56 | shouldBe(g.next().done, true); |
| 57 | } |
| 58 | { |
| 59 | shouldThrow(() => { |
| 60 | let g = new A.staticGen(); |
| 61 | }, `TypeError: function is not a constructor (evaluating 'new A.staticGen()')`); |
| 62 | } |