-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Trim regex anchors before generating random strings from the regex
- Loading branch information
1 parent
0554c1a
commit ec066a0
Showing
19 changed files
with
132 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
consumer/groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/RegexpMatcherSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package au.com.dius.pact.consumer.groovy | ||
|
||
import spock.lang.Issue | ||
import spock.lang.Specification | ||
|
||
class RegexpMatcherSpec extends Specification { | ||
def 'returns the value provided to the constructor'() { | ||
expect: | ||
new RegexpMatcher('\\w+', 'word').value == 'word' | ||
} | ||
|
||
def 'if no value is provided to the constructor, generates a random value when needed'() { | ||
expect: | ||
new RegexpMatcher('\\w+', null).value ==~ /\w+/ | ||
} | ||
|
||
@Issue('#1826') | ||
def 'handles regex anchors'() { | ||
expect: | ||
new RegexpMatcher('^\\w+$', null).value ==~ /\w+/ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core/model/src/test/groovy/au/com/dius/pact/core/model/generators/RegexGeneratorSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package au.com.dius.pact.core.model.generators | ||
|
||
import spock.lang.Issue | ||
import spock.lang.Specification | ||
|
||
class RegexGeneratorSpec extends Specification { | ||
def 'generates a random value when needed'() { | ||
expect: | ||
new RegexGenerator('\\w+').generate([:], '') ==~ /\w+/ | ||
} | ||
|
||
@Issue('#1826') | ||
def 'handles regex anchors'() { | ||
expect: | ||
new RegexGenerator('^\\w+$').generate([:], '') ==~ /\w+/ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
core/support/src/main/kotlin/au/com/dius/pact/core/support/Random.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package au.com.dius.pact.core.support | ||
|
||
import com.mifmif.common.regex.Generex | ||
|
||
/** | ||
* Support for the generator of random values | ||
*/ | ||
object Random { | ||
/** | ||
* Generate a random string from a regular expression | ||
*/ | ||
@JvmStatic | ||
fun generateRandomString(regex: String): String { | ||
return if (regex.endsWith('$') && !regex.endsWith("\\$")) { | ||
Generex(regex.trimStart('^').trimEnd('$')).random() | ||
} else { | ||
Generex(regex.trimStart('^')).random() | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
core/support/src/test/groovy/au/com/dius/pact/core/support/RandomSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package au.com.dius.pact.core.support | ||
|
||
import spock.lang.Issue | ||
import spock.lang.Specification | ||
|
||
class RandomSpec extends Specification { | ||
def 'generates a random value from the regular expression'() { | ||
expect: | ||
Random.generateRandomString('\\w+') ==~ /\w+/ | ||
} | ||
|
||
@Issue('#1826') | ||
def 'handles regex anchors'() { | ||
expect: | ||
Random.generateRandomString('^\\w+$') ==~ /\w+/ | ||
} | ||
|
||
def 'does not remove escaped values'() { | ||
expect: | ||
Random.generateRandomString('\\^\\w+\\$') ==~ /\^\w+\$/ | ||
} | ||
} |