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