@Test public void getIdsShouldIncludeBothIndexAndNormalProperties() { array.put(0, array, "index"); array.put("a", array, "normal"); assertThat(array.getIds(), is(new Object[] {0, "a"})); }
public static Object map(Context cx, Scriptable thisObj, Object[] args, Function funObj) { NativeJavaObject njo = (NativeJavaObject) args[0]; NativeArray array = (NativeArray) cx.newArray(thisObj, 0); Function f = (Function) args[1]; Collection list = (Collection) njo.unwrap(); int i = 0; for (Object o : list) { Object[] argx = {o, Context.javaToJS(i, f), njo}; Object r = f.call(cx, thisObj, f, argx); array.put((int) array.getLength(), array, Context.javaToJS(r, f)); i++; } return array; }
public static Object filter(Context cx, Scriptable thisObj, Object[] args, Function funObj) { NativeArray array = (NativeArray) cx.newArray(thisObj, 0); NativeJavaObject njo = (NativeJavaObject) args[0]; Function f = (Function) args[1]; Collection list = (Collection) njo.unwrap(); int i = 0; for (Object o : list) { Object[] argx = {o, i, njo}; Object r = f.call(cx, thisObj, f, argx); if (r != null && r.getClass() != Undefined.class && !r.equals(false)) { array.put((int) array.getLength(), array, o); } } return array; }
protected Object wrap(List<?> result) { Context context = Context.getCurrentContext(); Scriptable scope = getParentScope(); NativeArray array = (NativeArray) context.newArray(scope, result.size()); int i = 0; for (Object part : result) { Object value = part; if (value instanceof ODocument) value = wrap((ODocument) part); else value = Context.javaToJS(value, scope); array.put(i, array, value); i++; } return array; }
@Test public void getIndexIdsShouldIncludeIdsLessThan2ToThe32() { int maxIndex = (int) (1L << 31) - 1; array.put(maxIndex, array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {maxIndex})); }
@Test public void getIndexIdsShouldNotIncludeNegativeIds() { array.put(-1, array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {})); }
@Test public void getIndexIdsShouldWorkWhenIndicesSetAsString() { array.put("0", array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {0})); }
@Test public void getIndexIdsShouldBeAZeroForSimpleSingletonArray() { array.put(0, array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {0})); }
@Test public void getShouldReturnNormalProperties() { array.put("p", array, "a"); assertThat((String) array.get("p", array), is("a")); }
@Test public void putShouldAddNormalProperties() { array.put("p", array, "a"); assertThat(array.has("p", array), is(true)); }
@Test public void putShouldAddIndexProperties() { array.put(0, array, "a"); assertThat(array.has(0, array), is(true)); }
@Test public void deleteShouldRemoveNormalProperties() { array.put("p", array, "a"); array.delete("p"); assertThat(array.has("p", array), is(false)); }
@Test public void deleteShouldRemoveIndexProperties() { array.put(0, array, "a"); array.delete(0); assertThat(array.has(0, array), is(false)); }
@Test public void getIndexIdsShouldNotReturnNonNumericIds() { array.put("x", array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {})); }
@Test public void getIndexIdsShouldNotIncludeIdsGreaterThanOrEqualTo2ToThe32() { array.put((1L << 31) + "", array, "a"); assertThat(array.getIndexIds(), is(new Integer[] {})); }