freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [freemarker] 02/02: Fixed target type isSequence logic in IntermediateStreamOperationLikeBuiltIn.
Date Sat, 20 Jul 2019 15:30:52 GMT
This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch 2.3-gae
in repository https://gitbox.apache.org/repos/asf/freemarker.git

commit 4c41a640c2db77964f8ccbf52e0a8f9bd7ed906a
Author: ddekany <ddekany@apache.org>
AuthorDate: Sat Jul 20 17:16:06 2019 +0200

    Fixed target type isSequence logic in IntermediateStreamOperationLikeBuiltIn.
---
 .../java/freemarker/core/BuiltInsForSequences.java |  5 ++--
 src/test/java/freemarker/core/FilterBiTest.java    | 31 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/src/main/java/freemarker/core/BuiltInsForSequences.java b/src/main/java/freemarker/core/BuiltInsForSequences.java
index d95ad50..b0cc82e 100644
--- a/src/main/java/freemarker/core/BuiltInsForSequences.java
+++ b/src/main/java/freemarker/core/BuiltInsForSequences.java
@@ -1085,8 +1085,9 @@ class BuiltInsForSequences {
                     targetIterator = isLazilyGeneratedResultEnabled()
                             ? new LazyCollectionTemplateModelIterator((TemplateCollectionModel)
targetValue)
                             : ((TemplateCollectionModel) targetValue).iterator();
-                    targetIsSequence = targetValue instanceof LazilyGeneratedCollectionModel
?
-                        ((LazilyGeneratedCollectionModel) targetValue).isSequence() : false;
+                    targetIsSequence = targetValue instanceof LazilyGeneratedCollectionModel
+                            ? ((LazilyGeneratedCollectionModel) targetValue).isSequence()
+                            : targetValue instanceof TemplateSequenceModel;
                 } else if (targetValue instanceof TemplateSequenceModel) {
                     targetIterator = new LazySequenceIterator((TemplateSequenceModel) targetValue);
                     targetIsSequence = true;
diff --git a/src/test/java/freemarker/core/FilterBiTest.java b/src/test/java/freemarker/core/FilterBiTest.java
index ee16dff..a2d0649 100644
--- a/src/test/java/freemarker/core/FilterBiTest.java
+++ b/src/test/java/freemarker/core/FilterBiTest.java
@@ -28,7 +28,13 @@ import com.google.common.collect.ImmutableSet;
 
 import freemarker.template.Configuration;
 import freemarker.template.DefaultObjectWrapper;
+import freemarker.template.SimpleScalar;
+import freemarker.template.TemplateCollectionModel;
 import freemarker.template.TemplateException;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+import freemarker.template.TemplateModelIterator;
+import freemarker.template.TemplateSequenceModel;
 import freemarker.test.TemplateTest;
 
 public class FilterBiTest extends TemplateTest {
@@ -145,6 +151,13 @@ public class FilterBiTest extends TemplateTest {
     }
 
     @Test
+    public void testSequenceAndCollectionTarget() throws Exception {
+        addToDataModel("xs", new SequenceAndCollection());
+        assertOutput("${xs?filter(x -> x != 'a')?join(', ')}", "b");
+        assertOutput("<#assign xs2 = xs?filter(x -> x != 'a')>${xs2?join(', ')}",
"b");
+    }
+
+    @Test
     public void testNonSequenceInput() throws Exception {
         addToDataModel("coll", ImmutableSet.of("a", "b", "c"));
         assertErrorContains("${coll?filter(it -> it != 'a')[0]}", "sequence", "evaluated
to a collection");
@@ -164,4 +177,22 @@ public class FilterBiTest extends TemplateTest {
         }
     }
 
+    public class SequenceAndCollection implements TemplateSequenceModel, TemplateCollectionModel
{
+        public TemplateModelIterator iterator() throws TemplateModelException {
+            return new SequenceIterator(this);
+        }
+
+        public TemplateModel get(int index) throws TemplateModelException {
+            switch (index) {
+                case 0: return new SimpleScalar("a");
+                case 1: return new SimpleScalar("b");
+                default: return null;
+            }
+        }
+
+        public int size() throws TemplateModelException {
+            return 2;
+        }
+    }
+
 }


Mime
View raw message