changeset 352:9b40969f01e3 draft

Added new tests into CompiledScriptTest.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Thu, 10 Apr 2014 11:05:49 +0200
parents 48afda73ee6e
children 6cb1ff232c39
files ChangeLog src/org/RhinoTests/CompiledScriptTest.java src/org/RhinoTests/JavaScriptSnippets.java
diffstat 3 files changed, 122 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Apr 09 13:30:07 2014 +0200
+++ b/ChangeLog	Thu Apr 10 11:05:49 2014 +0200
@@ -1,3 +1,9 @@
+2014-04-10  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/org/RhinoTests/CompiledScriptTest.java:
+	* src/org/RhinoTests/JavaScriptSnippets.java:
+	Added new tests into CompiledScriptTest.
+
 2014-04-09  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* src/org/RhinoTests/JavaScriptSnippets.java:
--- a/src/org/RhinoTests/CompiledScriptTest.java	Wed Apr 09 13:30:07 2014 +0200
+++ b/src/org/RhinoTests/CompiledScriptTest.java	Thu Apr 10 11:05:49 2014 +0200
@@ -317,6 +317,102 @@
      * @throws ScriptException
      *             this exception is thrown when this test case failed.
      */
+    protected void testEvalNumericExpression7() throws ScriptException {
+    	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_7;
+    	CompiledScript script = getCompiledScript(expression);
+    	Object result = script.eval();
+    	assertNotNull(result, "result should not be null");
+    	assertTrue(result instanceof Number, "result is not an instance of Number");
+		if (getJavaVersion() >= 7) {
+			assertTrue(result instanceof Double, "result is not an instance of Double");
+			double doubleResult = ((Double) result).doubleValue();
+			assertEquals(doubleResult, 42, "wrong result " + doubleResult + " for the expression: '" + expression + "'");
+		}
+		else {
+			assertTrue(result instanceof Integer, "result is not an instance of Integer");
+			int integerResult = ((Integer) result).intValue();
+			assertEquals(integerResult, 42, "wrong result " + integerResult + " for the expression: '" + expression + "'");
+		}
+    }
+
+    /**
+     * 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 testEvalNumericExpression8() throws ScriptException {
+    	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_8;
+    	CompiledScript script = getCompiledScript(expression);
+    	Object result = script.eval();
+    	assertNotNull(result, "result should not be null");
+    	assertTrue(result instanceof Number, "result is not an instance of Number");
+		if (getJavaVersion() >= 7) {
+			assertTrue(result instanceof Double, "result is not an instance of Double");
+			double doubleResult = ((Double) result).doubleValue();
+			assertEquals(doubleResult, 42, "wrong result " + doubleResult + " for the expression: '" + expression + "'");
+		}
+		else {
+			assertTrue(result instanceof Integer, "result is not an instance of Integer");
+			int integerResult = ((Integer) result).intValue();
+			assertEquals(integerResult, 42, "wrong result " + integerResult + " for the expression: '" + expression + "'");
+		}
+    }
+
+    /**
+     * 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 testEvalNumericExpression9() throws ScriptException {
+    	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_9;
+    	CompiledScript script = getCompiledScript(expression);
+    	Object result = script.eval();
+    	assertNotNull(result, "result should not be null");
+    	assertTrue(result instanceof Number, "result is not an instance of Number");
+		if (getJavaVersion() >= 7) {
+			assertTrue(result instanceof Double, "result is not an instance of Double");
+			double doubleResult = ((Double) result).doubleValue();
+			assertEquals(doubleResult, 3, "wrong result " + doubleResult + " for the expression: '" + expression + "'");
+		}
+		else {
+			assertTrue(result instanceof Integer, "result is not an instance of Integer");
+			int integerResult = ((Integer) result).intValue();
+			assertEquals(integerResult, 3, "wrong result " + integerResult + " for the expression: '" + expression + "'");
+		}
+    }
+
+    /**
+     * 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 testEvalNumericExpression10() throws ScriptException {
+    	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_10;
+    	CompiledScript script = getCompiledScript(expression);
+    	Object result = script.eval();
+    	assertNotNull(result, "result should not be null");
+    	assertTrue(result instanceof Number, "result is not an instance of Number");
+		if (getJavaVersion() >= 7) {
+			assertTrue(result instanceof Double, "result is not an instance of Double");
+			double doubleResult = ((Double) result).doubleValue();
+			assertEquals(doubleResult, 6, "wrong result " + doubleResult + " for the expression: '" + expression + "'");
+		}
+		else {
+			assertTrue(result instanceof Integer, "result is not an instance of Integer");
+			int integerResult = ((Integer) result).intValue();
+			assertEquals(integerResult, 6, "wrong result " + integerResult + " for the expression: '" + expression + "'");
+		}
+    }
+
+    /**
+     * 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 testEvalDoubleExpression1() throws ScriptException {
     	final String expression = JavaScriptSnippets.DOUBLE_NUMERIC_EXPRESSION_1;
     	CompiledScript script = getCompiledScript(expression);
--- a/src/org/RhinoTests/JavaScriptSnippets.java	Wed Apr 09 13:30:07 2014 +0200
+++ b/src/org/RhinoTests/JavaScriptSnippets.java	Thu Apr 10 11:05:49 2014 +0200
@@ -153,6 +153,26 @@
     protected static final String NUMERIC_EXPRESSION_6 = "(1+2)*3";
 
     /**
+     * Numeric expression.
+     */
+    protected static final String NUMERIC_EXPRESSION_7 = "(42)";
+
+    /**
+     * Numeric expression.
+     */
+    protected static final String NUMERIC_EXPRESSION_8 = "((42))";
+
+    /**
+     * Numeric expression.
+     */
+    protected static final String NUMERIC_EXPRESSION_9 = "((1+2))";
+
+    /**
+     * Numeric expression.
+     */
+    protected static final String NUMERIC_EXPRESSION_10 = "((1+2+3))";
+
+    /**
      * Numeric expression containing floating point value.
      */
     protected static final String DOUBLE_NUMERIC_EXPRESSION_1 = "1./2";