@Override protected void setUp() throws Exception { Map<String, EvaluationFunction> funcs = new HashMap<String, EvaluationFunction>(); funcs.put( "if", new EvaluationFunction() { @Override public Object handle(List<? extends Object> parameters) { Boolean cond = (Boolean) parameters.get(0); String ifTrue = (String) parameters.get(1); String ifFalse = (String) parameters.get(2); if (cond) return ifTrue; else return ifFalse; } @Override @SuppressWarnings("unchecked") public Collection<? extends Class<?>> acceptedTypes(int num) { if (num == 0) return Arrays.asList(Boolean.class); if (num > 0) return Arrays.asList(String.class); return Arrays.asList(); } }); visitor.setFunctions(funcs); }
public void testFuncCall() { List<FuncParamAST> params = new ArrayList<FuncParamAST>(); params.add(new FuncParamAST(new ConstantAST("true"))); params.add(new FuncParamAST(new LiteralAST("it works!"))); params.add(new FuncParamAST(new LiteralAST("it doesn't"))); FuncCallAST funcCallAST = new FuncCallAST("if", params); assertEquals("it works!", visitor.visitFunction(funcCallAST)); }