1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// Test array
17var a = Symbol ('a');
18var b = Symbol ('b');
19var c = Symbol ('c');
20var d = Symbol ();
21
22var arr = [a, b, c, d];
23var props = Object.getOwnPropertySymbols (arr);
24// props should not contain: a, b, c, d
25assert (props.indexOf ('0') === -1);
26assert (props.indexOf ('1') === -1);
27assert (props.indexOf ('2') === -1);
28assert (props.indexOf ('length') === -1);
29assert (props.length === 0);
30
31// Test object
32var obj = {};
33obj[a] = 'a';
34obj[b] = 'b';
35obj[c] = 'c';
36obj[d] = 'd';
37props = Object.getOwnPropertySymbols (obj);
38// props should contain: a, b, c, d and the order is not defined!
39assert (props.indexOf(a) !== -1);
40assert (props.indexOf(b) !== -1);
41assert (props.indexOf(c) !== -1);
42assert (props.indexOf(d) !== -1);
43assert (props.length === 4);
44
45// Test same descriptions
46var fooSymbol1 = Symbol ('foo');
47var fooSymbol2 = Symbol ('foo');
48var fooSymbol3 = Symbol ('foo');
49var fooSymbol4 = Symbol ('foo');
50
51var obj = {}
52obj[fooSymbol1] = 'foo';
53obj[fooSymbol2] = 'bar';
54obj[fooSymbol3] = 'foobar';
55obj[fooSymbol4] = 'barfoo';
56
57props = Object.getOwnPropertySymbols (obj);
58assert (props.indexOf (fooSymbol1) !== -1);
59assert (props.indexOf (fooSymbol2) !== -1);
60assert (props.indexOf (fooSymbol3) !== -1);
61assert (props.indexOf (fooSymbol4) !== -1);
62assert (props.length === 4);
63
64var mixed_object = {};
65var foo = Symbol ('foo');
66var bar = Symbol.for ('bar');
67
68mixed_object[foo] = 'localSymbol';
69mixed_object[bar] = 'globalSymbol';
70mixed_object['foo'] = 'string';
71
72var props = Object.getOwnPropertySymbols (mixed_object);
73
74assert (typeof props[0] === 'symbol')
75assert (props.indexOf(foo) !== -1);
76assert (props.indexOf(bar) !== -1);
77assert (props.indexOf('foo') === -1);
78assert (props.length === 2)
79
80// Test prototype chain
81function Parent() {}
82Parent.prototype.inheritedMethod = function() {};
83
84function Child() {
85  this[a] = 5;
86  this[b] = function() {};
87}
88Child.prototype = new Parent;
89Child.prototype.prototypeMethod = function() {};
90
91props = Object.getOwnPropertySymbols (new Child());
92// props should contain: a, b and the order is not defined!
93assert (props.indexOf(a) !== -1);
94assert (props.indexOf(b) !== -1);
95
96assert (props.length === 2);
97
98// Test non-emumerable symbols
99var object = {};
100var foo = Symbol ('foo');
101var foo2 = Symbol ('foo2');
102object[foo] = 'EnumerableSymbolProp';
103Object.defineProperty(object, foo2, { value : 'NonEnumerableSymbolProp' });
104
105props = Object.getOwnPropertySymbols (object);
106
107assert (props.indexOf(foo) !== -1);
108assert (props.indexOf(foo2) !== -1);
109assert (props.length === 2);
110assert (Object.getOwnPropertyDescriptor (object, foo).enumerable === true);
111assert (Object.getOwnPropertyDescriptor (object, foo2).enumerable === false);
112