freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject incubator-freemarker git commit: Renamed macro_caller_template_name to caller_template_name, as it actually works for #function as well. Updated and cleaned up related tests.
Date Sat, 10 Mar 2018 07:23:15 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/3 59829da65 -> f48c81368


Renamed macro_caller_template_name to caller_template_name, as it actually works for #function
as well. Updated and cleaned up related tests.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/f48c8136
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/f48c8136
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/f48c8136

Branch: refs/heads/3
Commit: f48c81368ff3e0c4a38b76c03303ea48c7a5840a
Parents: 59829da
Author: ddekany <ddekany@apache.org>
Authored: Sat Mar 10 08:22:51 2018 +0100
Committer: ddekany <ddekany@apache.org>
Committed: Sat Mar 10 08:22:51 2018 +0100

----------------------------------------------------------------------
 .../freemarker/core/CallerTemplateNameTest.java | 148 +++++++++++++++++++
 .../core/MacroCallerTemplateNameTest.java       | 142 ------------------
 .../freemarker/core/ASTExpBuiltInVariable.java  |  11 +-
 3 files changed, 154 insertions(+), 147 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/f48c8136/freemarker-core-test/src/test/java/org/apache/freemarker/core/CallerTemplateNameTest.java
----------------------------------------------------------------------
diff --git a/freemarker-core-test/src/test/java/org/apache/freemarker/core/CallerTemplateNameTest.java
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/CallerTemplateNameTest.java
new file mode 100644
index 0000000..73363ae
--- /dev/null
+++ b/freemarker-core-test/src/test/java/org/apache/freemarker/core/CallerTemplateNameTest.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.freemarker.core;
+
+import org.apache.freemarker.test.TemplateTest;
+import org.apache.freemarker.test.TestConfigurationBuilder;
+import org.junit.Test;
+
+public class CallerTemplateNameTest  extends TemplateTest {
+
+    @Override
+    protected Configuration createDefaultConfiguration() throws Exception {
+        return new TestConfigurationBuilder().localizedTemplateLookup(true).build();
+    }
+
+    @Test
+    public void testBaics() throws Exception {
+        addTemplate("main.ftl", ""
+                + "<#macro m>${.callerTemplateName}</#macro>"
+                + "<#function f()><#return .callerTemplateName></#function>"
+                + "<@m /> ${f()} [<#include 'other.ftl'>] <@m /> ${f()}");
+        addTemplate("other.ftl", ""
+                + "<@m /> ${f()} [<#include 'yet-another.ftl'>] <@m />
${f()}");
+        addTemplate("yet-another.ftl", ""
+                + "<@m /> ${f()}");
+        
+        assertOutputForNamed("main.ftl", ""
+                + "main.ftl main.ftl "
+                + "[other.ftl other.ftl "
+                + "[yet-another.ftl yet-another.ftl] "
+                + "other.ftl other.ftl] "
+                + "main.ftl main.ftl");
+    }
+    
+    @Test
+    public void testNoCaller() throws Exception {
+        assertErrorContains("${.callerTemplateName}", "no macro or function");
+
+        assertErrorContains(""
+                + "<#macro m><#nested></#macro>"
+                + "<@m>${.callerTemplateName}</@>",
+                "no macro or function");
+
+        addTemplate("main.ftl", "${.callerTemplateName}");
+        assertErrorContainsForNamed("main.ftl", "no macro or function");
+    }
+
+    @Test
+    public void testNamelessCaller() throws Exception {
+        assertOutput(""
+                + "<#macro m2>${.callerTemplateName}</#macro>"
+                + "[<@m2/>]",
+                "[]");
+    }
+
+    @Test
+    public void testNested() throws Exception {
+        addTemplate("main.ftl", ""
+                + "<#include 'lib1.ftl'>"
+                + "<#include 'lib2.ftl'>"
+                + "<@m1 />");
+        addTemplate("lib1.ftl", ""
+                + "<#macro m1>"
+                + "${.callerTemplateName} [<@m2>${.callerTemplateName}</@m2>]
${.callerTemplateName}"
+                + "</#macro>");
+        addTemplate("lib2.ftl", ""
+                + "<#macro m2>"
+                + "${.callerTemplateName} [<#nested>] ${.callerTemplateName}"
+                + "</#macro>");
+        assertOutputForNamed("main.ftl", ""
+                + "main.ftl [lib1.ftl [main.ftl] lib1.ftl] main.ftl");
+    }
+    
+    @Test
+    public void testSelfCaller() throws Exception {
+        addTemplate("main.ftl", ""
+                + "<#macro m>${.callerTemplateName}</#macro>"
+                + "<@m />");
+        assertOutputForNamed("main.ftl", "main.ftl");
+    }
+
+    @Test
+    public void testImportedTemplateCaller() throws Exception {
+        addTemplate("main.ftl", ""
+                + "<#import 'lib/foo.ftl' as foo>"
+                + "<@foo.m />, <@foo.m2 />");
+        addTemplate("lib/foo.ftl", ""
+                + "<#macro m>${.callerTemplateName}</#macro>"
+                + "<#macro m2><@m3/></#macro>"
+                + "<#macro m3>${.callerTemplateName}</#macro>");
+        assertOutputForNamed("main.ftl",
+                "main.ftl, lib/foo.ftl");
+    }
+    
+    @Test
+    public void testNestedIntoNonUserDirectives() throws Exception {
+        addTemplate("main.ftl", ""
+                + "<#macro m><#list 1..2 as _><#if true>${.callerTemplateName}</#if>;</#list></#macro>"
+                + "<@m/>");
+        assertOutputForNamed("main.ftl", "main.ftl;main.ftl;");
+    }
+
+    @Test
+    public void testUsedInArgument() throws Exception {
+        addTemplate("main.ftl", ""
+                + "<#include 'inc.ftl'>"
+                + "<#macro start>"
+                + "<@m .callerTemplateName />"
+                + "<@m2 />"
+                + "</#macro>"
+                + "<@start />");
+        addTemplate("inc.ftl", ""
+                + "<#macro m x{positional}, y{positional}=.callerTemplateName>"
+                + "x: ${x}; y: ${y}; caller: ${.callerTemplateName};"
+                + "</#macro>"
+                + "<#macro m2><@m .callerTemplateName /></#macro>");
+        
+        assertOutputForNamed("main.ftl", ""
+                + "x: main.ftl; y: main.ftl; caller: main.ftl;"
+                + "x: main.ftl; y: inc.ftl; caller: inc.ftl;");
+    }
+    
+    @Test
+    public void testReturnsLookupName() throws Exception {
+        addTemplate("main_en.ftl", ""
+                + "<#macro m>${.callerTemplateName}</#macro>"
+                + "<@m />");
+        assertOutputForNamed("main.ftl", "main.ftl"); // Not main_en.ftl
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/f48c8136/freemarker-core-test/src/test/java/org/apache/freemarker/core/MacroCallerTemplateNameTest.java
----------------------------------------------------------------------
diff --git a/freemarker-core-test/src/test/java/org/apache/freemarker/core/MacroCallerTemplateNameTest.java
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/MacroCallerTemplateNameTest.java
deleted file mode 100644
index d02a6dd..0000000
--- a/freemarker-core-test/src/test/java/org/apache/freemarker/core/MacroCallerTemplateNameTest.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.freemarker.core;
-
-import org.apache.freemarker.test.TemplateTest;
-import org.apache.freemarker.test.TestConfigurationBuilder;
-import org.junit.Test;
-
-public class MacroCallerTemplateNameTest  extends TemplateTest {
-
-    @Override
-    protected Configuration createDefaultConfiguration() throws Exception {
-        return new TestConfigurationBuilder().localizedTemplateLookup(true).build();
-    }
-
-    @Test
-    public void testNoCaller() throws Exception {
-        assertErrorContains("${.macroCallerTemplateName}", "no macro call");
-
-        assertErrorContains(""
-                + "<#macro m><#nested></#macro>"
-                + "<@m>${.macroCallerTemplateName}</@>",
-                "no macro call");
-
-        addTemplate("main.ftl", "${.macroCallerTemplateName}");
-        assertErrorContainsForNamed("main.ftl", "no macro call");
-    }
-
-    @Test
-    public void testNested() throws Exception {
-        assertOutput(""
-                + "<#macro m><#nested></#macro>"
-                + "<#macro m2><@m>${.macroCallerTemplateName}</@></#macro>"
-                + "[<@m2/>]",
-                "[]");
-        assertOutput(""
-                + "<#macro m2>${.macroCallerTemplateName}</#macro>"
-                + "[<@m2/>]",
-                "[]");
-    }
-    
-    @Test
-    public void testSameTemplateCaller() throws Exception {
-        addTemplate("main.ftl", ""
-                + "<#macro m>${.macroCallerTemplateName}</#macro>"
-                + "<@m />, <#attempt>${.macroCallerTemplateName}<#recover>-</#attempt>");
-        assertOutputForNamed("main.ftl", "main.ftl, -");
-    }
-
-    @Test
-    public void testIncludedTemplateCaller() throws Exception {
-        addTemplate("main.ftl", ""
-                + "<#include 'lib/foo.ftl'>"
-                + "<@m />, <@m2 />");
-        addTemplate("lib/foo.ftl", ""
-                + "<#macro m>${.macroCallerTemplateName}</#macro>"
-                + "<#macro m2><@m3/></#macro>"
-                + "<#macro m3>${.macroCallerTemplateName}</#macro>");
-        assertOutputForNamed("main.ftl",
-                "main.ftl, lib/foo.ftl");
-    }
-
-    @Test
-    public void testImportedTemplateCaller() throws Exception {
-        addTemplate("main.ftl", ""
-                + "<#import 'lib/foo.ftl' as foo>"
-                + "<@foo.m />, <@foo.m2 />");
-        addTemplate("lib/foo.ftl", ""
-                + "<#macro m>${.macroCallerTemplateName}</#macro>"
-                + "<#macro m2><@m3/></#macro>"
-                + "<#macro m3>${.macroCallerTemplateName}</#macro>");
-        assertOutputForNamed("main.ftl",
-                "main.ftl, lib/foo.ftl");
-    }
-    
-    @Test
-    public void testNestedIntoNonUserDirectives() throws Exception {
-        addTemplate("main.ftl", ""
-                + "<#macro m><#list 1..2 as _><#if true>${.macroCallerTemplateName}</#if>;</#list></#macro>"
-                + "<@m/>");
-        assertOutputForNamed("main.ftl", "main.ftl;main.ftl;");
-    }
-
-    @Test
-    public void testMulitpleLevels() throws Exception {
-        addTemplate("main.ftl", ""
-                + "<#include 'inc1.ftl'>"
-                + "<@m1 />");
-        addTemplate("inc1.ftl", ""
-                + "<#include 'inc2.ftl'>"
-                + "<#macro m1>m1: ${.macroCallerTemplateName}; <@m2 /></#macro>");
-        addTemplate("inc2.ftl", ""
-                + "<#macro m2>m2: ${.macroCallerTemplateName};</#macro>");
-        assertOutputForNamed("main.ftl", "m1: main.ftl; m2: inc1.ftl;");
-    }
-
-    @Test
-    public void testUsedInArgument() throws Exception {
-        addTemplate("main.ftl", ""
-                + "<#include 'inc.ftl'>"
-                + "<#macro start>"
-                + "<@m .macroCallerTemplateName />"
-                + "<@m2 />"
-                + "</#macro>"
-                + "<@start />");
-        addTemplate("inc.ftl", ""
-                + "<#macro m x{positional}, y{positional}=.macroCallerTemplateName>"
-                + "x: ${x}; y: ${y}; caller: ${.macroCallerTemplateName};"
-                + "</#macro>"
-                + "<#macro m2><@m .macroCallerTemplateName /></#macro>");
-        
-        assertOutputForNamed("main.ftl", ""
-                + "x: main.ftl; y: main.ftl; caller: main.ftl;"
-                + "x: main.ftl; y: inc.ftl; caller: inc.ftl;");
-    }
-    
-    @Test
-    public void testReturnsLookupName() throws Exception {
-        addTemplate("main_en.ftl", ""
-                + "<#macro m>${.macroCallerTemplateName}</#macro>"
-                + "<@m />");
-        assertOutputForNamed("main.ftl", "main.ftl"); // Not main_en.ftl
-    }
-    
-}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/f48c8136/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpBuiltInVariable.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpBuiltInVariable.java
b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpBuiltInVariable.java
index d55883d..08b9218 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpBuiltInVariable.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpBuiltInVariable.java
@@ -60,11 +60,12 @@ final class ASTExpBuiltInVariable extends ASTExpression {
     static final String URL_ESCAPING_CHARSET = "urlEscapingCharset";
     static final String NOW = "now";
     static final String GET_OPTIONAL_TEMPLATE = "getOptionalTemplate";
-    static final String MACRO_CALLER_TEMPLATE_NAME = "macroCallerTemplateName";    
+    static final String CALLER_TEMPLATE_NAME = "callerTemplateName";    
     
     static final Set<String> BUILT_IN_VARIABLE_NAMES = new _SortedArraySet<>(
-        // Must be sorted alphabetically!
+        // IMPORTANT! Keep this sorted alphabetically!
         AUTO_ESC,
+        CALLER_TEMPLATE_NAME,
         CURRENT_TEMPLATE_NAME,
         DATA_MODEL,
         ERROR,
@@ -75,7 +76,6 @@ final class ASTExpBuiltInVariable extends ASTExpression {
         LOCALE,
         LOCALE_OBJECT,
         LOCALS,
-        MACRO_CALLER_TEMPLATE_NAME,
         MAIN,
         MAIN_TEMPLATE_NAME,
         NAMESPACE,
@@ -203,12 +203,13 @@ final class ASTExpBuiltInVariable extends ASTExpression {
         if (name == GET_OPTIONAL_TEMPLATE) {
             return GetOptionalTemplateFunction.INSTANCE;
         }        
-        if (name == MACRO_CALLER_TEMPLATE_NAME) {
+        if (name == CALLER_TEMPLATE_NAME) {
             Context ctx = env.getCurrentMacroContext();
             if (ctx == null) {
                 // TODO [FM3] Adjust error message if the special variable syntax is not
`.someName` anymore.
                 throw new TemplateException(
-                        "Can't get ." + MACRO_CALLER_TEMPLATE_NAME + " here, as there's no
macro call in context.");
+                        "Can't get ." + CALLER_TEMPLATE_NAME + " here, as there's no macro
or function (that's "
+                        + "implemented in the template) call in context.");
             }
             String name = ctx.callPlace.getTemplate().getLookupName();
             return name != null ? new SimpleString(name) : TemplateStringModel.EMPTY_STRING;


Mime
View raw message