From b83d8ccab226581074a2808321db5beb9451ae99 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 21 Aug 2019 10:01:07 -0400 Subject: [PATCH 1/3] Add prepend operation to list --- .../jinjava/objects/collections/PyList.java | 3 ++ .../objects/collections/PyListTest.java | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java diff --git a/src/main/java/com/hubspot/jinjava/objects/collections/PyList.java b/src/main/java/com/hubspot/jinjava/objects/collections/PyList.java index f92d44455..b9be3420f 100644 --- a/src/main/java/com/hubspot/jinjava/objects/collections/PyList.java +++ b/src/main/java/com/hubspot/jinjava/objects/collections/PyList.java @@ -26,4 +26,7 @@ public boolean append(Object e) { return add(e); } + public void prepend(Object e) { + add(0, e); + } } diff --git a/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java b/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java new file mode 100644 index 000000000..28b78677c --- /dev/null +++ b/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java @@ -0,0 +1,34 @@ +package com.hubspot.jinjava.objects.collections; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Collections; + +import org.junit.Before; +import org.junit.Test; + +import com.hubspot.jinjava.Jinjava; + +public class PyListTest { + + private Jinjava jinjava; + + @Before + public void setup() { + jinjava = new Jinjava(); + } + + @Test + public void itSupportsAppendOperation() { + assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + + "{% do test.append(4) %}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("[1, 2, 3, 4]"); + } + + @Test + public void itSupportsPrependOperation() { + assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + + "{% do test.prepend(4) %}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("[4, 1, 2, 3]"); + } +} From 08d63b23eb6aae47553699de8371fcc7cee254e2 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 21 Aug 2019 16:04:18 -0400 Subject: [PATCH 2/3] More operations. --- .../jinjava/objects/collections/PyList.java | 46 ++++++++++++- .../objects/collections/PyListTest.java | 68 ++++++++++++++++++- 2 files changed, 109 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/objects/collections/PyList.java b/src/main/java/com/hubspot/jinjava/objects/collections/PyList.java index b9be3420f..a159d6dd9 100644 --- a/src/main/java/com/hubspot/jinjava/objects/collections/PyList.java +++ b/src/main/java/com/hubspot/jinjava/objects/collections/PyList.java @@ -1,6 +1,9 @@ package com.hubspot.jinjava.objects.collections; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Objects; import com.google.common.collect.ForwardingList; import com.hubspot.jinjava.objects.PyWrapper; @@ -26,7 +29,46 @@ public boolean append(Object e) { return add(e); } - public void prepend(Object e) { - add(0, e); + public void insert(int i, Object e) { + add(i, e); + } + + public boolean extend(PyList e) { + return addAll(e.list); + } + + public Object pop() { + return remove(list.size() - 1); + } + + public Object pop(int index) { + return remove(index); + } + + public long count(Object o) { + return stream() + .filter(object -> Objects.equals(object, o)) + .count(); + } + + public void reverse() { + Collections.reverse(list); + } + + public PyList copy() { + return new PyList(new ArrayList<>(list)); + } + + public int index(Object o) { + return indexOf(o); + } + + public int index(Object o, int begin, int end) { + for (int i = begin; i < end; i++) { + if (Objects.equals(o, get(i))) { + return i; + } + } + return -1; } } diff --git a/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java b/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java index 28b78677c..a582a9dac 100644 --- a/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java +++ b/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java @@ -26,9 +26,71 @@ public void itSupportsAppendOperation() { } @Test - public void itSupportsPrependOperation() { + public void itSupportsExtendOperation() { assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + - "{% do test.prepend(4) %}" + - "{{ test }}", Collections.emptyMap())).isEqualTo("[4, 1, 2, 3]"); + "{% do test.extend([4, 5, 6]) %}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("[1, 2, 3, 4, 5, 6]"); + } + + @Test + public void itSupportsInsertOperation() { + assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + + "{% do test.insert(1, 4) %}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("[1, 4, 2, 3]"); + } + + @Test + public void itSupportsPopOperation() { + assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + + "{{ test.pop() }}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("3[1, 2]"); + } + + @Test + public void itSupportsPopAtIndexOperation() { + assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + + "{{ test.pop(1) }}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("2[1, 3]"); + } + + @Test + public void itSupportsClearOperation() { + assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + + "{% do test.clear() %}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("[]"); + } + + @Test + public void itSupportsCountOperation() { + assertThat(jinjava.render("{% set test = [1, 1, 2, 2, 2, 3] %}" + + "{{ test.count(2) }}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("3[1, 1, 2, 2, 2, 3]"); + } + + @Test + public void itSupportsReverseOperation() { + assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + + "{% do test.reverse() %}" + + "{{ test }}", Collections.emptyMap())).isEqualTo("[3, 2, 1]"); + } + + @Test + public void itSupportsCopyOperation() { + assertThat(jinjava.render("{% set test = [1, 2, 3] %}" + + "{% set test2 = test.copy() %}" + + "{% do test.append(4) %}" + + "{{ test }}{{test2}}", Collections.emptyMap())).isEqualTo("[1, 2, 3, 4][1, 2, 3]"); + } + + @Test + public void itSupportsIndexOperation() { + assertThat(jinjava.render("{% set test = [10, 20, 30, 10, 20, 30] %}" + + "{{ test.index(20) }}", Collections.emptyMap())).isEqualTo("1"); + } + + @Test + public void itSupportsIndexWithinBoundsOperation() { + assertThat(jinjava.render("{% set test = [10, 20, 30, 10, 20, 30] %}" + + "{{ test.index(20, 2, 6) }}", Collections.emptyMap())).isEqualTo("4"); } } From efd73c7bd67e44e22566dcd3c3d8dbd94690ef78 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 21 Aug 2019 16:10:23 -0400 Subject: [PATCH 3/3] More tests --- .../jinjava/objects/collections/PyListTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java b/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java index a582a9dac..3c97e5b67 100644 --- a/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java +++ b/src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java @@ -93,4 +93,16 @@ public void itSupportsIndexWithinBoundsOperation() { assertThat(jinjava.render("{% set test = [10, 20, 30, 10, 20, 30] %}" + "{{ test.index(20, 2, 6) }}", Collections.emptyMap())).isEqualTo("4"); } + + @Test + public void itReturnsNegativeOneForMissingObjectForIndex() { + assertThat(jinjava.render("{% set test = [10, 20, 30, 10, 20, 30] %}" + + "{{ test.index(999) }}", Collections.emptyMap())).isEqualTo("-1"); + } + + @Test + public void itReturnsNegativeOneForMissingObjectForIndexWithinBounds() { + assertThat(jinjava.render("{% set test = [10, 20, 30, 10, 20, 30] %}" + + "{{ test.index(999, 1, 5) }}", Collections.emptyMap())).isEqualTo("-1"); + } }