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);
}
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();
}
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);
}
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)");
}
}
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)");
}
}
Aggregations