| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import junit.framework.TestCase; |
| 8 |
|
| 9 |
import org.mozilla.javascript.Context; |
| 10 |
import org.mozilla.javascript.ContextAction; |
| 11 |
import org.mozilla.javascript.ContextFactory; |
| 12 |
import org.mozilla.javascript.ScriptableObject; |
| 13 |
import org.mozilla.javascript.EvaluatorException; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public class StrictModeApiTest extends TestCase { |
| 21 |
|
| 22 |
private ScriptableObject global; |
| 23 |
private ContextFactory contextFactory; |
| 24 |
|
| 25 |
static class MyContextFactory extends ContextFactory { |
| 26 |
@Override |
| 27 |
protected boolean hasFeature(Context cx, int featureIndex) { |
| 28 |
switch (featureIndex) { |
| 29 |
case Context.FEATURE_STRICT_MODE: |
| 30 |
case Context.FEATURE_STRICT_VARS: |
| 31 |
case Context.FEATURE_STRICT_EVAL: |
| 32 |
case Context.FEATURE_WARNING_AS_ERROR: |
| 33 |
return true; |
| 34 |
} |
| 35 |
return super.hasFeature(cx, featureIndex); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public void testStrictModeError() { |
| 40 |
contextFactory = new MyContextFactory(); |
| 41 |
Context cx = contextFactory.enterContext(); |
| 42 |
try { |
| 43 |
global = cx.initStandardObjects(); |
| 44 |
try { |
| 45 |
runScript("({}.nonexistent);"); |
| 46 |
fail(); |
| 47 |
} catch (EvaluatorException e) { |
| 48 |
assertTrue(e.getMessage().startsWith("Reference to undefined property")); |
| 49 |
} |
| 50 |
} finally { |
| 51 |
Context.exit(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
private Object runScript(final String scriptSourceText) { |
| 56 |
return this.contextFactory.call(new ContextAction() { |
| 57 |
public Object run(Context context) { |
| 58 |
return context.evaluateString(global, scriptSourceText, |
| 59 |
"test source", 1, null); |
| 60 |
} |
| 61 |
}); |
| 62 |
} |
| 63 |
} |