Search in sources :

Example 1 with SoyValueProvider

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

the class ListBackedList method render.

@Override
public void render(LoggingAdvisingAppendable appendable) throws IOException {
    appendable.append('[');
    boolean isFirst = true;
    for (SoyValueProvider valueProvider : asJavaList()) {
        if (isFirst) {
            isFirst = false;
        } else {
            appendable.append(", ");
        }
        valueProvider.resolve().render(appendable);
    }
    appendable.append(']');
}
Also used : SoyValueProvider(com.google.template.soy.data.SoyValueProvider)

Example 2 with SoyValueProvider

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

the class SoySauceTest method testExceptionRewriting.

@Test
public void testExceptionRewriting() {
    SoySauce.Renderer tmpl = sauce.renderTemplate("strict_test.callsItself");
    SoyValueProvider intProvider = new DetachableSoyValueProvider() {

        @Override
        protected RenderResult doResolve() {
            resolvedValue = IntegerData.ZERO;
            return RenderResult.done();
        }
    };
    try {
        tmpl.setData(ImmutableMap.of("depth", 10, "p", intProvider)).render();
        fail();
    } catch (ClassCastException cce) {
        // we get an CCE because we passed an int but it expected a string
        StackTraceElement[] stackTrace = cce.getStackTrace();
        assertThat(stackTrace[0].toString()).isEqualTo("strict_test.callsItself.render(strict.soy:32)");
        for (int i = 1; i < 11; i++) {
            assertThat(stackTrace[i].toString()).isEqualTo("strict_test.callsItself.render(strict.soy:34)");
        }
    }
}
Also used : DetachableSoyValueProvider(com.google.template.soy.jbcsrc.runtime.DetachableSoyValueProvider) SoyValueProvider(com.google.template.soy.data.SoyValueProvider) DetachableSoyValueProvider(com.google.template.soy.jbcsrc.runtime.DetachableSoyValueProvider) Test(org.junit.Test)

Example 3 with SoyValueProvider

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

the class DictImplTest method testLegacyObjectMapMethods.

@Test
public void testLegacyObjectMapMethods() {
    StringData boo = StringData.forValue("boo");
    Map<String, SoyValueProvider> providerMap = new HashMap<>();
    DictImpl dict = DictImpl.forProviderMap(providerMap, RuntimeMapTypeTracker.Type.UNKNOWN);
    assertThat(dict.getItemCnt()).isEqualTo(0);
    assertThat(dict.getItemKeys()).isEmpty();
    assertThat(dict.hasItem(boo)).isFalse();
    assertThat(dict.getItem(boo)).isNull();
    assertThat(dict.getItemProvider(boo)).isNull();
    providerMap.put("boo", IntegerData.forValue(111));
    assertThat(dict.getItemCnt()).isEqualTo(1);
    assertThat(dict.getItemKeys()).hasSize(1);
    assertThat(Iterables.getOnlyElement(dict.getItemKeys()).stringValue()).isEqualTo("boo");
    providerMap.put("foo", IntegerData.forValue(222));
    providerMap.put("goo", IntegerData.forValue(333));
    assertThat(dict.getItemCnt()).isEqualTo(3);
    assertThat(dict.getItemKeys()).hasSize(3);
    assertThat(dict.hasItem(boo)).isTrue();
    assertThat(dict.getItem(boo).integerValue()).isEqualTo(111);
    assertThat(dict.getItemProvider(boo).resolve().integerValue()).isEqualTo(111);
    providerMap.remove("foo");
    assertThat(dict.getItemCnt()).isEqualTo(2);
    providerMap.remove("boo");
    providerMap.remove("goo");
    assertThat(dict.getItemCnt()).isEqualTo(0);
    assertThat(dict.getItemKeys()).isEmpty();
    assertThat(dict.hasItem(boo)).isFalse();
    assertThat(dict.getItem(boo)).isNull();
    assertThat(dict.getItemProvider(boo)).isNull();
}
Also used : SoyValueProvider(com.google.template.soy.data.SoyValueProvider) HashMap(java.util.HashMap) StringData(com.google.template.soy.data.restricted.StringData) Test(org.junit.Test)

