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