Skip to content

Commit ab84e91

Browse files
Vampireleonard84
andauthored
Fix nested regex finding in conditions (#1931)
Fixes #1930 --------- Co-authored-by: Leonard Brünings <leonard.bruenings@gradle.com>
1 parent dd5d83f commit ab84e91

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

spock-core/src/main/java/org/spockframework/runtime/SpockRuntime.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static void verifyMethodCondition(ErrorCollector errorCollector, @Nullabl
133133

134134
public static final String MATCH_COLLECTIONS_AS_SET = "matchCollectionsAsSet";
135135

136-
public static boolean matchCollectionsAsSet(Object left, Object right) {
136+
public static Object matchCollectionsAsSet(Object left, Object right) {
137137
if (isIterableOrArray(left) && isIterableOrArray(right)) {
138138
Set<?> actual = GroovyRuntimeUtil.coerce(left, LinkedHashSet.class);
139139
Set<?> expected = GroovyRuntimeUtil.coerce(right, LinkedHashSet.class);
@@ -142,8 +142,7 @@ public static boolean matchCollectionsAsSet(Object left, Object right) {
142142
return (left == null) && (right == null);
143143
} else {
144144
Pattern pattern = Pattern.compile(String.valueOf(right));
145-
java.util.regex.Matcher matcher = pattern.matcher(String.valueOf(left));
146-
return matcher.find();
145+
return pattern.matcher(String.valueOf(left));
147146
}
148147
}
149148

spock-specs/src/test/groovy/org/spockframework/smoke/condition/CollectionConditionRendering.groovy

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* https://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
*/
15+
116
package org.spockframework.smoke.condition
217

18+
import spock.lang.Issue
19+
320
class CollectionConditionRendering extends ConditionRenderingSpec {
421
def "nested lenient matching"() {
522
expect:
@@ -17,6 +34,44 @@ false
1734
}
1835
}
1936

37+
@Issue("https://github.com/spockframework/spock/issues/1930")
38+
def "nested regex finding"() {
39+
expect:
40+
isRendered """
41+
(x =~ y).count == 0
42+
| | | | |
43+
| | . 3 false
44+
| java.util.regex.Matcher[pattern=. region=0,3 lastmatch=]
45+
foo
46+
""", {
47+
def x = 'foo'
48+
def y = /./
49+
assert (x =~ y).count == 0
50+
}
51+
}
52+
53+
@Issue("https://github.com/spockframework/spock/issues/1930")
54+
def "nested regex complex finding"() {
55+
expect:
56+
isRendered """
57+
(output =~ /on (executor-\\d+)/).collect { it[1] }.unique().size() == 3
58+
| | | | | |
59+
| | | | 2 false
60+
| | | [executor-1, executor-2]
61+
| | [executor-1, executor-2]
62+
| java.util.regex.Matcher[pattern=on (executor-\\d+) region=0,54 lastmatch=]
63+
Foo on executor-1
64+
Bar on executor-2
65+
Baz on executor-1""", {
66+
def output = '''\
67+
Foo on executor-1
68+
Bar on executor-2
69+
Baz on executor-1
70+
'''
71+
assert (output =~ /on (executor-\d+)/).collect { it[1] }.unique().size() == 3
72+
}
73+
}
74+
2075
def "rendering of lenient matching with variables"() {
2176
expect:
2277
isRendered """
@@ -114,6 +169,21 @@ false
114169
}
115170
}
116171

172+
def "nested regex matching"() {
173+
expect:
174+
isRendered """
175+
!(x ==~ y)
176+
| | | |
177+
| a | .
178+
| true
179+
false
180+
""", {
181+
def x = 'a'
182+
def y = /./
183+
assert !(x ==~ y)
184+
}
185+
}
186+
117187
def "indirect regex find works with different representations"() {
118188
expect:
119189
isRendered """
@@ -134,7 +204,7 @@ x =~ y
134204
isRendered """
135205
x =~ /\\d/
136206
| |
137-
| false
207+
| java.util.regex.Matcher[pattern=\\d region=0,3 lastmatch=]
138208
[a]
139209
""", {
140210
def x = "[a]"

0 commit comments

Comments
 (0)