|
1 | 1 | package net.sourceforge.htmlunit; |
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
4 | | -import static org.junit.Assert.assertFalse; |
5 | 4 |
|
6 | 5 | import org.junit.Test; |
7 | 6 | import org.mozilla.javascript.Context; |
8 | 7 | import org.mozilla.javascript.ContextAction; |
| 8 | +import org.mozilla.javascript.ContextFactory; |
| 9 | +import org.mozilla.javascript.EcmaError; |
9 | 10 | import org.mozilla.javascript.Scriptable; |
10 | 11 |
|
11 | 12 | /** |
@@ -47,19 +48,41 @@ public Object run(final Context cx) { |
47 | 48 | */ |
48 | 49 | @Test |
49 | 50 | public void getPrototypeOfString() throws Exception { |
50 | | - final String script = "Object.getPrototypeOf('')"; |
| 51 | + getPrototypeOfString("", "''", true); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @throws Exception if the test fails |
| 56 | + */ |
| 57 | + @Test(expected = EcmaError.class) |
| 58 | + public void getPrototypeOfStringFalse() throws Exception { |
| 59 | + getPrototypeOfString("", "''", false); |
| 60 | + } |
| 61 | + |
| 62 | + public void getPrototypeOfString(final Object expected, final String value, final boolean contextFeature) throws Exception { |
| 63 | + final ContextFactory myContextFactory = new ContextFactory() { |
| 64 | + @Override |
| 65 | + protected boolean hasFeature(final Context cx, final int featureIndex) { |
| 66 | + if (Context.FEATURE_HTMLUNIT_GET_PROTOTYPE_OF_STRING == featureIndex) { |
| 67 | + return contextFeature; |
| 68 | + } |
| 69 | + return super.hasFeature(cx, featureIndex); |
| 70 | + }; |
| 71 | + }; |
| 72 | + |
| 73 | + final String script = "Object.getPrototypeOf(" + value + ")"; |
51 | 74 |
|
52 | 75 | final ContextAction action = new ContextAction() { |
53 | 76 | @Override |
54 | 77 | public Object run(final Context cx) { |
55 | 78 | final Scriptable scope = cx.initStandardObjects(); |
56 | 79 | final Object result = cx.evaluateString(scope, script, "test.js", 1, null); |
57 | | - assertEquals("", result); |
| 80 | + assertEquals(expected, result); |
58 | 81 | return null; |
59 | 82 | } |
60 | 83 | }; |
61 | 84 |
|
62 | | - Utils.runWithAllOptimizationLevels(action); |
| 85 | + Utils.runWithAllOptimizationLevels(myContextFactory, action); |
63 | 86 | } |
64 | 87 |
|
65 | 88 | /** |
@@ -87,18 +110,14 @@ public Object run(final Context cx) { |
87 | 110 | */ |
88 | 111 | @Test |
89 | 112 | public void getPrototypeOfBoolean() throws Exception { |
90 | | - final String script = "Object.getPrototypeOf(true)"; |
91 | | - |
92 | | - final ContextAction action = new ContextAction() { |
93 | | - @Override |
94 | | - public Object run(final Context cx) { |
95 | | - final Scriptable scope = cx.initStandardObjects(); |
96 | | - final Object result = cx.evaluateString(scope, script, "test.js", 1, null); |
97 | | - assertFalse((Boolean) result); |
98 | | - return null; |
99 | | - } |
100 | | - }; |
| 113 | + getPrototypeOfString(false, "true", true); |
| 114 | + } |
101 | 115 |
|
102 | | - Utils.runWithAllOptimizationLevels(action); |
| 116 | + /** |
| 117 | + * @throws Exception if the test fails |
| 118 | + */ |
| 119 | + @Test(expected = EcmaError.class) |
| 120 | + public void getPrototypeOfBooleanFalse() throws Exception { |
| 121 | + getPrototypeOfString("", "true", false); |
103 | 122 | } |
104 | 123 | } |
0 commit comments