Skip to content

Commit

Permalink
Fix nested regex finding in conditions (#1931)
Browse files Browse the repository at this point in the history
Fixes #1930

---------

Co-authored-by: Leonard Brünings <leonard.bruenings@gradle.com>
  • Loading branch information
Vampire and leonard84 authored Mar 21, 2024
1 parent dd5d83f commit ab84e91
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static void verifyMethodCondition(ErrorCollector errorCollector, @Nullabl

public static final String MATCH_COLLECTIONS_AS_SET = "matchCollectionsAsSet";

public static boolean matchCollectionsAsSet(Object left, Object right) {
public static Object matchCollectionsAsSet(Object left, Object right) {
if (isIterableOrArray(left) && isIterableOrArray(right)) {
Set<?> actual = GroovyRuntimeUtil.coerce(left, LinkedHashSet.class);
Set<?> expected = GroovyRuntimeUtil.coerce(right, LinkedHashSet.class);
Expand All @@ -142,8 +142,7 @@ public static boolean matchCollectionsAsSet(Object left, Object right) {
return (left == null) && (right == null);
} else {
Pattern pattern = Pattern.compile(String.valueOf(right));
java.util.regex.Matcher matcher = pattern.matcher(String.valueOf(left));
return matcher.find();
return pattern.matcher(String.valueOf(left));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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
* https://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.spockframework.smoke.condition

import spock.lang.Issue

class CollectionConditionRendering extends ConditionRenderingSpec {
def "nested lenient matching"() {
expect:
Expand All @@ -17,6 +34,44 @@ false
}
}

@Issue("https://github.com/spockframework/spock/issues/1930")
def "nested regex finding"() {
expect:
isRendered """
(x =~ y).count == 0
| | | | |
| | . 3 false
| java.util.regex.Matcher[pattern=. region=0,3 lastmatch=]
foo
""", {
def x = 'foo'
def y = /./
assert (x =~ y).count == 0
}
}

@Issue("https://github.com/spockframework/spock/issues/1930")
def "nested regex complex finding"() {
expect:
isRendered """
(output =~ /on (executor-\\d+)/).collect { it[1] }.unique().size() == 3
| | | | | |
| | | | 2 false
| | | [executor-1, executor-2]
| | [executor-1, executor-2]
| java.util.regex.Matcher[pattern=on (executor-\\d+) region=0,54 lastmatch=]
Foo on executor-1
Bar on executor-2
Baz on executor-1""", {
def output = '''\
Foo on executor-1
Bar on executor-2
Baz on executor-1
'''
assert (output =~ /on (executor-\d+)/).collect { it[1] }.unique().size() == 3
}
}

def "rendering of lenient matching with variables"() {
expect:
isRendered """
Expand Down Expand Up @@ -114,6 +169,21 @@ false
}
}

def "nested regex matching"() {
expect:
isRendered """
!(x ==~ y)
| | | |
| a | .
| true
false
""", {
def x = 'a'
def y = /./
assert !(x ==~ y)
}
}

def "indirect regex find works with different representations"() {
expect:
isRendered """
Expand All @@ -134,7 +204,7 @@ x =~ y
isRendered """
x =~ /\\d/
| |
| false
| java.util.regex.Matcher[pattern=\\d region=0,3 lastmatch=]
[a]
""", {
def x = "[a]"
Expand Down

0 comments on commit ab84e91

Please sign in to comment.