@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 testWhileLoopWithInvalidExpression() { try { StatementBuilder.create() .declareVariable("n", Integer.class) .loadVariable("n") .while_().finish().toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } try { StatementBuilder.create() .declareVariable("str", String.class) .declareVariable("str2", String.class) .loadVariable("str") .while_(BooleanOperator.GreaterThan, Variable.get("str2")).finish() .toJavaString(); fail("Expected InvalidExpressionException"); } catch (InvalidExpressionException iee) { // expected assertTrue("Wrong exception thrown", iee.getMessage().contains(String.class.getName())); } }
@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 testWhileLoopsNested() { Context c = Context.create().addVariable("str", String.class).addVariable("str2", String.class); String s = StatementBuilder.create(c) .loadVariable("str") .while_(BooleanOperator.NotEquals, null) .append( StatementBuilder.create(c) .while_(Bool.expr(Variable.get("str2"), BooleanOperator.NotEquals, null)) .finish()) .finish().toJavaString(); assertEquals("Failed to generate nested while loops", WHILE_NESTED_LOOPS, s); }