@Override public String generate(Context context) { type = context.getVariable(name).getType(); return name; }
@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()); } }
@Override public String generate(Context context) { type = context.getVariable(name).getType(); return name; }
@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()); }
/** * Returns a reference to the class member {@link Variable} with the given name. * * @param name * the name of the class member variable. * * @return the {@link VariableReference} found, can not be null. * * @throws OutOfScopeException * if member variable with the given name can not be found. */ public VariableReference getClassMember(final String name) { checkThread(); return getVariable(name, true); }
@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()); }
/** * Returns a reference to the {@link Variable} with the given name. * * @param name * the name of the variable. * * @return the {@link VariableReference} found, can not be null. * * @throws OutOfScopeException * if variable with the given name can not be found. */ public VariableReference getVariable(final String name) { checkThread(); return getVariable(name, false); }
/** * Returns a reference to the {@link Variable} with the given name. * * @param name * the name of the variable. * * @return the {@link VariableReference} found, can not be null. * * @throws OutOfScopeException * if variable with the given name can not be found. */ public VariableReference getVariable(final String name) { checkThread(); return getVariable(name, false); }
@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 testSwitchBlockWithInvalidCaseValueUsingEnumForInt() { try { Context c = Context.create().addVariable("n", int.class); StatementBuilder.create(c) .switch_(c.getVariable("n")) .case_(TestEnum.A) .finish() .toJavaString(); fail("expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } }
@Test public void testAddVariableWithStringTypeInference() { Context ctx = ContextBuilder.create().addVariable("n", "10").getContext(); 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 testDeclareVariableWithObjectInitializationWithStringTypeInference() { final Context ctx = Context.create(); final String s = StatementBuilder.create(ctx) .declareVariable("str", ObjectBuilder.newInstanceOf(String.class)).toJavaString(); assertEquals("failed to generate variable declaration with object initialization and string type inference", "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 testAddVariableWithExactTypeProvided() { Context ctx = ContextBuilder.create().addVariable("n", Integer.class, 10).getContext(); 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 testAddVariableWithIntegerTypeInference() { Context ctx = ContextBuilder.create().addVariable("n", 10).getContext(); 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 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 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 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 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 testAddVariableWithImplicitTypeConversion() { Context ctx = ContextBuilder.create().addVariable("n", Integer.class, "10").getContext(); 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 { Stmt.create(ContextBuilder.create().addVariable("n", Integer.class, "abc").getContext()).toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException ive) { // expected assertTrue(ive.getCause() instanceof NumberFormatException); } }