Mercurial > hg > rhino-tests
changeset 16:60e68ea5a35f draft
Added 14 new tests to the test case CompiledScriptTest (it was empty
before this push).
author | Pavel Tisnovsky <ptisnovs@redhat.com> |
---|---|
date | Thu, 26 Jul 2012 14:39:16 +0200 |
parents | 6c5e550a5579 |
children | 50619c42d67c |
files | ChangeLog src/org/RhinoTests/CompiledScriptTest.java |
diffstat | 2 files changed, 261 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Tue Jul 24 13:30:43 2012 +0200 +++ b/ChangeLog Thu Jul 26 14:39:16 2012 +0200 @@ -1,3 +1,8 @@ +2012-07-26 Pavel Tisnovsky <ptisnovs@redhat.com> + + * src/org/RhinoTests/CompiledScriptTest.java: + Added 14 new tests to this test case (it was empty before this push). + 2012-07-24 Pavel Tisnovsky <ptisnovs@redhat.com> * Makefile:
--- a/src/org/RhinoTests/CompiledScriptTest.java Tue Jul 24 13:30:43 2012 +0200 +++ b/src/org/RhinoTests/CompiledScriptTest.java Thu Jul 26 14:39:16 2012 +0200 @@ -40,18 +40,270 @@ package org.RhinoTests; +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + /** - * TODO: not implemented - * @author ptisnovs - * + * This test case check the behaviour of CompiledScript abstract class and it's + * descendents. + * + * @author Pavel Tisnovsky */ -public class CompiledScriptTest { +public class CompiledScriptTest extends BaseRhinoTest { + + /** + * Instance of ScriptEngineManager which is used by all tests in this test + * case. + */ + ScriptEngineManager engineManager; + + /** + * Instance of ScriptEngine which is used by all tests in this test case. + */ + ScriptEngine scriptEngine; + + @Override + protected void setUp(String[] args) { + this.engineManager = new ScriptEngineManager(); + this.scriptEngine = this.engineManager.getEngineByName("JavaScript"); + } + + @Override + protected void tearDown() { + // this block could be empty + return; + } + + /** + * Helper method which tries to retrieve an instance of class which + * implements CompiledScript interface for a given script. + * + * @param scriptText + * script source code + * @return instance of CompiledScript class + * @throws AssertionError + * when CompilingEngine cannot be retrieved + * @throws ScriptException + * thrown when script cannot be compiled + */ + private CompiledScript getCompiledScript(String scriptText) throws AssertionError, ScriptException { + // check if retyping could be done + assertTrue(this.scriptEngine instanceof Compilable, "ScriptEngine does not implement Compilable"); + // scriptEngine should be also retyped to Compilable, at least in case of JavaScript. + Compilable compilingEngine = (Compilable) this.scriptEngine; + // should not happen, but... + assertNotNull(compilingEngine, "cannot get compiling engine"); + // try to compile given script + return compileScript(scriptText, compilingEngine); + } + + /** + * Helper method which tries to compile given JavaScript. + * + * @param scriptText script source code + * @param compilingEngine instance of class which implements Compilable interface + * @return compiled script + * @throws ScriptException + * @throws AssertionError + */ + private CompiledScript compileScript(String scriptText, Compilable compilingEngine) throws ScriptException, AssertionError { + CompiledScript script = compilingEngine.compile(scriptText); + assertNotNull(script, "cannot compile script"); + return script; + } + + /** + * Test if it is possible to compile script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testCompileScriptStoredInString() throws ScriptException { + Compilable compilingEngine = (Compilable)this.scriptEngine; + assertNotNull(compilingEngine, "cannot get compiling engine"); + CompiledScript script = compilingEngine.compile(""); + assertNotNull(script, "cannot compile script"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testCompileAndRunSimpleScriptStoredInString() throws ScriptException { + CompiledScript script = getCompiledScript(""); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalIntegerExpression() throws ScriptException { + CompiledScript script = getCompiledScript("1+2*3"); + Object result = script.eval(); + assertNotNull(result, "result should not be null"); + assertTrue(result instanceof Number, "result is not an instance of Number"); + assertTrue(result instanceof Integer, "result is not an instance of Integer"); + int integerResult = ((Integer) result).intValue(); + assertEquals(integerResult, 7, "wrong result " + integerResult); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalDoubleExpression() throws ScriptException { + CompiledScript script = getCompiledScript("1./2"); + Object result = script.eval(); + assertTrue(result instanceof Number, "result is not an instance of Number"); + assertTrue(result instanceof Double, "result is not an instance of Integer"); + double doubleResult = ((Double) result).doubleValue(); + assertEquals(doubleResult, 0.5f, "wrong result " + doubleResult); + } + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalStringExpression1() throws ScriptException { + CompiledScript script = getCompiledScript("'Hello' + ' ' + 'world'"); + Object result = script.eval(); + assertNotNull(result, "result should not be null"); + assertTrue(result instanceof String, "result is not an instance of String"); + String stringResult = (String)result; + assertEquals(stringResult, "Hello world", "wrong result " + stringResult); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalStringExpression2() throws ScriptException { + CompiledScript script = getCompiledScript("'Hello world!'.substring(4, 7)"); + Object result = script.eval(); + assertNotNull(result, "result should not be null"); + assertTrue(result instanceof String, "result is not an instance of String"); + String stringResult = (String)result; + assertEquals(stringResult, "o w", "wrong result " + stringResult); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalPrintCommand1() throws ScriptException { + CompiledScript script = getCompiledScript("print('Hello world\\n')"); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalPrintCommand2() throws ScriptException { + CompiledScript script = getCompiledScript("print('\\tHello world\\n')"); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalPrintCommand3() throws ScriptException { + CompiledScript script = getCompiledScript("print('')"); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalPrintCommand4() throws ScriptException { + CompiledScript script = getCompiledScript("print()"); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalPrintlnCommand1() throws ScriptException { + CompiledScript script = getCompiledScript("println('Hello world')"); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalPrintlnCommand2() throws ScriptException { + CompiledScript script = getCompiledScript("println('\\tHello world')"); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalPrintlnCommand3() throws ScriptException { + CompiledScript script = getCompiledScript("println('')"); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testEvalPrintlnCommand4() throws ScriptException { + CompiledScript script = getCompiledScript("println()"); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + + /** * Entry point to this test case. * * @param args parameters passed from command line */ public static void main(String[] args) { + new CompiledScriptTest().doTests(args); } }