You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/02/02 23:12:29 UTC

[groovy] branch GROOVY_3_0_X updated (52e852f -> 23afb2e)

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

paulk pushed a change to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from 52e852f  GROOVY-9911: Tuples with same collection values do not match
     new 0c81c5f  GROOVY-9926: Groovy documentation does not describe how to instantiate empty maps in section where maps are first described
     new 23afb2e  GROOVY-9925: Groovy match operator documentation should contain real examples and slashy strings reminder

The 2 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.


Summary of changes:
 src/spec/doc/core-operators.adoc   | 22 ++++++++++++++++++++++
 src/spec/test/OperatorsTest.groovy | 21 +++++++++++++++++++++
 src/spec/test/SyntaxTest.groovy    |  3 +++
 3 files changed, 46 insertions(+)


[groovy] 02/02: GROOVY-9925: Groovy match operator documentation should contain real examples and slashy strings reminder

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

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

commit 23afb2edd3a1f05dcf293de21cdcf61a0042ff7b
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Feb 2 10:40:34 2021 +1000

    GROOVY-9925: Groovy match operator documentation should contain real examples and slashy strings reminder
---
 src/spec/doc/core-operators.adoc   | 22 ++++++++++++++++++++++
 src/spec/test/OperatorsTest.groovy | 21 +++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/src/spec/doc/core-operators.adoc b/src/spec/doc/core-operators.adoc
index a20eb00..24699c5 100644
--- a/src/spec/doc/core-operators.adoc
+++ b/src/spec/doc/core-operators.adoc
@@ -516,6 +516,11 @@ include::{projectdir}/src/spec/test/OperatorsTest.groovy[tags=pattern_op_variant
 <3> the dollar-slashy string lets you use slashes and the dollar sign without having to escape them
 <4> you can also use a GString!
 
+[NOTE]
+While you can use most String forms with the Pattern, Find and Match operators,
+we recommend using the slashy string most of the time to save having to
+remember the otherwise needed escaping requirements.
+
 === Find operator
 
 Alternatively to building a pattern, you can use the find operator `=~` to directly create a `java.util.regex.Matcher` instance:
@@ -545,6 +550,23 @@ include::{projectdir}/src/spec/test/OperatorsTest.groovy[tags=pattern_matcher_st
 <2> the return type of `==~` is therefore a `boolean`
 <3> equivalent to calling `if (text ==~ /match/)`
 
+=== Comparing Find vs Match operators
+
+Typically, the match operator is used when the pattern involves a single exact match, otherwise
+the find operator might be more useful.
+[source,groovy]
+----
+include::../test/OperatorsTest.groovy[tags=pattern_find_vs_matcher,indent=0]
+----
+<1> equivalent, but explicit ^ and $ are discouraged since they aren't needed
+<2> no match because of leading space
+<3> one match
+<4> ^ and $ indicate exact match required
+<5> zero matches
+<6> one match, greedily starting at first word
+<7> one match, ignores leading space
+<8> two matches
+
 == Other operators
 
 === Spread operator
diff --git a/src/spec/test/OperatorsTest.groovy b/src/spec/test/OperatorsTest.groovy
index a833afa..0393a47 100644
--- a/src/spec/test/OperatorsTest.groovy
+++ b/src/spec/test/OperatorsTest.groovy
@@ -373,6 +373,27 @@ assert user.@name == 'Bob'                   // <1>
             throw new RuntimeException("Should not reach that point!")
         }
         // end::pattern_matcher_strict_op[]
+
+        // tag::pattern_find_vs_matcher[]
+        assert 'two words' ==~ /\S+\s+\S+/
+        assert 'two words' ==~ /^\S+\s+\S+$/         // <1>
+        assert !(' leading space' ==~ /\S+\s+\S+/)   // <2>
+
+        def m1 = 'two words' =~ /^\S+\s+\S+$/
+        assert m1.size() == 1                          // <3>
+        def m2 = 'now three words' =~ /^\S+\s+\S+$/    // <4>
+        assert m2.size() == 0                          // <5>
+        def m3 = 'now three words' =~ /\S+\s+\S+/
+        assert m3.size() == 1                          // <6>
+        assert m3[0] == 'now three'
+        def m4 = ' leading space' =~ /\S+\s+\S+/
+        assert m4.size() == 1                          // <7>
+        assert m4[0] == 'leading space'
+        def m5 = 'and with four words' =~ /\S+\s+\S+/
+        assert m5.size() == 2                          // <8>
+        assert m5[0] == 'and with'
+        assert m5[1] == 'four words'
+        // end::pattern_find_vs_matcher[]
     }
 
     void testSpreadDotOperator() {


[groovy] 01/02: GROOVY-9926: Groovy documentation does not describe how to instantiate empty maps in section where maps are first described

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

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

commit 0c81c5f30bb5c9a210a476ffec83d6b2ac052449
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Feb 2 09:59:16 2021 +1000

    GROOVY-9926: Groovy documentation does not describe how to instantiate empty maps in section where maps are first described
---
 src/spec/test/SyntaxTest.groovy | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/spec/test/SyntaxTest.groovy b/src/spec/test/SyntaxTest.groovy
index 8f3d0b6..694d74c 100644
--- a/src/spec/test/SyntaxTest.groovy
+++ b/src/spec/test/SyntaxTest.groovy
@@ -767,6 +767,9 @@ class SyntaxTest extends CompilableTestSupport {
 
         // tag::unknown_key[]
         assert colors.unknown == null
+
+        def emptyMap = [:]
+        assert emptyMap.anyKey == null
         // end::unknown_key[]
 
         // tag::number_key[]