[ES6] Implement Reflect.ownKeys
[WebKit-https.git] / Source / JavaScriptCore / tests / stress / reflect-own-keys.js
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 function shouldBeArray(actual, expected) {
20     shouldBe(actual.length, expected.length);
21     for (var i = 0; i < expected.length; ++i) {
22         try {
23             shouldBe(actual[i], expected[i]);
24         } catch(e) {
25             print(JSON.stringify(actual));
26             throw e;
27         }
28     }
29 }
30
31 shouldBe(Reflect.ownKeys.length, 1);
32
33 shouldThrow(() => {
34     Reflect.ownKeys("hello");
35 }, `TypeError: Reflect.ownKeys requires the first argument be an object`);
36
37 var cocoa = Symbol("Cocoa");
38 var cappuccino = Symbol("Cappuccino");
39
40 shouldBeArray(Reflect.ownKeys({}), []);
41 shouldBeArray(Reflect.ownKeys({42:42}), ['42']);
42 shouldBeArray(Reflect.ownKeys({0:0,1:1,2:2}), ['0','1','2']);
43 shouldBeArray(Reflect.ownKeys({0:0,1:1,2:2,hello:42}), ['0','1','2','hello']);
44 shouldBeArray(Reflect.ownKeys({hello:42,0:0,1:1,2:2,world:42}), ['0','1','2','hello','world']);
45 shouldBeArray(Reflect.ownKeys({[cocoa]:42,hello:42,0:0,1:1,2:2,world:42}), ['0','1','2','hello','world', cocoa]);
46 shouldBeArray(Reflect.ownKeys({[cocoa]:42,hello:42,0:0,1:1,2:2,[cappuccino]:42,world:42}), ['0','1','2','hello','world', cocoa, cappuccino]);