Search in sources :

Example 1 with StringData

use of com.google.template.soy.data.restricted.StringData 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 2 with StringData

use of com.google.template.soy.data.restricted.StringData 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 3 with StringData

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

the class EvalVisitor method visitLegacyObjectMapLiteralOrMapLiteralNode.

private SoyValue visitLegacyObjectMapLiteralOrMapLiteralNode(AbstractParentExprNode node) {
    checkState(node.getKind() == ExprNode.Kind.LEGACY_OBJECT_MAP_LITERAL_NODE || node.getKind() == ExprNode.Kind.MAP_LITERAL_NODE);
    int numItems = node.numChildren() / 2;
    boolean isStringKeyed = true;
    ExprNode firstNonstringKeyNode = null;
    List<SoyValue> keys = Lists.newArrayListWithCapacity(numItems);
    List<SoyValue> values = Lists.newArrayListWithCapacity(numItems);
    for (int i = 0; i < numItems; i++) {
        SoyValue key = visit(node.getChild(2 * i));
        if (isStringKeyed && !(key instanceof StringData)) {
            isStringKeyed = false;
            // temporary until we support nonstring key
            firstNonstringKeyNode = node.getChild(2 * i);
        }
        keys.add(key);
        values.add(visit(node.getChild(2 * i + 1)));
    }
    if (node.getKind() == ExprNode.Kind.LEGACY_OBJECT_MAP_LITERAL_NODE) {
        if (!isStringKeyed) {
            throw RenderException.create(String.format("legacy_object_map literals must have string keys (key \"%s\" in map %s does not " + "evaluate to a string).", firstNonstringKeyNode.toSourceString(), node.toSourceString()));
        }
        // Not an ImmutableMap, because map literals allow duplicate keys (last one wins).
        Map<String, SoyValue> map = new LinkedHashMap<>();
        for (int i = 0; i < numItems; i++) {
            map.put(keys.get(i).stringValue(), values.get(i));
        }
        return DictImpl.forProviderMap(map, RuntimeMapTypeTracker.Type.LEGACY_OBJECT_MAP_OR_RECORD);
    } else {
        ImmutableMap.Builder<SoyValue, SoyValue> builder = ImmutableMap.builder();
        for (int i = 0; i < numItems; ++i) {
            SoyValue key = keys.get(i);
            SoyValue value = values.get(i);
            if (isNullOrUndefinedBase(key)) {
                throw RenderException.create(String.format("null key in entry: null=%s", value));
            }
            builder.put(key, value);
        }
        return SoyMapImpl.forProviderMap(builder.build());
    }
}
Also used : AbstractParentExprNode(com.google.template.soy.exprtree.AbstractParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) StringData(com.google.template.soy.data.restricted.StringData) SoyValue(com.google.template.soy.data.SoyValue) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with StringData

use of com.google.template.soy.data.restricted.StringData 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)

Example 5 with StringData

use of com.google.template.soy.data.restricted.StringData 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)

Aggregations

StringData (com.google.template.soy.data.restricted.StringData)7 SoyValue (com.google.template.soy.data.SoyValue)5 Test (org.junit.Test)4 SanitizedContent (com.google.template.soy.data.SanitizedContent)2 SoyValueProvider (com.google.template.soy.data.SoyValueProvider)2 FloatData (com.google.template.soy.data.restricted.FloatData)2 HashMap (java.util.HashMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SoyEasyList (com.google.template.soy.data.SoyEasyList)1 SoyList (com.google.template.soy.data.SoyList)1 IntegerData (com.google.template.soy.data.restricted.IntegerData)1 AbstractParentExprNode (com.google.template.soy.exprtree.AbstractParentExprNode)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 LinkedHashMap (java.util.LinkedHashMap)1