Skip to content

Commit ddf8c41

Browse files
committed
Tests for parameterization of getPrototypeOf
1 parent 579990a commit ddf8c41

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/test/java/net/sourceforge/htmlunit/NativeObjectTest.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package net.sourceforge.htmlunit;
22

33
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertFalse;
54

65
import org.junit.Test;
76
import org.mozilla.javascript.Context;
87
import org.mozilla.javascript.ContextAction;
8+
import org.mozilla.javascript.ContextFactory;
9+
import org.mozilla.javascript.EcmaError;
910
import org.mozilla.javascript.Scriptable;
1011

1112
/**
@@ -47,19 +48,41 @@ public Object run(final Context cx) {
4748
*/
4849
@Test
4950
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 + ")";
5174

5275
final ContextAction action = new ContextAction() {
5376
@Override
5477
public Object run(final Context cx) {
5578
final Scriptable scope = cx.initStandardObjects();
5679
final Object result = cx.evaluateString(scope, script, "test.js", 1, null);
57-
assertEquals("", result);
80+
assertEquals(expected, result);
5881
return null;
5982
}
6083
};
6184

62-
Utils.runWithAllOptimizationLevels(action);
85+
Utils.runWithAllOptimizationLevels(myContextFactory, action);
6386
}
6487

6588
/**
@@ -87,18 +110,14 @@ public Object run(final Context cx) {
87110
*/
88111
@Test
89112
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+
}
101115

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);
103122
}
104123
}

0 commit comments

Comments
 (0)