You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/04/29 19:58:52 UTC

[groovy] branch GROOVY-10612 created (now 6f52e8e98f)

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a change to branch GROOVY-10612
in repository https://gitbox.apache.org/repos/asf/groovy.git


      at 6f52e8e98f GROOVY-10612: Tweak plus operation for `Map` instance to align with Java

This branch includes the following new commits:

     new 6f52e8e98f GROOVY-10612: Tweak plus operation for `Map` instance to align with Java

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[groovy] 01/01: GROOVY-10612: Tweak plus operation for `Map` instance to align with Java

Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY-10612
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 6f52e8e98f1ab6ee829708ee34ed2e1405fbb7b5
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Apr 30 03:33:27 2022 +0800

    GROOVY-10612: Tweak plus operation for `Map` instance to align with Java
---
 .../groovy/runtime/DefaultGroovyMethods.java       | 33 +++++++++++++++
 src/test/groovy/bugs/Groovy10612.groovy            | 47 ++++++++++++++++++++++
 2 files changed, 80 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index baa6e62639..c92169e541 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -24,6 +24,7 @@ import groovy.lang.DelegatesTo;
 import groovy.lang.DelegatingMetaClass;
 import groovy.lang.EmptyRange;
 import groovy.lang.ExpandoMetaClass;
+import groovy.lang.GString;
 import groovy.lang.GroovyObject;
 import groovy.lang.GroovyRuntimeException;
 import groovy.lang.GroovySystem;
@@ -15668,6 +15669,38 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         return plus(Integer.valueOf(left), right);
     }
 
+    /**
+     * Appends a String to the literal of the Map instance.
+     *
+     * <pre class="groovyTestCase">
+     * assert '[a:1] is a map' == [a:1] + ' is a map'
+     * </pre>
+     *
+     * @param left  a Map
+     * @param right a String
+     * @return the concatenated string
+     * @since 4.0.3
+     */
+    public static String plus(Map left, String right) {
+        return DefaultGroovyMethods.toString(left) + right;
+    }
+
+    /**
+     * Appends a GString to the literal of the Map instance.
+     *
+     * <pre class="groovyTestCase">
+     * assert '[a:1] is a map' == [a:1] + " is ${'a'} map"
+     * </pre>
+     *
+     * @param left  a Map
+     * @param right a GString
+     * @return the concatenated string
+     * @since 4.0.3
+     */
+    public static String plus(Map left, GString right) {
+        return DefaultGroovyMethods.toString(left) + right;
+    }
+
     /**
      * Compare a Character and a Number. The ordinal value of the Character
      * is used in the comparison (the ordinal value is the unicode
diff --git a/src/test/groovy/bugs/Groovy10612.groovy b/src/test/groovy/bugs/Groovy10612.groovy
new file mode 100644
index 0000000000..65c4b0908a
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10612.groovy
@@ -0,0 +1,47 @@
+/*
+ *  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 bugs
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+@CompileStatic
+class Groovy10612 {
+    @Test
+    void testStringConcatenationWithPlus() {
+        assertScript '''
+            assert '[a:1] is a map' == [a:1] + ' is a map'
+            assert '[a:1] is a map' == [a:1] + " is ${'a'} map"
+        '''
+    }
+
+    @Test
+    void testStringConcatenationWithPlus_CS() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            def m() {
+                assert '[a:1] is a map' == [a:1] + ' is a map'
+                assert '[a:1] is a map' == [a:1] + " is ${'a'} map"
+            }
+            m()
+        '''
+    }
+}