< prev index next >

test/src/jdk/nashorn/api/javaaccess/test/ArrayConversionTest.java

Print this page




  87 
  88     @Test
  89     public void testBooleanArrays() throws ScriptException {
  90         runTest("assertBooleanArrayConversions", "[false, true, '', 'false', 0, 1, 0.4, 0.6, {}, [], [false], [true], NaN, Infinity, null, undefined]");
  91     }
  92 
  93     @Test
  94     public void testArrayAmbiguity() throws ScriptException {
  95         runTest("x", "'abc'");
  96         runTest("x", "['foo', 'bar']");
  97     }
  98 
  99     @Test
 100     public void testListArrays() throws ScriptException {
 101         runTest("assertListArray", "[['foo', 'bar'], ['apple', 'orange']]");
 102     }
 103 
 104     @Test
 105     public void testVarArgs() throws ScriptException {
 106         // Sole NativeArray in vararg position becomes vararg array itself
 107         runTest("assertVarArg_42_17", "[42, 17]");
 108         // NativeArray in vararg position becomes an argument if there are more arguments
 109         runTest("assertVarArg_array_17", "[42], 18");
 110         // Only NativeArray is converted to vararg array, other objects (e.g. a function) aren't
 111         runTest("assertVarArg_function", "function() { return 'Hello' }");
 112     }
 113 
 114     private static void runTest(final String testMethodName, final String argument) throws ScriptException {
 115         e.eval("Java.type('" + ArrayConversionTest.class.getName() + "')." + testMethodName + "(" + argument + ")");
 116     }
 117 
 118     public static void assertNullIntArray(final int[] array) {
 119         assertNull(array);
 120     }
 121 
 122     public static void assertNullIntIntArray(final int[][] array) {
 123         assertNull(array);
 124     }
 125 
 126     public static void assertEmptyIntArray(final int[] array) {
 127         assertEquals(0, array.length);
 128     }
 129 
 130     public static void assertSingle42IntArray(final int[] array) {
 131         assertEquals(1, array.length);


 192         assertFalse(array[4]); // 0
 193         assertTrue(array[5]); // 1
 194         assertTrue(array[6]); // 0.4
 195         assertTrue(array[7]); // 0.6
 196         assertTrue(array[8]); // {}
 197         assertTrue(array[9]); // []
 198         assertTrue(array[10]); // [false]
 199         assertTrue(array[11]); // [true]
 200         assertFalse(array[12]); // NaN
 201         assertTrue(array[13]); // Infinity
 202         assertFalse(array[14]); // null
 203         assertFalse(array[15]); // undefined
 204     }
 205 
 206     public static void assertListArray(final List<?>[] array) {
 207         assertEquals(2, array.length);
 208         assertEquals(Arrays.asList("foo", "bar"), array[0]);
 209         assertEquals(Arrays.asList("apple", "orange"), array[1]);
 210     }
 211 
 212     public static void assertVarArg_42_17(final Object... args) {
 213         assertEquals(2, args.length);
 214         assertEquals(42, ((Number)args[0]).intValue());
 215         assertEquals(17, ((Number)args[1]).intValue());
 216     }
 217 
 218     public static void assertVarArg_array_17(final Object... args) throws ScriptException {
 219         assertEquals(2, args.length);
 220         e.getBindings(ScriptContext.ENGINE_SCOPE).put("arr", args[0]);
 221         assertTrue((Boolean)e.eval("arr instanceof Array && arr.length == 1 && arr[0] == 42"));
 222         assertEquals(18, ((Number)args[1]).intValue());
 223     }
 224 
 225     public static void assertVarArg_function(final Object... args) throws ScriptException {
 226         assertEquals(1, args.length);
 227         e.getBindings(ScriptContext.ENGINE_SCOPE).put("fn", args[0]);
 228         assertEquals("Hello", e.eval("fn()"));
 229     }
 230 
 231 
 232 
 233     public static void x(final String y) {
 234         assertEquals("abc", y);
 235     }
 236     public static void x(final String[] y) {
 237         assertTrue(Arrays.equals(new String[] { "foo", "bar"}, y));
 238     }
 239 }


  87 
  88     @Test
  89     public void testBooleanArrays() throws ScriptException {
  90         runTest("assertBooleanArrayConversions", "[false, true, '', 'false', 0, 1, 0.4, 0.6, {}, [], [false], [true], NaN, Infinity, null, undefined]");
  91     }
  92 
  93     @Test
  94     public void testArrayAmbiguity() throws ScriptException {
  95         runTest("x", "'abc'");
  96         runTest("x", "['foo', 'bar']");
  97     }
  98 
  99     @Test
 100     public void testListArrays() throws ScriptException {
 101         runTest("assertListArray", "[['foo', 'bar'], ['apple', 'orange']]");
 102     }
 103 
 104     @Test
 105     public void testVarArgs() throws ScriptException {
 106         // Sole NativeArray in vararg position becomes vararg array itself
 107         runTest("assertVarArgWith42And17", "[42, 17]");
 108         // NativeArray in vararg position becomes an argument if there are more arguments
 109         runTest("assertVarArgArray7", "[42], 18");
 110         // Only NativeArray is converted to vararg array, other objects (e.g. a function) aren't
 111         runTest("assertVarArgFunction", "function() { return 'Hello' }");
 112     }
 113 
 114     private static void runTest(final String testMethodName, final String argument) throws ScriptException {
 115         e.eval("Java.type('" + ArrayConversionTest.class.getName() + "')." + testMethodName + "(" + argument + ")");
 116     }
 117 
 118     public static void assertNullIntArray(final int[] array) {
 119         assertNull(array);
 120     }
 121 
 122     public static void assertNullIntIntArray(final int[][] array) {
 123         assertNull(array);
 124     }
 125 
 126     public static void assertEmptyIntArray(final int[] array) {
 127         assertEquals(0, array.length);
 128     }
 129 
 130     public static void assertSingle42IntArray(final int[] array) {
 131         assertEquals(1, array.length);


 192         assertFalse(array[4]); // 0
 193         assertTrue(array[5]); // 1
 194         assertTrue(array[6]); // 0.4
 195         assertTrue(array[7]); // 0.6
 196         assertTrue(array[8]); // {}
 197         assertTrue(array[9]); // []
 198         assertTrue(array[10]); // [false]
 199         assertTrue(array[11]); // [true]
 200         assertFalse(array[12]); // NaN
 201         assertTrue(array[13]); // Infinity
 202         assertFalse(array[14]); // null
 203         assertFalse(array[15]); // undefined
 204     }
 205 
 206     public static void assertListArray(final List<?>[] array) {
 207         assertEquals(2, array.length);
 208         assertEquals(Arrays.asList("foo", "bar"), array[0]);
 209         assertEquals(Arrays.asList("apple", "orange"), array[1]);
 210     }
 211 
 212     public static void assertVarArgWith42And17(final Object... args) {
 213         assertEquals(2, args.length);
 214         assertEquals(42, ((Number)args[0]).intValue());
 215         assertEquals(17, ((Number)args[1]).intValue());
 216     }
 217 
 218     public static void assertVarArgArray7(final Object... args) throws ScriptException {
 219         assertEquals(2, args.length);
 220         e.getBindings(ScriptContext.ENGINE_SCOPE).put("arr", args[0]);
 221         assertTrue((Boolean)e.eval("arr instanceof Array && arr.length == 1 && arr[0] == 42"));
 222         assertEquals(18, ((Number)args[1]).intValue());
 223     }
 224 
 225     public static void assertVarArgFunction(final Object... args) throws ScriptException {
 226         assertEquals(1, args.length);
 227         e.getBindings(ScriptContext.ENGINE_SCOPE).put("fn", args[0]);
 228         assertEquals("Hello", e.eval("fn()"));
 229     }
 230 
 231 
 232 
 233     public static void x(final String y) {
 234         assertEquals("abc", y);
 235     }
 236     public static void x(final String[] y) {
 237         assertTrue(Arrays.equals(new String[] { "foo", "bar"}, y));
 238     }
 239 }
< prev index next >