Search in sources :

Example 16 with SoyDict

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

the class DictImplTest method testDictMethods.

@Test
public void testDictMethods() {
    SoyDict dict = DictImpl.forProviderMap(ImmutableMap.of("boo", StringData.forValue("aaah"), "foo", FloatData.forValue(3.14)), RuntimeMapTypeTracker.Type.UNKNOWN);
    Map<String, ? extends SoyValueProvider> m1 = dict.asJavaStringMap();
    assertThat(m1).hasSize(2);
    assertThat(m1.get("boo").resolve().stringValue()).isEqualTo("aaah");
    Map<String, ? extends SoyValue> m2 = dict.asResolvedJavaStringMap();
    assertThat(m2).hasSize(2);
    assertThat(m2.get("foo").floatValue()).isWithin(0.0).of(3.14);
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 17 with SoyDict

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

the class DictImplTest method testRecordMethods.

@Test
public void testRecordMethods() {
    Map<String, SoyValueProvider> providerMap = new HashMap<>();
    SoyDict dict = DictImpl.forProviderMap(providerMap, RuntimeMapTypeTracker.Type.UNKNOWN);
    assertThat(dict.hasField("boo")).isFalse();
    assertThat(dict.getField("boo")).isNull();
    assertThat(dict.getFieldProvider("boo")).isNull();
    providerMap.put("boo", StringData.forValue("blah"));
    assertThat(dict.hasField("boo")).isTrue();
    assertThat(dict.getField("boo").stringValue()).isEqualTo("blah");
    assertThat(dict.getFieldProvider("boo").resolve().stringValue()).isEqualTo("blah");
    providerMap.remove("boo");
    assertThat(dict.hasField("boo")).isFalse();
    assertThat(dict.getField("boo")).isNull();
    assertThat(dict.getFieldProvider("boo")).isNull();
    providerMap.put("foo", FloatData.forValue(3.14));
    providerMap.put("too", BooleanData.TRUE);
    assertThat(dict.hasField("foo")).isTrue();
    assertThat(dict.getField("foo").floatValue()).isWithin(0.0).of(3.14);
    assertThat(dict.getField("too").booleanValue()).isTrue();
}
Also used : SoyValueProvider(com.google.template.soy.data.SoyValueProvider) HashMap(java.util.HashMap) SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 18 with SoyDict

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

the class AugmentMapFunctionTest method testComputeForJava.

@Test
public void testComputeForJava() {
    AugmentMapFunction augmentMapFunction = new AugmentMapFunction();
    SoyLegacyObjectMap origMap = SoyValueConverterUtility.newDict("aaa", "blah", "bbb", "bleh", "ccc", SoyValueConverterUtility.newDict("xxx", 2));
    SoyLegacyObjectMap additionalMap = SoyValueConverterUtility.newDict("aaa", "bluh", "ccc", SoyValueConverterUtility.newDict("yyy", 5));
    SoyDict augmentedDict = (SoyDict) augmentMapFunction.computeForJava(ImmutableList.<SoyValue>of(origMap, additionalMap));
    assertThat(augmentedDict.getField("aaa").stringValue()).isEqualTo("bluh");
    assertThat(augmentedDict.getItem(StringData.forValue("bbb")).stringValue()).isEqualTo("bleh");
    assertThat(((SoyDict) augmentedDict.getField("ccc")).getField("yyy").integerValue()).isEqualTo(5);
    assertThat(((SoyDict) augmentedDict.getField("ccc")).getField("xxx")).isEqualTo(null);
}
Also used : SoyLegacyObjectMap(com.google.template.soy.data.SoyLegacyObjectMap) SoyDict(com.google.template.soy.data.SoyDict) SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 19 with SoyDict

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

the class TofuExceptionsTest method testExceptions_wrongTypeFuture.

@Test
public void testExceptions_wrongTypeFuture() {
    SoyDict data = SoyValueConverterUtility.newDict("foo", Futures.immediateFuture("not a record"));
    // This error occurs due to data of the wrong type, hidden behind a future.
    try {
        tofu.newRenderer("ns.callerTemplate").setData(data).render();
        fail();
    } catch (SoyTofuException ste) {
        assertThat(ste).hasCauseThat().isNull();
        assertThat(ste).hasMessageThat().isEqualTo("When evaluating \"$foo.boo\": Parameter type mismatch: attempt to bind value " + "'not a record' (a StringData) to parameter 'foo' which has a declared type " + "of '[bad: string, boo: int]'.");
        assertThat(ste.getStackTrace()[0].toString()).isEqualTo("ns.calleeTemplate(no-path:8)");
        assertThat(ste.getStackTrace()[1].toString()).isEqualTo("ns.calleeTemplate(no-path:10)");
        assertThat(ste.getStackTrace()[2].toString()).isEqualTo("ns.callerTemplate(no-path:5)");
    }
}
Also used : SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 20 with SoyDict

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

the class TofuExceptionsTest method testExceptions_failedFuture.

@Test
public void testExceptions_failedFuture() {
    Exception futureFailureCause = new Exception("boom");
    SoyDict data = SoyValueConverterUtility.newDict("foo", immediateFailedFuture(futureFailureCause));
    // This error occurs due to a failed future.
    try {
        tofu.newRenderer("ns.callerTemplate").setData(data).render();
        fail();
    } catch (SoyTofuException ste) {
        assertThat(ste).hasMessageThat().isEqualTo("When evaluating \"$foo.boo\": Error dereferencing future");
        SoyFutureException sfe = (SoyFutureException) ste.getCause();
        assertThat(sfe).hasMessageThat().isEqualTo("Error dereferencing future");
        assertThat(sfe).hasCauseThat().isEqualTo(futureFailureCause);
        assertThat(ste.getStackTrace()[0].toString()).isEqualTo("ns.calleeTemplate(no-path:10)");
        assertThat(ste.getStackTrace()[1].toString()).isEqualTo("ns.callerTemplate(no-path:5)");
    }
}
Also used : SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyDict(com.google.template.soy.data.SoyDict) SoyFutureException(com.google.template.soy.data.SoyFutureException) SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyFutureException(com.google.template.soy.data.SoyFutureException) Test(org.junit.Test)

Aggregations

SoyDict (com.google.template.soy.data.SoyDict)26 Test (org.junit.Test)25 SoyTofuException (com.google.template.soy.tofu.SoyTofuException)6 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)2 SoyFutureException (com.google.template.soy.data.SoyFutureException)2 SoyLegacyObjectMap (com.google.template.soy.data.SoyLegacyObjectMap)2 SoyValue (com.google.template.soy.data.SoyValue)2 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)2 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)2 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)2 SoyRecord (com.google.template.soy.data.SoyRecord)1 SoyValueProvider (com.google.template.soy.data.SoyValueProvider)1 SoyMapImpl (com.google.template.soy.data.internal.SoyMapImpl)1 TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1 Flushable (java.io.Flushable)1 HashMap (java.util.HashMap)1