|
| 1 | +// Copyright 2015 Samsung Electronics Co., Ltd. |
| 2 | +// Copyright 2015 University of Szeged. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +// Test array |
| 17 | +var arr = ['a', 'b', 'c']; |
| 18 | +var props = Object.getOwnPropertyNames(arr); |
| 19 | +// props should contain: 0,1,2,length and the order is not defined! |
| 20 | +assert (props.indexOf("0") !== -1); |
| 21 | +assert (props.indexOf("1") !== -1); |
| 22 | +assert (props.indexOf("2") !== -1); |
| 23 | +assert (props.indexOf("length") !== -1); |
| 24 | +assert (props.length === 4); |
| 25 | + |
| 26 | +// Test object |
| 27 | +var obj = {key1: 'a', key3: 'b', key2: 'c', key4: 'c', key5: ''}; |
| 28 | +props = Object.getOwnPropertyNames(obj); |
| 29 | +// props should contain: key1,key2,key3,key4,key5 and the order is not defined! |
| 30 | +assert (props.indexOf("key1") !== -1); |
| 31 | +assert (props.indexOf("key2") !== -1); |
| 32 | +assert (props.indexOf("key3") !== -1); |
| 33 | +assert (props.indexOf("key4") !== -1); |
| 34 | +assert (props.indexOf("key5") !== -1); |
| 35 | +assert (props.length === 5); |
| 36 | + |
| 37 | +var obj2 = {}; |
| 38 | +Object.defineProperties(obj2, { |
| 39 | + key_one: {enumerable: true, value: 'one'}, |
| 40 | + key_two: {enumerable: false, value: 'two'}, |
| 41 | +}); |
| 42 | + |
| 43 | +props = Object.getOwnPropertyNames(obj2); |
| 44 | +// props should contain: key_one,key_two and the order is not defined! |
| 45 | +assert (props.indexOf("key_one") !== -1); |
| 46 | +assert (props.indexOf("key_two") !== -1); |
| 47 | +assert (props.length === 2); |
| 48 | + |
| 49 | +// Test prototype chain |
| 50 | +function Parent() {} |
| 51 | +Parent.prototype.inheritedMethod = function() {}; |
| 52 | + |
| 53 | +function Child() { |
| 54 | + this.prop = 5; |
| 55 | + this.method = function() {}; |
| 56 | +} |
| 57 | +Child.prototype = new Parent; |
| 58 | +Child.prototype.prototypeMethod = function() {}; |
| 59 | + |
| 60 | +props = Object.getOwnPropertyNames (new Child()); |
| 61 | +// props should contain: prop,method and the order is not defined! |
| 62 | +assert (props.indexOf("prop") !== -1); |
| 63 | +assert (props.indexOf("method") !== -1); |
| 64 | + |
| 65 | +assert (props.length === 2); |
| 66 | + |
| 67 | +// Test non-object argument |
| 68 | +try { |
| 69 | + Object.getOwnPrototypeNames("hello"); |
| 70 | + assert (false); |
| 71 | +} catch (e) { |
| 72 | + assert (e instanceof TypeError); |
| 73 | +} |
0 commit comments