Search in sources :

Example 51 with SoyValue

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

the class BidiMarkAfterFunctionTest method testComputeForJava.

@Test
public void testComputeForJava() {
    SoyValue text = StringData.EMPTY_STRING;
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = StringData.forValue("a");
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = StringData.forValue("\u05E0");
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200E"));
    text = SanitizedContents.unsanitizedText("a");
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = SanitizedContents.unsanitizedText("a", Dir.LTR);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = SanitizedContents.unsanitizedText("a", Dir.NEUTRAL);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = SanitizedContents.unsanitizedText("a", Dir.RTL);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200E"));
    text = SanitizedContents.unsanitizedText("\u05E0");
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200E"));
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.RTL);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200E"));
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.NEUTRAL);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200E"));
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.LTR);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_LTR.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200E"));
    text = StringData.EMPTY_STRING;
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = StringData.forValue("\u05E0");
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = StringData.forValue("a");
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200F"));
    text = SanitizedContents.unsanitizedText("\u05E0");
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.RTL);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.NEUTRAL);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.EMPTY_STRING);
    text = SanitizedContents.unsanitizedText("\u05E0", Dir.LTR);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200F"));
    text = SanitizedContents.unsanitizedText("a");
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200F"));
    text = SanitizedContents.unsanitizedText("a", Dir.LTR);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200F"));
    text = SanitizedContents.unsanitizedText("a", Dir.NEUTRAL);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200F"));
    text = SanitizedContents.unsanitizedText("a", Dir.RTL);
    assertThat(BIDI_MARK_AFTER_FUNCTION_FOR_STATIC_RTL.computeForJava(ImmutableList.of(text))).isEqualTo(StringData.forValue("\u200F"));
}
Also used : SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 52 with SoyValue

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

the class EasyListImplTest method testListMethods.

@Test
public void testListMethods() {
    StringData BLAH_0 = StringData.forValue("blah");
    FloatData PI = FloatData.forValue(3.14);
    SoyValue BLAH_2 = StringData.forValue("blah");
    SoyEasyList list = new EasyListImpl();
    assertThat(list.length()).isEqualTo(0);
    assertThat(list.asJavaList()).isEmpty();
    assertThat(list.asResolvedJavaList()).isEmpty();
    assertThat(list.get(0)).isNull();
    assertThat(list.getProvider(0)).isNull();
    list.add(BLAH_0);
    list.add(PI);
    list.add(BLAH_2);
    // At this point, list should be [BLAH_0, PI, BLAH_2].
    assertThat(list.length()).isEqualTo(3);
    assertThat(list.asJavaList()).isEqualTo(ImmutableList.of(BLAH_0, PI, BLAH_2));
    assertThat(list.asResolvedJavaList()).isEqualTo(ImmutableList.of(BLAH_0, PI, BLAH_2));
    assertThat(list.get(0)).isSameAs(BLAH_0);
    assertThat(list.get(0)).isNotSameAs(BLAH_2);
    // not same, but they compare equal
    assertThat(list.get(0)).isEqualTo(BLAH_2);
    assertThat(list.getProvider(1).resolve().floatValue()).isWithin(0.0).of(3.14);
}
Also used : SoyEasyList(com.google.template.soy.data.SoyEasyList) FloatData(com.google.template.soy.data.restricted.FloatData) StringData(com.google.template.soy.data.restricted.StringData) SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 53 with SoyValue

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

the class EasyListImplTest method testSoyValueMethods.

@Test
public void testSoyValueMethods() {
    SoyValue val1 = new EasyListImpl();
    // EasyListImpl is always truthy.
    assertThat(val1.coerceToBoolean()).isTrue();
    assertThat(val1.coerceToString()).isEqualTo("[]");
    SoyValue val2 = new EasyListImpl();
    // EasyListImpl uses object identity.
    assertThat(val1.equals(val2)).isFalse();
    SoyValue val3 = SoyValueConverterUtility.newList(111, true);
    assertThat(val3.coerceToBoolean()).isTrue();
    assertThat(val3.coerceToString()).isEqualTo("[111, true]");
}
Also used : SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 54 with SoyValue

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

the class RenderVisitor method visitPrintNode.

@Override
protected void visitPrintNode(PrintNode node) {
    SoyValue result = eval(node.getExpr(), node);
    if (result instanceof UndefinedData) {
        throw RenderException.createWithSource("In 'print' tag, expression \"" + node.getExpr().toSourceString() + "\" evaluates to undefined.", node);
    }
    // Process directives.
    for (PrintDirectiveNode directiveNode : node.getChildren()) {
        // Evaluate directive args.
        List<ExprRootNode> argsExprs = directiveNode.getArgs();
        List<SoyValue> argsSoyDatas = Lists.newArrayListWithCapacity(argsExprs.size());
        for (ExprRootNode argExpr : argsExprs) {
            argsSoyDatas.add(eval(argExpr, directiveNode));
        }
        // Apply directive.
        result = applyDirective(directiveNode.getPrintDirective(), result, argsSoyDatas, node);
    }
    append(currOutputBuf, result, node);
}
Also used : PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) UndefinedData(com.google.template.soy.data.restricted.UndefinedData) SoyValue(com.google.template.soy.data.SoyValue) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 55 with SoyValue

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

the class RoundFunction method computeForJava.

@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue value = args.get(0);
    int numDigitsAfterPt = (args.size() == 2) ? args.get(1).integerValue() : 0;
    return BasicFunctionsRuntime.round(value, numDigitsAfterPt);
}
Also used : 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