use of com.google.template.soy.data.SoyList in project closure-templates by google.
the class ListImplTest method testMapMethods.
@Test
public void testMapMethods() {
SoyList list = ListImpl.forProviderList(ImmutableList.of(FloatData.forValue(3.14), BooleanData.TRUE));
assertThat(list.getItemCnt()).isEqualTo(2);
assertThat(list.getItemKeys()).containsExactly(IntegerData.ZERO, IntegerData.ONE).inOrder();
assertThat(list.hasItem(IntegerData.ONE)).isTrue();
assertThat(list.getItem(IntegerData.ZERO).floatValue()).isWithin(0.0).of(3.14);
assertThat(list.getItemProvider(IntegerData.ONE).resolve().booleanValue()).isTrue();
// For backwards compatibility: accept string arguments.
assertThat(list.hasItem(StringData.forValue("0"))).isTrue();
assertThat(list.hasItem(StringData.forValue("-99"))).isFalse();
assertThat(list.hasItem(StringData.forValue("99"))).isFalse();
assertThat(list.getItem(StringData.forValue("0")).floatValue()).isWithin(0.0).of(3.14);
assertThat(list.getItemProvider(StringData.forValue("1")).resolve().booleanValue()).isTrue();
}
use of com.google.template.soy.data.SoyList 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.SoyList 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");
}
use of com.google.template.soy.data.SoyList in project closure-templates by google.
the class EasyListImplTest method testMapMethods.
@Test
public void testMapMethods() {
SoyList list = SoyValueConverterUtility.newList(3.14, true);
assertThat(list.getItemCnt()).isEqualTo(2);
assertThat(list.getItemKeys()).isEqualTo(ImmutableList.of(IntegerData.ZERO, IntegerData.ONE));
assertThat(list.hasItem(IntegerData.ONE)).isTrue();
assertThat(list.getItem(IntegerData.ZERO).floatValue()).isWithin(0.0).of(3.14);
assertThat(list.getItemProvider(IntegerData.ONE).resolve().booleanValue()).isTrue();
// For backwards compatibility: accept string arguments.
assertThat(list.hasItem(StringData.forValue("0"))).isTrue();
assertThat(list.hasItem(StringData.forValue("-99"))).isFalse();
assertThat(list.hasItem(StringData.forValue("99"))).isFalse();
assertThat(list.getItem(StringData.forValue("0")).floatValue()).isWithin(0.0).of(3.14);
assertThat(list.getItemProvider(StringData.forValue("1")).resolve().booleanValue()).isTrue();
}
use of com.google.template.soy.data.SoyList in project closure-templates by google.
the class RenderVisitor method visitForNode.
@Override
protected void visitForNode(ForNode node) {
Optional<RangeArgs> exprAsRangeArgs = RangeArgs.createFromNode(node);
if (exprAsRangeArgs.isPresent()) {
RangeArgs args = exprAsRangeArgs.get();
int step = args.increment().isPresent() ? evalRangeArg(node, args.increment().get()) : 1;
int start = args.start().isPresent() ? evalRangeArg(node, args.start().get()) : 0;
int end = evalRangeArg(node, args.limit());
int length = end - start;
if ((length ^ step) < 0) {
// handle ifempty, if present
if (node.numChildren() == 2) {
visit(node.getChild(1));
}
} else {
ForNonemptyNode child = (ForNonemptyNode) node.getChild(0);
int size = length / step + (length % step == 0 ? 0 : 1);
for (int i = 0; i < size; ++i) {
executeForeachBody(child, i, IntegerData.forValue(start + step * i), size);
}
}
} else {
SoyValue dataRefValue = eval(node.getExpr(), node);
if (!(dataRefValue instanceof SoyList)) {
throw RenderException.createWithSource("In 'foreach' command " + node.toSourceString() + ", the data reference does not " + "resolve to a SoyList " + "(encountered type " + dataRefValue.getClass().getName() + ").", node);
}
SoyList foreachList = (SoyList) dataRefValue;
int listLength = foreachList.length();
if (listLength > 0) {
// Case 1: Nonempty list.
ForNonemptyNode child = (ForNonemptyNode) node.getChild(0);
for (int i = 0; i < listLength; ++i) {
executeForeachBody(child, i, foreachList.getProvider(i), listLength);
}
} else {
// Case 2: Empty list. If the 'ifempty' node exists, visit it.
if (node.numChildren() == 2) {
visit(node.getChild(1));
}
}
}
}
Aggregations