Ejemplo n.º 1
0
  @Override
  public JsExpr computeForJsSrc(List<JsExpr> args) {

    JsExpr aList = args.get(0);

    return new JsExpr(aList.getText() + ".slice(-1)", Integer.MAX_VALUE);
  }
Ejemplo n.º 2
0
  @Override
  public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg0 = args.get(0);
    JsExpr arg1 = args.get(1);

    return new JsExpr(
        "Math.max(" + arg0.getText() + ", " + arg1.getText() + ")", Integer.MAX_VALUE);
  }
Ejemplo n.º 3
0
  @Override
  public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg = args.get(0);

    JsExpr random = new JsExpr("Math.random()", Integer.MAX_VALUE);
    JsExpr randomTimesArg =
        SoyJsCodeUtils.genJsExprUsingSoySyntax(Operator.TIMES, Lists.newArrayList(random, arg));
    return new JsExpr("Math.floor(" + randomTimesArg.getText() + ")", Integer.MAX_VALUE);
  }
 /**
  * Checks that the given Soy expression translates to the given JsExpr.
  *
  * @param soyExpr The Soy expression to test.
  * @param expectedJsExpr The expected translated JsExpr.
  * @param jsSrcOptions The JsSrc compiler options.
  */
 private void assertTranslation(
     String soyExpr, JsExpr expectedJsExpr, SoyJsSrcOptions jsSrcOptions) {
   SoyFileSetNode soyTree =
       SoyFileSetParserBuilder.forFileContents(
               "{namespace ns autoescape=\"deprecated-noncontextual\"}\n"
                   + "/***/\n"
                   + "{template .aaa}\n"
                   + "{print \n"
                   + soyExpr
                   + "}\n"
                   + "{/template}\n")
           .parse();
   List<PrintNode> printNodes = SoytreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
   ExprNode exprNode = printNodes.get(0).getExprUnion().getExpr();
   JsExpr actualJsExpr =
       new TranslateToJsExprVisitor(
               SOY_JS_SRC_FUNCTIONS_MAP,
               jsSrcOptions,
               LOCAL_VAR_TRANSLATIONS,
               ExplodingErrorReporter.get())
           .exec(exprNode);
   assertThat(actualJsExpr.getText()).isEqualTo(expectedJsExpr.getText());
   assertThat(actualJsExpr.getPrecedence()).isEqualTo(expectedJsExpr.getPrecedence());
 }
  /** @param indicesToNode Series of indices for walking down to the node we want to test. */
  private static void assertGeneratedJsExprs(
      String soyCode, List<JsExpr> expectedJsExprs, int... indicesToNode) {
    ErrorReporter boom = ExplodingErrorReporter.get();
    SoyFileSetNode soyTree =
        SoyFileSetParserBuilder.forTemplateContents(soyCode).errorReporter(boom).parse();
    // Required by testPrintGoogMsg.
    new ReplaceMsgsWithGoogMsgsVisitor(boom).exec(soyTree);
    SoyNode node = SharedTestUtils.getNode(soyTree, indicesToNode);

    GenJsExprsVisitor gjev =
        INJECTOR.getInstance(GenJsExprsVisitorFactory.class).create(LOCAL_VAR_TRANSLATIONS);
    List<JsExpr> actualJsExprs = gjev.exec(node);

    assertThat(actualJsExprs).hasSize(expectedJsExprs.size());
    for (int i = 0; i < expectedJsExprs.size(); i++) {
      JsExpr expectedJsExpr = expectedJsExprs.get(i);
      JsExpr actualJsExpr = actualJsExprs.get(i);
      assertThat(actualJsExpr.getText()).isEqualTo(expectedJsExpr.getText());
      assertThat(actualJsExpr.getPrecedence()).isEqualTo(expectedJsExpr.getPrecedence());
    }
  }
Ejemplo n.º 6
0
  @Override
  public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg = args.get(0);

    return new JsExpr("Math.ceil(" + arg.getText() + ")", Integer.MAX_VALUE);
  }