@Test public void testAddVariableWithObjectInitializationWithExactTypeProvided() { Context ctx = ContextBuilder.create().addVariable("str", String.class, ObjectBuilder.newInstanceOf(String.class)).getContext(); VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), str.getType()); // variable value cannot be verified before initialization statement was generated. Variable v = ctx.getVariables().get("str"); v.generate(ctx); assertNotNull("Value could not be generated", v.getValue()); }
@Test public void testAddVariableWithObjectInitializationWithStringTypeInference() { Context ctx = ContextBuilder.create().addVariable("str", ObjectBuilder.newInstanceOf(String.class)).getContext(); VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); // variable type and value cannot be verified before initialization statement was generated. Variable v = ctx.getVariables().get("str"); v.generate(ctx); assertNotNull("Value could not be generated", v.getValue()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), v.getType()); } }
@Test public void testDeclareFinalVariable() { final Context ctx = Context.create(); final String s = StatementBuilder.create(ctx) .declareVariable(String.class).asFinal().named("str").initializeWith("10").toJavaString(); assertEquals("failed to generate final variable declaration", "final String str = \"10\";", s); final VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); Assert.assertTrue("Variable should be final", ctx.getVariables().get("str").isFinal()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), str.getType()); }