/** * Executes a JS Script on this runtime and returns the result as a double. * If the result is not a double, then a V8ResultUndefinedException is thrown. * * @param script The script to execute. * * @return The result of the script as a double, or V8ResultUndefinedException if * the result is not a double. */ public double executeDoubleScript(final String script) { return executeDoubleScript(script, null, 0); }
/** * Executes a JS Script on this runtime and returns the result as a double. * If the result is not a double, then a V8ResultUndefinedException is thrown. * * @param script The script to execute. * @param scriptName The name of the script * @param lineNumber The line number that is considered to be the first line of * the script. Typically 0, but could be set to another value for exception stack trace purposes. * * @return The result of the script as a double, or V8ResultUndefinedException if * the result is not a double. */ public double executeDoubleScript(final String script, final String scriptName, final int lineNumber) { checkThread(); checkScript(script); return executeDoubleScript(v8RuntimePtr, script, scriptName, lineNumber); }
/** * Executes a JS Script on this runtime and returns the result as a double. * If the result is not a double, then a V8ResultUndefinedException is thrown. * * @param script The script to execute. * * @return The result of the script as a double, or V8ResultUndefinedException if * the result is not a double. */ public double executeDoubleScript(final String script) { return executeDoubleScript(script, null, 0); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedExceptionDoubleScript() { v8.executeDoubleScript(""); }
@Test(expected = V8ScriptCompilationException.class) public void testV8ScriptCompilationExceptionForDoubleScript() { v8.executeDoubleScript(script); }
@Test public void testCreateV8ObjectFromFloatMap() { Map<String, Float> map = new HashMap<String, Float>(); map.put("a", 1.1f); map.put("b", 3.14159f); map.put("c", 4.999f); int size = registerAndRelease("result", map); assertEquals(3, size); assertEquals(1.1, v8.executeDoubleScript("result.a"), 0.000001); assertEquals(3.14159, v8.executeDoubleScript("result['b']"), 0.000001); assertEquals(4.999, v8.executeDoubleScript("result['c']"), 0.000001); }
@Test public void testCreateV8ArrayFromDoubleList() { List<Double> list = new ArrayList<Double>(); list.add(3.14159); list.add(4.1); list.add(5.3); int size = registerAndRelease("result", list); assertEquals(3, size); assertEquals(3.14159, v8.executeDoubleScript("result[0]"), 0.000001); assertEquals(4.1, v8.executeDoubleScript("result[1]"), 0.000001); assertEquals(5.3, v8.executeDoubleScript("result[2]"), 0.000001); }
@Test public void testCreateV8ObjectFromDoubleMap() { Map<String, Double> map = new HashMap<String, Double>(); map.put("a", 1.1); map.put("b", 3.14159); map.put("c", 4.999); int size = registerAndRelease("result", map); assertEquals(3, size); assertEquals(1.1, v8.executeDoubleScript("result.a"), 0.000001); assertEquals(3.14159, v8.executeDoubleScript("result['b']"), 0.000001); assertEquals(4.999, v8.executeDoubleScript("result['c']"), 0.000001); }
@Test public void testCreateV8ObjectFromMaxLongMap() { Map<String, Long> map = new HashMap<String, Long>(); map.put("a", Long.MAX_VALUE); map.put("b", Long.MIN_VALUE); int size = registerAndRelease("result", map); assertEquals(2, size); assertEquals(Long.MAX_VALUE, (long) v8.executeDoubleScript("result.a")); assertEquals(Long.MIN_VALUE, (long) v8.executeDoubleScript("result.b")); }
/*** Double Script ***/ @Test public void testSimpleDoubleScript() { double result = v8.executeDoubleScript("3.14159;"); assertEquals(3.14159, result, 0.00001); }
@Test public void testDoubleScriptWithInt() { double result = v8.executeDoubleScript("1"); assertEquals(1.0, result, 0.00001); }
@Test public void testDoubleScriptHandlesInts() { int result = (int) v8.executeDoubleScript("1"); assertEquals(1, result); }
@Test public void testDoubleScriptWithName() { double result = v8.executeDoubleScript("3.14159;", "name", 3); assertEquals(3.14159, result, 0.00001); }
@Test public void testDoubleMethodCalledFromScriptWithResult() { ICallback callback = mock(ICallback.class); doReturn(3.14159).when(callback).doubleMethodNoParameters(); v8.registerJavaMethod(callback, "doubleMethodNoParameters", "foo", new Class<?>[0]); double result = v8.executeDoubleScript("foo();"); assertEquals(3.14159, result, 0.0000001); }
@Test public void testCreateV8ArrayFromFloatList() { List<Float> list = new ArrayList<Float>(); list.add(1.1f); int size = registerAndRelease("result", list); assertEquals(1, size); assertEquals(1.1, v8.executeDoubleScript("result[0]"), 0.0000001); }
@Test public void testCreateV8ObjectFromLongTimestampMap() { Map<String, Long> map = new HashMap<String, Long>(); map.put("a", 1477486236000L); int size = registerAndRelease("result", map); assertEquals(1, size); assertEquals(1477486236000L, (long) v8.executeDoubleScript("result.a")); }
@Test public void testCreateV8ArrayFromMaxValueLong() { List<Long> list = new ArrayList<Long>(); list.add(Long.MAX_VALUE); int size = registerAndRelease("result", list); assertEquals(1, size); assertEquals(Long.MAX_VALUE, v8.executeDoubleScript("result[0]"), 0.0000001); }
/*** Add Double ***/ @Test public void testAddDouble() { v8.add("foo", 3.14159); double result = v8.executeDoubleScript("foo"); assertEquals(3.14159, result, 0.000001); }
@Test public void testAddDoubleReplaceValue() { v8.add("foo", 42.1); v8.add("foo", 43.1); double result = v8.executeDoubleScript("foo"); assertEquals(43.1, result, 0.000001); }
@Test public void testAddObjectWithDouble() { V8Object v8Object = new V8Object(v8); v8Object.add("double", 75.5); v8.add("foo", v8Object); double result = v8.executeDoubleScript("foo.double"); assertEquals(75.5, result, 0.000001); v8Object.close(); }