/hg/rhino-tests: Added 14 new tests to the test case CompiledScr...
ptisnovs at icedtea.classpath.org
ptisnovs at icedtea.classpath.org
Thu Jul 26 05:36:39 PDT 2012
changeset 60e68ea5a35f in /hg/rhino-tests
details: http://icedtea.classpath.org/hg/rhino-tests?cmd=changeset;node=60e68ea5a35f
author: Pavel Tisnovsky <ptisnovs at redhat.com>
date: Thu Jul 26 14:39:16 2012 +0200
Added 14 new tests to the test case CompiledScriptTest (it was empty
before this push).
diffstat:
ChangeLog | 5 +
src/org/RhinoTests/CompiledScriptTest.java | 260 ++++++++++++++++++++++++++++-
2 files changed, 261 insertions(+), 4 deletions(-)
diffs (290 lines):
diff -r 6c5e550a5579 -r 60e68ea5a35f ChangeLog
--- 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 at 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 at redhat.com>
* Makefile:
diff -r 6c5e550a5579 -r 60e68ea5a35f src/org/RhinoTests/CompiledScriptTest.java
--- 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);
}
}
More information about the distro-pkg-dev
mailing list