Search in sources :

Example 16 with SoyValue

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

the class AbstractSoyPrintDirectiveTestCase method assertTofuOutput.

/**
 * @param expectedOutput The expected result of applying directive to value with args.
 * @param value The test input.
 * @param directive The directive whose {@link SoyJavaPrintDirective#applyForJava} is under test.
 * @param args Arguments to the Soy directive.
 */
protected void assertTofuOutput(SoyValue expectedOutput, Object value, SoyJavaPrintDirective directive, Object... args) {
    ImmutableList.Builder<SoyValue> argsData = ImmutableList.builder();
    for (Object arg : args) {
        argsData.add(SoyValueConverter.INSTANCE.convert(arg).resolve());
    }
    assertThat(directive.applyForJava(SoyValueConverter.INSTANCE.convert(value).resolve(), argsData.build()).toString()).isEqualTo(expectedOutput.toString());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) SoyValue(com.google.template.soy.data.SoyValue)

Example 17 with SoyValue

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

the class ListImplTest method testListMethods.

@Test
public void testListMethods() {
    StringData BLAH_0 = StringData.forValue("blah");
    FloatData PI = FloatData.forValue(3.14);
    SoyValue BLAH_2 = StringData.forValue("blah");
    SoyList list = ListImpl.forProviderList(EMPTY);
    assertThat(list.length()).isEqualTo(0);
    assertThat(list.asJavaList()).isEmpty();
    assertThat(list.asResolvedJavaList()).isEmpty();
    assertThat(list.get(0)).isNull();
    assertThat(list.getProvider(0)).isNull();
    list = ListImpl.forProviderList(ImmutableList.of(BLAH_0, PI, 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 : FloatData(com.google.template.soy.data.restricted.FloatData) SoyList(com.google.template.soy.data.SoyList) StringData(com.google.template.soy.data.restricted.StringData) SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 18 with SoyValue

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

the class ListImplTest method testSoyValueMethods.

@Test
public void testSoyValueMethods() {
    SoyValue val1 = ListImpl.forProviderList(EMPTY);
    // ListImpl is always truthy.
    assertThat(val1.coerceToBoolean()).isTrue();
    assertThat(val1.coerceToString()).isEqualTo("[]");
    SoyValue val2 = ListImpl.forProviderList(EMPTY);
    // ListImpl uses object identity.
    assertThat(val1.equals(val2)).isFalse();
    SoyValue val3 = ListImpl.forProviderList(ImmutableList.of(IntegerData.forValue(111), BooleanData.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 19 with SoyValue

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

the class FloorFunctionTest method testComputeForJava.

@Test
public void testComputeForJava() {
    FloorFunction floorFunction = new FloorFunction();
    SoyValue float0 = FloatData.forValue(7.5);
    assertThat(floorFunction.computeForJava(ImmutableList.of(float0))).isEqualTo(IntegerData.forValue(7));
    SoyValue integer = IntegerData.forValue(14);
    assertThat(floorFunction.computeForJava(ImmutableList.of(integer))).isEqualTo(IntegerData.forValue(14));
}
Also used : SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 20 with SoyValue

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

the class StrIndexOfFunctionTest method testComputeForJava_doesNotContainSanitizedContent.

@Test
public void testComputeForJava_doesNotContainSanitizedContent() {
    StrIndexOfFunction strIndexOf = new StrIndexOfFunction();
    SoyValue arg0 = ordainAsSafe("foobarfoo", ContentKind.TEXT);
    SoyValue arg1 = ordainAsSafe("baz", ContentKind.TEXT);
    assertThat(strIndexOf.computeForJava(ImmutableList.of(arg0, arg1))).isEqualTo(IntegerData.forValue(-1));
}
Also used : SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

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