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/05/02 18:25:42 UTC

[groovy] branch master updated: GROOVY-10612: Tweak plus operation for `Map` instance to align with Java

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 8802d4d9ab GROOVY-10612: Tweak plus operation for `Map` instance to align with Java
8802d4d9ab is described below

commit 8802d4d9aba7d9b13fda3ef8eb4a1f786b8351b2
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()
+        '''
+    }
+}