@Test public void testInvokeWithParameterTypeConversionOfVariable() { Context c = Context.create().addVariable("n", Integer.class, 123); String s = StatementBuilder.create(c) .declareVariable("str", String.class) .loadVariable("str") .invoke("endsWith", c.getVariable("n").getValue()) .toJavaString(); assertEquals( "Failed to generate invocation with parameter type conversion of variable", "str.endsWith(\"123\")", s); }
@Test public void testInvokeWithParameterizedClassAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable( "set", Set.class, StatementBuilder.create().invokeStatic(Foo.class, "baz", Set.class)) .toJavaString(); assertEquals( "Failed to generate method invocation with variable return type inferred from Class<T>", "Set set = Foo.baz(Set.class);", s); }
@Test public void testInvokeWithParameterizedMapAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("map", new TypeLiteral<Map<String, Integer>>() {}) .declareVariable( "val", Integer.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("map"))) .toJavaString(); assertEquals( "Failed to generate method invocation with variable return type inferred from Map<K, V>", "Integer val = Foo.bar(map);", s); }
@Test public void testInvokeWithParameterizedListAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("list", new TypeLiteral<List<String>>() {}) .declareVariable( "str", String.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("list"))) .toJavaString(); assertEquals( "Failed to generate method invocation with variable return type inferred from List<T>", "String str = Foo.bar(list);", s); }
@Test public void testInvokeWithInvalidVariableReturnType() { try { StatementBuilder.create(Context.create().autoImport()) .declareVariable("list", new TypeLiteral<List<String>>() {}) .declareVariable( "n", Integer.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("list"))) .toJavaString(); fail("expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } }
@Test public void testInvokeWithVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("s", String.class) .declareVariable( "str", String.class, StatementBuilder.create().invokeStatic(Foo.class, "foo", Variable.get("s"))) .toJavaString(); assertEquals( "Failed to generate method invocation using variable return type", "String str = Foo.foo(s);", s); }