Search in sources :

Example 26 with SoyValue

use of com.google.template.soy.data.SoyValue in project closure-templates by google.

the class StrLenFunctionTest method testComputeForJava_containsString.

@Test
public void testComputeForJava_containsString() {
    StrLenFunction strLen = new StrLenFunction();
    SoyValue arg0 = StringData.forValue("foobarfoo");
    assertThat(strLen.computeForJava(ImmutableList.of(arg0))).isEqualTo(IntegerData.forValue(9));
}
Also used : SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 27 with SoyValue

use of com.google.template.soy.data.SoyValue in project closure-templates by google.

the class BidiDirAttrFunctionTest method testComputeForJava.

@Test
public void testComputeForJava() {
    SoyValue text = StringData.EMPTY_STRING;
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("", SanitizedContent.ContentKind.ATTRIBUTES));
    text = StringData.forValue("a");
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("", SanitizedContent.ContentKind.ATTRIBUTES));
    text = StringData.forValue("\u05E0");
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("dir=\"rtl\"", SanitizedContent.ContentKind.ATTRIBUTES));
    text = SanitizedContents.unsanitizedText("\u05E0");
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("dir=\"rtl\"", SanitizedContent.ContentKind.ATTRIBUTES));
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.RTL);
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("dir=\"rtl\"", SanitizedContent.ContentKind.ATTRIBUTES));
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.LTR);
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("", SanitizedContent.ContentKind.ATTRIBUTES));
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.NEUTRAL);
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("", SanitizedContent.ContentKind.ATTRIBUTES));
    text = StringData.EMPTY_STRING;
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("", SanitizedContent.ContentKind.ATTRIBUTES));
    text = StringData.forValue("\u05E0");
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("", SanitizedContent.ContentKind.ATTRIBUTES));
    text = StringData.forValue("a");
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("dir=\"ltr\"", SanitizedContent.ContentKind.ATTRIBUTES));
    text = SanitizedContents.unsanitizedText("a");
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("dir=\"ltr\"", SanitizedContent.ContentKind.ATTRIBUTES));
    text = SanitizedContents.unsanitizedText("a", Dir.LTR);
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("dir=\"ltr\"", SanitizedContent.ContentKind.ATTRIBUTES));
    text = SanitizedContents.unsanitizedText("a", Dir.RTL);
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("", SanitizedContent.ContentKind.ATTRIBUTES));
    text = SanitizedContents.unsanitizedText("a", Dir.NEUTRAL);
    assertThat(BIDI_DIR_ATTR_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(UnsafeSanitizedContentOrdainer.ordainAsSafe("", SanitizedContent.ContentKind.ATTRIBUTES));
}
Also used : SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 28 with SoyValue

use of com.google.template.soy.data.SoyValue in project closure-templates by google.

the class SimplifyExprVisitor method visitAndOpNode.

// -----------------------------------------------------------------------------------------------
// Implementations for operators.
@Override
protected void visitAndOpNode(AndOpNode node) {
    // Recurse.
    visitChildren(node);
    // Can simplify if either child is constant. We assume no side-effects.
    SoyValue operand0 = getConstantOrNull(node.getChild(0));
    if (operand0 != null) {
        ExprNode replacementNode = operand0.coerceToBoolean() ? node.getChild(1) : node.getChild(0);
        node.getParent().replaceChild(node, replacementNode);
    }
}
Also used : ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) SoyValue(com.google.template.soy.data.SoyValue)

Example 29 with SoyValue

use of com.google.template.soy.data.SoyValue in project closure-templates by google.

the class SimplifyExprVisitor method visitOrOpNode.

@Override
protected void visitOrOpNode(OrOpNode node) {
    // Recurse.
    visitChildren(node);
    // Can simplify if either child is constant. We assume no side-effects.
    SoyValue operand0 = getConstantOrNull(node.getChild(0));
    if (operand0 != null) {
        ExprNode replacementNode = operand0.coerceToBoolean() ? node.getChild(0) : node.getChild(1);
        node.getParent().replaceChild(node, replacementNode);
    }
}
Also used : ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) SoyValue(com.google.template.soy.data.SoyValue)

Example 30 with SoyValue

use of com.google.template.soy.data.SoyValue in project closure-templates by google.

the class SimplifyExprVisitor method visitConditionalOpNode.

@Override
protected void visitConditionalOpNode(ConditionalOpNode node) {
    // Recurse.
    visitChildren(node);
    // Can simplify if operand0 is constant. We assume no side-effects.
    SoyValue operand0 = getConstantOrNull(node.getChild(0));
    if (operand0 == null) {
        // cannot simplify
        return;
    }
    ExprNode replacementNode = operand0.coerceToBoolean() ? node.getChild(1) : node.getChild(2);
    node.getParent().replaceChild(node, replacementNode);
}
Also used : ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) SoyValue(com.google.template.soy.data.SoyValue)

Aggregations

SoyValue (com.google.template.soy.data.SoyValue)61 Test (org.junit.Test)31 StringData (com.google.template.soy.data.restricted.StringData)5 ExprNode (com.google.template.soy.exprtree.ExprNode)4 SoyLegacyObjectMap (com.google.template.soy.data.SoyLegacyObjectMap)3 SoyList (com.google.template.soy.data.SoyList)3 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)2 SanitizedContent (com.google.template.soy.data.SanitizedContent)2 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)2 SoyDataException (com.google.template.soy.data.SoyDataException)2 SoyDict (com.google.template.soy.data.SoyDict)2 SoyRecord (com.google.template.soy.data.SoyRecord)2 FloatData (com.google.template.soy.data.restricted.FloatData)2 IntegerData (com.google.template.soy.data.restricted.IntegerData)2 SoyString (com.google.template.soy.data.restricted.SoyString)2 UndefinedData (com.google.template.soy.data.restricted.UndefinedData)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)2