utatane.tea@gmail.com | a977ed8 | 2015-08-13 17:25:01 +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 | var proto = {}; |
| 22 | var object = Object.preventExtensions(Object.create(proto)); |
| 23 | |
| 24 | shouldBe(Object.setPrototypeOf(object, proto), object); |
| 25 | shouldThrow(() => { |
| 26 | Object.setPrototypeOf(object, {}); |
| 27 | }, `TypeError: Attempted to assign to readonly property.`); |
| 28 | shouldBe(Reflect.getPrototypeOf(object), proto); |
| 29 | |
| 30 | shouldBe(Reflect.setPrototypeOf(object, proto), true); |
| 31 | shouldBe(Reflect.setPrototypeOf(object, {}), false); |
| 32 | shouldBe(Reflect.getPrototypeOf(object), proto); |
| 33 | |
| 34 | object.__proto__ = proto; |
| 35 | shouldThrow(() => { |
| 36 | object.__proto__ = {}; |
| 37 | }, `TypeError: Attempted to assign to readonly property.`); |
| 38 | shouldBe(object.__proto__, proto); |