changeset 214:6e3bb66d34ab draft

Added new tests for checking expression return types and values.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Mon, 19 Aug 2013 11:04:52 +0200
parents baa3f1d2d03c
children 5674e01de2f4
files ChangeLog src/org/RhinoTests/CompiledScriptTest.java src/org/RhinoTests/JavaScriptSnippets.java
diffstat 3 files changed, 123 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Aug 16 10:43:16 2013 +0200
+++ b/ChangeLog	Mon Aug 19 11:04:52 2013 +0200
@@ -1,3 +1,9 @@
+2013-08-19  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/org/RhinoTests/CompiledScriptTest.java:
+	* src/org/RhinoTests/JavaScriptSnippets.java:
+	Added new tests for checking expression return types and values.
+
 2013-08-16  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* src/org/RhinoTests/ScriptContextClassTest.java:
--- a/src/org/RhinoTests/CompiledScriptTest.java	Fri Aug 16 10:43:16 2013 +0200
+++ b/src/org/RhinoTests/CompiledScriptTest.java	Mon Aug 19 11:04:52 2013 +0200
@@ -149,6 +149,30 @@
      * @throws ScriptException
      *             this exception is thrown when this test case failed.
      */
+    protected void testEvalNumericExpression0() throws ScriptException {
+    	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_0;
+    	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 testEvalNumericExpression1() throws ScriptException {
     	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_1;
     	CompiledScript script = getCompiledScript(expression);
@@ -221,6 +245,78 @@
      * @throws ScriptException
      *             this exception is thrown when this test case failed.
      */
+    protected void testEvalNumericExpression4() throws ScriptException {
+    	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_4;
+    	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, 5, "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, 5, "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 testEvalNumericExpression5() throws ScriptException {
+    	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_5;
+    	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, 7, "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, 7, "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 testEvalNumericExpression6() throws ScriptException {
+    	final String expression = JavaScriptSnippets.NUMERIC_EXPRESSION_6;
+    	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, 9, "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, 9, "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	Fri Aug 16 10:43:16 2013 +0200
+++ b/src/org/RhinoTests/JavaScriptSnippets.java	Mon Aug 19 11:04:52 2013 +0200
@@ -1,7 +1,7 @@
 /*
   Rhino test framework
 
-   Copyright (C) 2012  Red Hat
+   Copyright (C) 2012, 2013  Red Hat
 
 This file is part of IcedTea.
 
@@ -120,6 +120,11 @@
     /**
      * Numeric expression.
      */
+    protected static final String NUMERIC_EXPRESSION_0 = "42";
+
+    /**
+     * Numeric expression.
+     */
     protected static final String NUMERIC_EXPRESSION_1 = "1+2";
 
     /**
@@ -133,6 +138,21 @@
     protected static final String NUMERIC_EXPRESSION_3 = "1+2*3";
 
     /**
+     * Numeric expression.
+     */
+    protected static final String NUMERIC_EXPRESSION_4 = "1*2+3";
+
+    /**
+     * Numeric expression.
+     */
+    protected static final String NUMERIC_EXPRESSION_5 = "1+(2*3)";
+
+    /**
+     * Numeric expression.
+     */
+    protected static final String NUMERIC_EXPRESSION_6 = "(1+2)*3";
+
+    /**
      * Numeric expression containing floating point value.
      */
     protected static final String DOUBLE_NUMERIC_EXPRESSION_1 = "1./2";