@Override public Statement getValue() { return ref.getValue(); }
@Override public Statement getValue() { return ref.getValue(); }
@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 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 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 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 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 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); } }
@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); } }