Example 4 with SoyValueProvider

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

the class KeysFunctionTest method testComputeForJava.

@Test
public void testComputeForJava() {
    KeysFunction keysFunction = new KeysFunction();
    SoyValue map = SoyValueConverterUtility.newDict("boo", "bar", "foo", 2, "goo", SoyValueConverterUtility.newDict("moo", 4));
    SoyValue result = keysFunction.computeForJava(ImmutableList.of(map));
    assertThat(result).isInstanceOf(SoyList.class);
    SoyList resultAsList = (SoyList) result;
    assertThat(resultAsList.length()).isEqualTo(3);
    Set<String> resultItems = Sets.newHashSet();
    for (SoyValueProvider itemProvider : resultAsList.asJavaList()) {
        resultItems.add(itemProvider.resolve().stringValue());
    }
    assertThat(resultItems).containsExactly("boo", "foo", "goo");
}
Also used : SoyValueProvider(com.google.template.soy.data.SoyValueProvider) SoyList(com.google.template.soy.data.SoyList) SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Example 5 with SoyValueProvider

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

the class DictImplTest method testMapMethods.

@Test
public void testMapMethods() {
    StringData boo = StringData.forValue("boo");
    Map<String, SoyValueProvider> providerMap = new HashMap<>();
    DictImpl dict = DictImpl.forProviderMap(providerMap, RuntimeMapTypeTracker.Type.UNKNOWN);
    assertThat(dict.size()).isEqualTo(0);
    assertThat(dict.keys()).isEmpty();
    assertThat(dict.containsKey(boo)).isFalse();
    assertThat(dict.get(boo)).isNull();
    assertThat(dict.getProvider(boo)).isNull();
    providerMap.put("boo", IntegerData.forValue(111));
    assertThat(dict.size()).isEqualTo(1);
    assertThat(dict.keys()).hasSize(1);
    assertThat(Iterables.getOnlyElement(dict.keys()).stringValue()).isEqualTo("boo");
    providerMap.put("foo", IntegerData.forValue(222));
    providerMap.put("goo", IntegerData.forValue(333));
    assertThat(dict.size()).isEqualTo(3);
    assertThat(dict.keys()).hasSize(3);
    assertThat(dict.containsKey(boo)).isTrue();
    assertThat(dict.get(boo).integerValue()).isEqualTo(111);
    assertThat(dict.getProvider(boo).resolve().integerValue()).isEqualTo(111);
    providerMap.remove("foo");
    assertThat(dict.size()).isEqualTo(2);
    providerMap.remove("boo");
    providerMap.remove("goo");
    assertThat(dict.size()).isEqualTo(0);
    assertThat(dict.keys()).isEmpty();
    assertThat(dict.containsKey(boo)).isFalse();
    assertThat(dict.get(boo)).isNull();
    assertThat(dict.getProvider(boo)).isNull();
}
Also used : SoyValueProvider(com.google.template.soy.data.SoyValueProvider) HashMap(java.util.HashMap) StringData(com.google.template.soy.data.restricted.StringData) Test(org.junit.Test)

Aggregations

SoyValueProvider (com.google.template.soy.data.SoyValueProvider)6 Test (org.junit.Test)5 HashMap (java.util.HashMap)3 StringData (com.google.template.soy.data.restricted.StringData)2 SoyDict (com.google.template.soy.data.SoyDict)1 SoyList (com.google.template.soy.data.SoyList)1 SoyValue (com.google.template.soy.data.SoyValue)1 DetachableSoyValueProvider (com.google.template.soy.jbcsrc.runtime.DetachableSoyValueProvider)1