Exemplo n.º 1
0
  @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);
  }
Exemplo n.º 2
0
  @Test
  public void testInvokeWithParameterizedClassAndVariableReturnType() {
    String s =
        StatementBuilder.create(Context.create().autoImport())
            .declareVariable(
                "set",
                Set.class,
                StatementBuilder.create().invokeStatic(Foo.class, "baz", Set.class))
            .toJavaString();

    assertEquals(
        "Failed to generate method invocation with variable return type inferred from Class<T>",
        "Set set = Foo.baz(Set.class);",
        s);
  }
Exemplo n.º 3
0
  @Test
  public void testInvokeWithParameterizedMapAndVariableReturnType() {
    String s =
        StatementBuilder.create(Context.create().autoImport())
            .declareVariable("map", new TypeLiteral<Map<String, Integer>>() {})
            .declareVariable(
                "val",
                Integer.class,
                StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("map")))
            .toJavaString();

    assertEquals(
        "Failed to generate method invocation with variable return type inferred from Map<K, V>",
        "Integer val = Foo.bar(map);",
        s);
  }
Exemplo n.º 4
0
  @Test
  public void testInvokeWithParameterizedListAndVariableReturnType() {
    String s =
        StatementBuilder.create(Context.create().autoImport())
            .declareVariable("list", new TypeLiteral<List<String>>() {})
            .declareVariable(
                "str",
                String.class,
                StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("list")))
            .toJavaString();

    assertEquals(
        "Failed to generate method invocation with variable return type inferred from List<T>",
        "String str = Foo.bar(list);",
        s);
  }
Exemplo n.º 5
0
  @Test
  public void testInvokeWithInvalidVariableReturnType() {

    try {
      StatementBuilder.create(Context.create().autoImport())
          .declareVariable("list", new TypeLiteral<List<String>>() {})
          .declareVariable(
              "n",
              Integer.class,
              StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("list")))
          .toJavaString();
      fail("expected InvalidTypeException");
    } catch (InvalidTypeException e) {
      // expected
    }
  }
Exemplo n.º 6
0
  @Test
  public void testInvokeWithVariableReturnType() {
    String s =
        StatementBuilder.create(Context.create().autoImport())
            .declareVariable("s", String.class)
            .declareVariable(
                "str",
                String.class,
                StatementBuilder.create().invokeStatic(Foo.class, "foo", Variable.get("s")))
            .toJavaString();

    assertEquals(
        "Failed to generate method invocation using variable return type",
        "String str = Foo.foo(s);",
        s);
  }