CarpaccioHelper { public static Object[] getArgumentsWithView(View view, Class[] parametersType, Object[] args) { Object[] out = new Object[args.length + 1]; try { out[0] = parametersType[0].cast(view); } catch (ClassCastException e) { if (LOG_FAILURES) CarpaccioLogger.e(TAG, view.getClass().toString() + " cannot be cast to " + parametersType[0].getClass().toString(), e); out[0] = view; } for (int i = 0; i < args.length; ++i) { Class paramClass = parametersType[i + 1]; Object param = args[i]; if (param instanceof String && isNumber(paramClass)) { out[i + 1] = stringToNumber((String) param, paramClass); } else { try { out[i + 1] = paramClass.cast(param); } catch (ClassCastException e) { if (LOG_FAILURES) CarpaccioLogger.e(TAG, param.getClass().toString() + " cannot be cast to " + paramClass.toString(), e); out[i + 1] = param; } } } return out; }  static Object construct(String name); static Class[] getClassesWithHeaderClass(Object[] args, Class headerClass); static Class[] getClasses(Object[] args); static Object[] getArgumentsWithView(View view, Class[] parametersType, Object[] args); static String getFunctionName(String tag); static String[] getAttributes(String tag); static Object removeTag(View view, String actionName); static String[] trim(String[] strings); static ObjectAndMethod findObjectWithThisMethod(List<Object> objects, String function, int numberOfParams); static Method callFunction(Object object, String name, View view, Object[] args); static Method callMethod(Object object, Method method, String name, View view, Object[] args); static T callFunction(Object object, String name); static T callFunction(Object object, String name, Object[] args); static Integer stringToInt(String s); static Double stringToDouble(String s); static Long stringToLong(String s); static Float stringToFloat(String s); static boolean isNumber(Class destinationClass); static Object stringToNumber(String s, Class destinationClass); static T findParentOfClass(View view, Class<T> theClass); static Carpaccio findParentCarpaccio(View view); static Carpaccio registerToParentCarpaccio(View view); static String TAG; static boolean LOG_FAILURES; }
@Test public void testGetArguments() throws Exception { Class viewClass = TextView.class; TextView textView = mock(TextView.class); String[] args = new String[]{"value1", "value2"}; Object[] outArgs = CarpaccioHelper.getArgumentsWithView((View) textView, new Class[]{TextView.class, String.class, String.class}, args); Object[] expectedArgs = new Object[]{textView, "value1", "value2"}; assertArrayEquals(expectedArgs, outArgs); assertTrue(outArgs[0] instanceof TextView); }
@Test public void testGetArguments_numeric() throws Exception { Class viewClass = TextView.class; TextView textView = mock(TextView.class); String[] args = new String[]{"value1", "1", "1.2"}; Object[] outArgs = CarpaccioHelper.getArgumentsWithView((View) textView, new Class[]{TextView.class, String.class, Integer.class, Float.class}, args); Object[] expectedArgs = new Object[]{textView, "value1", 1, 1.2f}; assertArrayEquals(expectedArgs, outArgs); assertTrue(outArgs[0] instanceof TextView); }
@Test public void testGetArguments_fail() throws Exception { Class viewClass = ImageView.class; TextView textView = mock(TextView.class); String[] args = new String[]{"value1", "value2"}; Object[] outArgs = CarpaccioHelper.getArgumentsWithView((View) textView, new Class[]{TextView.class, String.class, String.class}, args); Object[] expectedArgs = new Object[]{textView, "value1", "value2"}; assertArrayEquals(expectedArgs, outArgs); assertTrue(outArgs[0] instanceof TextView); }