@Test public void testInvokeWithParameterTypeConversionOfIntegerToString() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("endsWith", 123) .toJavaString(); assertEquals("Failed to generate invocation with parameter type conversion", "str.endsWith(\"123\")", s); }
@Test public void testInvokeWithParameterTypeConversionOfStringToInteger() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("substring", "1", "3") .toJavaString(); assertEquals("Failed to generate invocation with parameter type conversion", "str.substring(1, 3)", s); }
@Test public void testAssignArrayValueWithPreIncrementAssignment() { final String s = StatementBuilder.create() .declareVariable("twoDimArray", String[][].class) .loadVariable("twoDimArray", 1, 2) .assignValue(AssignmentOperator.PreIncrementAssign, "test") .toJavaString(); assertEquals("Failed to generate array assignment", "twoDimArray[1][2] += \"test\";", s); }
@Test public void testInvokeWithLiteralParameters() { String result = StatementBuilder.create().declareVariable("s", String.class) .loadVariable("s").invoke("replaceAll", "foo", "foo\t\n").toJavaString(); assertEquals("Failed to generate invocation using literal parameters", "s.replaceAll(\"foo\", \"foo\\t\\n\")", result); }
@Test public void testInvokeBestMatchingMethod() { String s = StatementBuilder.create() .declareVariable("n", Integer.class) .loadVariable("n") // 1 will be inferred to LiteralValue<Integer>, equals(Integer.class) // should be matched equals(Object.class) .invoke("equals", 1) .toJavaString(); assertEquals("Failed to generate invocation on matched method", "n.equals(1)", s); }
@Test public void testSingleInvocation() { String s = StatementBuilder.create() .declareVariable("obj", Object.class) .loadVariable("obj") .invoke("toString") .toJavaString(); assertEquals("Failed to generate invocation on variable", "obj.toString()", s); }
@Test public void testWhileLoopChainedWithEmptyExpressionWithoutBody() { String s = StatementBuilder.create() .declareVariable("b", Boolean.class) .loadVariable("b") .while_().finish().toJavaString(); assertEquals("Failed to generate empty while loop with chained lhs", WHILE_EMPTY, s); }
@Test public void testWhileLoopChainedWithNullCheck() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .while_(BooleanOperator.NotEquals, null) .finish().toJavaString(); assertEquals("Failed to generate while loop with chained lhs, rhs (null check) and no body", WHILE_RHS_NULL_EMPTY, s); }
@Test public void testForeachLoopWithNullCheckAndProviderVarType() { String foreachWithListOfStrings = StatementBuilder.create() .declareVariable("list", new TypeLiteral<List<String>>() {}) .loadVariable("list") .foreachIfNotNull("element", Object.class) .finish().toJavaString(); assertEquals("Failed to generate foreach loop using a List<String> and null check", FOREACH_OBJECT_IN_LIST_NOT_NULL, foreachWithListOfStrings); }
@Test public void testForeachLoopWithNullCheck() { String foreachWithListOfStrings = StatementBuilder.create() .declareVariable("list", new TypeLiteral<List<String>>() {}) .loadVariable("list") .foreachIfNotNull("element") .finish().toJavaString(); assertEquals("Failed to generate foreach loop using a List<String> and null check", FOREACH_STRING_IN_LIST_NOT_NULL, foreachWithListOfStrings); }
@Test public void testEmptyIfBlockUsingLiteralRhs() { String s = StatementBuilder.create() .declareVariable("n", int.class) .loadVariable("n") .if_(BooleanOperator.Equals, 1) .finish().toJavaString(); assertEquals("Failed to generate empty if block using a literal rhs", EMPTY_IF_BLOCK_LITERAL_RHS, s); }
@Test public void testWhileLoopChainedWithExpression() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("length") .while_(BooleanOperator.GreaterThanOrEqual, 2) .finish().toJavaString(); assertEquals("Failed to generate while loop with chained lhs, rhs and no body", WHILE_RHS_EMPTY, s); }
@Test public void testInvalidCast() { try { final Statement stmt = Cast.to(Integer.class, Stmt.declareVariable("str", String.class).loadVariable("str")); stmt.generate(Context.create()); fail("expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected assertEquals("Wrong exception message", "java.lang.String cannot be cast to java.lang.Integer", e.getMessage()); } }
@Test public void testEmptyIfBlockUsingNoRhs() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("endsWith", "abc") .if_() .finish().toJavaString(); assertEquals("Failed to generate empty if block using no rhs", EMPTY_IF_BLOCK_NO_RHS, s); }
@Test public void testEmptyIfBlockUsingNoRhsAndNegation() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("endsWith", "abc") .ifNot() .finish().toJavaString(); assertEquals("Failed to generate empty if block using no rhs", EMPTY_IF_BLOCK_NO_RHS_AND_NEGATION, s); }
@Test public void testIfBlockWithInstanceOfExpression() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .if_(BooleanOperator.InstanceOf, MetaClassFactory.getAsStatement(String.class)).finish() .toJavaString(); assertEquals("Failed to generate empty if block using an instance of expression", EMPTY_IF_BLOCK_INSTANCE_OF_RHS, s); }
@Test public void testForLoopChainedWithoutCountingExpression() { String s = StatementBuilder.create() .declareVariable("i", Integer.class, 0) .loadVariable("i") .for_(Stmt.loadVariable("i").assignValue(0), Bool.expr(BooleanOperator.LessThan, 100)) .finish().toJavaString(); assertEquals("Failed to generate for loop with initializer and chained lhs", FOR_CHAINED_INITIALIZER_NO_COUNTING_EXP_EMPTY, s); }
@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 testWhileLoopChainedWithEmptyExpressionWithBody() { String s = StatementBuilder.create() .declareVariable("b", Boolean.class) .loadVariable("b") .while_() .append(StatementBuilder.create().loadVariable("b").assignValue(false)) .finish().toJavaString(); assertEquals("Failed to generate while loop with chained lhs and body", WHILE_WITH_BODY, s); }
@Test public void testForeachLoopWithInvoke() { Builder loop = StatementBuilder.create() .declareVariable("map", Map.class) .loadVariable("map") .invoke("keySet") .foreach("key") .append(Stmt.loadStatic(System.class, "out").invoke("println", Variable.get("key"))) .finish(); assertEquals("Failed to generate foreach loop using invoke()", FOREACH_KEYSET_LOOP, loop.toJavaString()); }