allgettersetters.js revision 268:6344644b81ec
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * Exercise all setters on standard objects.
26 *
27 * @test
28 * @run
29 */
30
31function checkGetterSetter(obj, expectError) {
32    while (obj != undefined && obj != null) {
33        var properties = Object.getOwnPropertyNames(obj);
34        for (var i in properties) {
35            var prop = properties[i];
36            try {
37                if (!/\d.*/.test(prop)) {
38                    eval("obj." + prop + " = " + "obj." + prop + ";");
39                }
40                obj[prop] = obj[prop];
41            } catch (e) {
42                if (!expectError || !(e instanceof TypeError)) {
43                    fail(e + ": " + obj.toString() +"." + prop, e);
44                }
45            }
46        }
47        obj = Object.getPrototypeOf(obj);
48    }
49}
50
51// objects
52checkGetterSetter([2, 23]);
53checkGetterSetter(new Boolean(true));
54checkGetterSetter(new Date(0));
55checkGetterSetter(new Error());
56checkGetterSetter(new EvalError());
57if (typeof JSAdapter != 'undefined') {
58    checkGetterSetter(new JSAdapter({}));
59}
60if (typeof JavaImporter != 'undefined') {
61    checkGetterSetter(new JavaImporter(java.io));
62}
63checkGetterSetter(function() {});
64checkGetterSetter(new Number(42));
65checkGetterSetter(new Object());
66checkGetterSetter(new RangeError());
67checkGetterSetter(new ReferenceError());
68checkGetterSetter(/nashorn/);
69checkGetterSetter(new String('hello'));
70checkGetterSetter(new SyntaxError());
71checkGetterSetter(new TypeError());
72checkGetterSetter(new URIError());
73
74// constructors and prototypes
75checkGetterSetter(Array);
76checkGetterSetter(Array.prototype);
77checkGetterSetter(Boolean);
78checkGetterSetter(Boolean.prototype);
79checkGetterSetter(Error);
80checkGetterSetter(Error.prototype);
81checkGetterSetter(EvalError);
82checkGetterSetter(EvalError.prototype);
83checkGetterSetter(Function);
84checkGetterSetter(Function.prototype);
85if (typeof JSAdapter != 'undefined') {
86    checkGetterSetter(JSAdapter);
87    checkGetterSetter(JSAdapter.prototype);
88}
89if (typeof JavaImporter != 'undefined') {
90    checkGetterSetter(JavaImporter);
91    checkGetterSetter(JavaImporter.prototype);
92}
93checkGetterSetter(Number);
94checkGetterSetter(Number.prototype);
95checkGetterSetter(Object);
96checkGetterSetter(Object.prototype);
97checkGetterSetter(RangeError);
98checkGetterSetter(RangeError.prototype);
99checkGetterSetter(ReferenceError);
100checkGetterSetter(ReferenceError.prototype);
101checkGetterSetter(RegExp);
102checkGetterSetter(RegExp.prototype);
103checkGetterSetter(String);
104checkGetterSetter(String.prototype);
105checkGetterSetter(SyntaxError);
106checkGetterSetter(SyntaxError.prototype);
107checkGetterSetter(TypeError);
108checkGetterSetter(TypeError.prototype);
109checkGetterSetter(URIError);
110checkGetterSetter(URIError.prototype);
111
112// misc. objects
113checkGetterSetter(this);
114
115if (typeof Packages != 'undefined') {
116    checkGetterSetter(Packages);
117    checkGetterSetter(java);
118    checkGetterSetter(javax);
119}
120
121if (typeof Java != 'undefined') {
122    checkGetterSetter(Java);
123    checkGetterSetter(Java.prototype);
124}
125
126if (typeof Debug != 'undefined') {
127    checkGetterSetter(Debug);
128}
129
130checkGetterSetter((function() { return arguments; })());
131// TypeError expected on certain property getter/setter for strict arguments
132checkGetterSetter((function() { 'use strict'; return arguments; })(), true);
133checkGetterSetter(JSON);
134checkGetterSetter(JSON.prototype);
135checkGetterSetter(Math);
136checkGetterSetter(Math.prototype);
137