Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/DefineClassMapInheritance.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 static org.junit.Assert.*;
8
9 import java.lang.reflect.InvocationTargetException;
10
11 import org.junit.Test;
12 import org.mozilla.javascript.Context;
13 import org.mozilla.javascript.ScriptableObject;
14
15 @SuppressWarnings("serial")
16 public class DefineClassMapInheritance {
17
18 public static class Food extends ScriptableObject {
19 @Override
20 public String getClassName() {
21 return getClass().getSimpleName();
22 }
23 }
24
25 public static class Fruit extends Food {
26 }
27
28 public static class Vegetable extends Food {
29 }
30
31 @Test
32 public void test() throws IllegalAccessException, InstantiationException,
33 InvocationTargetException {
34 Context cx = Context.enter();
35 try {
36 ScriptableObject scope = cx.initStandardObjects();
37
38 // define two classes that share a parent prototype
39 ScriptableObject.defineClass(scope, Fruit.class, false, true);
40 ScriptableObject.defineClass(scope, Vegetable.class, false, true);
41
42 assertEquals(Boolean.TRUE,
43 evaluate(cx, scope, "(new Fruit instanceof Food)"));
44 assertEquals(Boolean.TRUE,
45 evaluate(cx, scope, "(new Vegetable instanceof Food)"));
46 } finally {
47 Context.exit();
48 }
49 }
50
51 private static Object evaluate(Context cx, ScriptableObject scope,
52 String source) {
53 return cx.evaluateString(scope, source, "<eval>", 1, null);
54 }
55 }