utatane.tea@gmail.com | ee8f0a9 | 2015-08-17 18:56:52 +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, 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 | shouldBe(Reflect.get.length, 2); |
| 20 | |
| 21 | shouldThrow(() => { |
| 22 | Reflect.get("hello"); |
| 23 | }, `TypeError: Reflect.get requires the first argument be an object`); |
| 24 | |
| 25 | var object = { hello: 42 }; |
| 26 | shouldBe(Reflect.get(object, 'hello'), 42); |
| 27 | shouldBe(Reflect.get(object, 'world'), undefined); |
| 28 | var proto = []; |
| 29 | object.__proto__ = proto; |
| 30 | shouldBe(Reflect.get(object, 'length'), 0); |
| 31 | |
| 32 | var array = [ 0, 1, 2 ]; |
| 33 | shouldBe(Reflect.get(array, 0), 0); |
| 34 | var proto = [ 0, 1, 2, 5, 6 ]; |
| 35 | array.__proto__ = proto; |
| 36 | shouldBe(Reflect.get(array, 3), 5); |
| 37 | array.__proto__ = Object.prototype; |
| 38 | shouldBe(Reflect.get(array, 3), undefined); |
| 39 | |
| 40 | var object = { |
| 41 | value: 42, |
| 42 | world: 200, |
| 43 | get hello() |
| 44 | { |
| 45 | return this.value; |
| 46 | } |
| 47 | }; |
| 48 | shouldBe(Reflect.get(object, 'hello'), 42); |
| 49 | shouldBe(Reflect.get(object, 'hello', { value: 200 }), 200); |
| 50 | shouldBe(Reflect.get(object, 'hello', "OK"), undefined); |
| 51 | shouldBe(Reflect.get(object, 'world'), 200); |
| 52 | shouldBe(Reflect.get(object, 'world', { value: 200 }), 200); |
| 53 | shouldBe(Reflect.get(object, 'world', "OK"), 200); |
| 54 | var value = 400; |
| 55 | shouldBe(Reflect.get(object, 'hello', null), 400); |
| 56 | shouldBe(Reflect.get(object, 'hello', undefined), 400); |
| 57 | |
| 58 | var object = { |
| 59 | value: 42, |
| 60 | world: 200, |
| 61 | get hello() |
| 62 | { |
| 63 | "use strict"; |
| 64 | return this.value; |
| 65 | } |
| 66 | }; |
| 67 | shouldBe(Reflect.get(object, 'hello'), 42); |
| 68 | shouldBe(Reflect.get(object, 'hello', { value: 200 }), 200); |
| 69 | shouldBe(Reflect.get(object, 'hello', "OK"), undefined); |
| 70 | shouldBe(Reflect.get(object, 'world'), 200); |
| 71 | shouldBe(Reflect.get(object, 'world', { value: 200 }), 200); |
| 72 | shouldBe(Reflect.get(object, 'world', "OK"), 200); |
| 73 | |
| 74 | shouldThrow(() => { |
| 75 | Reflect.get(object, 'hello', null); |
| 76 | }, `TypeError: null is not an object (evaluating 'this.value')`); |
| 77 | |
| 78 | shouldThrow(() => { |
| 79 | Reflect.get(object, 'hello', undefined); |
| 80 | }, `TypeError: undefined is not an object (evaluating 'this.value')`); |
| 81 | |
| 82 | var object = { |
| 83 | value: 42, |
| 84 | world: 200, |
| 85 | set hello(value) |
| 86 | { |
| 87 | } |
| 88 | }; |
| 89 | shouldBe(Reflect.get(object, 'hello'), undefined); |
| 90 | shouldBe(Reflect.get(object, 'hello', { hello: 42 }), undefined); |
| 91 | shouldBe(Reflect.get(object, 'ok', { ok: 42 }), undefined); |