You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the ==~ operator and passing a compiled Pattern instance with flags set, Spock replaces the object with a new Pattern object but uses only the regex string not the flags.
This is confusing because using the ==~ in plain groovy works
To Reproduce
import spock.lang.Specification
import java.util.regex.Pattern
class PatternBugSpec extends Specification {
def "Pattern.DOTALL is lost in direct assertion - BUG"() {
given:
def text = "foo\nBar\nfoo" // Multi-line string
def pattern = Pattern.compile(".*Bar.*", Pattern.DOTALL) // DOTALL to match across newlines
expect: "This FAILS because Spock loses the DOTALL flag"
text ==~ pattern
}
def "Workaround: assign to boolean first - WORKS"() {
given:
def text = "foo\nBar\nfoo"
def pattern = Pattern.compile(".*Bar.*", Pattern.DOTALL)
// This WORKS because Groovy handles the match, creating a boolean
def match = text ==~ pattern
expect: "This PASSES"
match
}
}
Activity
[-]Using ==~ with Pattern.compile looses flags[/-][+]Using ==~ with Pattern.compile loses flags[/+]Preserve Pattern flags when used in conditions (#2304)