@Test public void testDeclareVariableWithObjectInitializationWithExactTypeProvided() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("str", String.class, ObjectBuilder.newInstanceOf(String.class)).generate(ctx); assertEquals("failed to generate variable declaration with object initialization and type provided", "String str = new String();", s); final VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), str.getType()); }
@Test public void testDeclareVariableWithIntegerTypeInference() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", 10).generate(ctx); assertEquals("failed to generate variable declaration with Integers type inference", "Integer n = 10;", s); final VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(Integer.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral(10), n.getValue()); }
@Test public void testDeclareVariableWithStringTypeInference() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", "10").generate(ctx); assertEquals("failed to generate variable declaration with =String type inference", "String n = \"10\";", s); final VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral("10"), n.getValue()); }
@Test public void testDeclareVariableWithExactTypeProvided() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", Integer.class, 10).generate(ctx); assertEquals("failed to generate variable declaration with type provided", "Integer n = 10;", s); final VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(Integer.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral(10), n.getValue()); }
@Test public void testDeclareVariableWithStatementInitialization() { final Context ctx = Context.create(); final String s = Stmt.declareVariable("str", String.class, Stmt.nestedCall(Stmt.newObject(Integer.class).withParameters(2)).invoke("toString")) .generate(ctx); assertEquals("failed to generate variable declaration with statement initialization", "String str = new Integer(2).toString();", s); final VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), str.getType()); }
@Test public void testDeclareVariableWithImplicitTypeConversion() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", Integer.class, "10").generate(ctx); assertEquals("failed to generate variable declaration with implicit type conversion", "Integer n = 10;", s); final VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(Integer.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral(10), n.getValue()); try { StatementBuilder.create().declareVariable("n", Integer.class, "abc").toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException ive) { // expected assertTrue(ive.getCause() instanceof NumberFormatException); } }