| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import org.junit.Assert; |
| 8 |
import org.junit.Test; |
| 9 |
import org.mozilla.javascript.Context; |
| 10 |
import org.mozilla.javascript.ContextAction; |
| 11 |
import org.mozilla.javascript.RhinoException; |
| 12 |
import org.mozilla.javascript.ScriptableObject; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class ErrorPropertiesTest { |
| 22 |
final static String LS = System.getProperty("line.separator"); |
| 23 |
|
| 24 |
private void testScriptStackTrace(final String script, final String expectedStackTrace) { |
| 25 |
testScriptStackTrace(script, expectedStackTrace, -1); |
| 26 |
testScriptStackTrace(script, expectedStackTrace, 0); |
| 27 |
testScriptStackTrace(script, expectedStackTrace, 1); |
| 28 |
} |
| 29 |
|
| 30 |
private void testScriptStackTrace(final String script, final String expectedStackTrace, |
| 31 |
final int optimizationLevel) { |
| 32 |
try { |
| 33 |
Utils.executeScript(script, optimizationLevel); |
| 34 |
} |
| 35 |
catch (final RhinoException e) { |
| 36 |
Assert.assertEquals(expectedStackTrace, e.getScriptStackTrace()); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void fileName() { |
| 42 |
testIt("try { null.method() } catch (e) { e.fileName }", "myScript.js"); |
| 43 |
testIt("try { null.property } catch (e) { e.fileName }", "myScript.js"); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void lineNumber() { |
| 48 |
testIt("try { null.method() } catch (e) { e.lineNumber }", 1); |
| 49 |
testIt("try {\n null.method() \n} catch (e) { e.lineNumber }", 2); |
| 50 |
testIt("\ntry \n{\n null.method() \n} catch (e) { e.lineNumber }", 4); |
| 51 |
|
| 52 |
testIt("function f() {\n null.method(); \n}\n try { f() } catch (e) { e.lineNumber }", 2); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void defaultStack() { |
| 57 |
RhinoException.useMozillaStackStyle(false); |
| 58 |
testScriptStackTrace("null.method()", "\tat myScript.js:1" + LS); |
| 59 |
final String script = "function f() \n{\n null.method();\n}\nf();\n"; |
| 60 |
testScriptStackTrace(script, "\tat myScript.js:3 (f)" + LS + "\tat myScript.js:5" + LS); |
| 61 |
testIt("try { null.method() } catch (e) { e.stack }", "\tat myScript.js:1" + LS); |
| 62 |
final String expectedStack = "\tat myScript.js:2 (f)" + LS + "\tat myScript.js:4" + LS; |
| 63 |
testIt("function f() {\n null.method(); \n}\n try { f() } catch (e) { e.stack }", expectedStack); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void mozillaStack() { |
| 68 |
RhinoException.useMozillaStackStyle(true); |
| 69 |
testScriptStackTrace("null.method()", "@myScript.js:1" + LS); |
| 70 |
final String script = "function f() \n{\n null.method();\n}\nf();\n"; |
| 71 |
testScriptStackTrace(script, "f()@myScript.js:3" + LS + "@myScript.js:5" + LS); |
| 72 |
testIt("try { null.method() } catch (e) { e.stack }", "@myScript.js:1" + LS); |
| 73 |
final String expectedStack = "f()@myScript.js:2" + LS + "@myScript.js:4" + LS; |
| 74 |
testIt("function f() {\n null.method(); \n}\n try { f() } catch (e) { e.stack }", expectedStack); |
| 75 |
} |
| 76 |
|
| 77 |
private void testIt(final String script, final Object expected) { |
| 78 |
final ContextAction action = new ContextAction() { |
| 79 |
public Object run(final Context cx) { |
| 80 |
try { |
| 81 |
final ScriptableObject scope = cx.initStandardObjects(); |
| 82 |
final Object o = cx.evaluateString(scope, script, |
| 83 |
"myScript.js", 1, null); |
| 84 |
Assert.assertEquals(expected, o); |
| 85 |
return o; |
| 86 |
} |
| 87 |
catch (final RuntimeException e) { |
| 88 |
throw e; |
| 89 |
} |
| 90 |
catch (final Exception e) { |
| 91 |
throw new RuntimeException(e); |
| 92 |
} |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
Utils.runWithAllOptimizationLevels(action); |
| 97 |
} |
| 98 |
} |