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