Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/DefineClassTest.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript.tests;
6
7 import org.junit.Test;
8 import org.junit.Before;
9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertNull;
11
12 import org.mozilla.javascript.*;
13 import org.mozilla.javascript.annotations.*;
14
15 public class DefineClassTest {
16
17 Scriptable scope;
18
19 @Test
20 public void testAnnotatedHostObject() {
21 Context cx = Context.enter();
22 try {
23 Object result = evaluate(cx, "a = new AnnotatedHostObject(); a.initialized;");
24 assertEquals(result, Boolean.TRUE);
25 assertEquals(evaluate(cx, "a.instanceFunction();"), "instanceFunction");
26 assertEquals(evaluate(cx, "a.namedFunction();"), "namedFunction");
27 assertEquals(evaluate(cx, "AnnotatedHostObject.staticFunction();"), "staticFunction");
28 assertEquals(evaluate(cx, "AnnotatedHostObject.namedStaticFunction();"), "namedStaticFunction");
29 assertNull(evaluate(cx, "a.foo;"));
30 assertEquals(evaluate(cx, "a.foo = 'foo'; a.foo;"), "FOO");
31 assertEquals(evaluate(cx, "a.bar;"), "bar");
32
33 // Setting a property with no setting should be silently
34 // ignored in non-strict mode.
35 evaluate(cx, "a.bar = 'new bar'");
36 assertEquals("bar", evaluate(cx, "a.bar;"));
37 } finally {
38 Context.exit();
39 }
40 }
41
42 @Test
43 public void testTraditionalHostObject() {
44 Context cx = Context.enter();
45 try {
46 Object result = evaluate(cx, "t = new TraditionalHostObject(); t.initialized;");
47 assertEquals(result, Boolean.TRUE);
48 assertEquals(evaluate(cx, "t.instanceFunction();"), "instanceFunction");
49 assertEquals(evaluate(cx, "TraditionalHostObject.staticFunction();"), "staticFunction");
50 assertNull(evaluate(cx, "t.foo;"));
51 assertEquals(evaluate(cx, "t.foo = 'foo'; t.foo;"), "FOO");
52 assertEquals(evaluate(cx, "t.bar;"), "bar");
53
54 // Setting a property with no setting should be silently
55 // ignored in non-strict mode.
56 evaluate(cx, "t.bar = 'new bar'");
57 assertEquals("bar", evaluate(cx, "t.bar;"));
58 } finally {
59 Context.exit();
60 }
61 }
62
63 private Object evaluate(Context cx, String str) {
64 return cx.evaluateString(scope, str, "<testsrc>", 0, null);
65 }
66
67
68 @Before
69 public void init() throws Exception {
70 Context cx = Context.enter();
71 try {
72 scope = cx.initStandardObjects();
73 ScriptableObject.defineClass(scope, AnnotatedHostObject.class);
74 ScriptableObject.defineClass(scope, TraditionalHostObject.class);
75 } finally {
76 Context.exit();
77 }
78 }
79
80 public static class AnnotatedHostObject extends ScriptableObject {
81
82 String foo, bar = "bar";
83
84 public AnnotatedHostObject() {}
85
86 @Override
87 public String getClassName() {
88 return "AnnotatedHostObject";
89 }
90
91 @JSConstructor
92 public void jsConstructorMethod() {
93 put("initialized", this, Boolean.TRUE);
94 }
95
96 @JSFunction
97 public Object instanceFunction() {
98 return "instanceFunction";
99 }
100
101 @JSFunction("namedFunction")
102 public Object someFunctionName() {
103 return "namedFunction";
104 }
105
106 @JSStaticFunction
107 public static Object staticFunction() {
108 return "staticFunction";
109 }
110
111 @JSStaticFunction("namedStaticFunction")
112 public static Object someStaticFunctionName() {
113 return "namedStaticFunction";
114 }
115
116 @JSGetter
117 public String getFoo() {
118 return foo;
119 }
120
121 @JSSetter
122 public void setFoo(String foo) {
123 this.foo = foo.toUpperCase();
124 }
125
126 @JSGetter("bar")
127 public String getMyBar() {
128 return bar;
129 }
130
131 public void setBar(String bar) {
132 this.bar = bar.toUpperCase();
133 }
134 }
135
136 public static class TraditionalHostObject extends ScriptableObject {
137
138 String foo, bar = "bar";
139
140 public TraditionalHostObject() {}
141
142 @Override
143 public String getClassName() {
144 return "TraditionalHostObject";
145 }
146
147 public void jsConstructor() {
148 put("initialized", this, Boolean.TRUE);
149 }
150
151 public Object jsFunction_instanceFunction() {
152 return "instanceFunction";
153 }
154
155 public static Object jsStaticFunction_staticFunction() {
156 return "staticFunction";
157 }
158
159 public String jsGet_foo() {
160 return foo;
161 }
162
163 public void jsSet_foo(String foo) {
164 this.foo = foo.toUpperCase();
165 }
166
167 public String jsGet_bar() {
168 return bar;
169 }
170
171 // not a JS setter
172 public void setBar(String bar) {
173 this.bar = bar.toUpperCase();
174 }
175
176 }
177
178 }