Extend matchers (#598)
* Added anyHttpsUrl DslProperty * Added anyPositiveInt DslProperty
This commit is contained in:
committed by
Marcin Grzejszczak
parent
f6a8f49795
commit
663e1929d8
@@ -44,6 +44,10 @@ abstract class PatternValueDslProperty<T extends DslProperty> {
|
||||
return createAndValidateProperty(RegexPatterns.NUMBER, this.random.nextInt())
|
||||
}
|
||||
|
||||
T anyPositiveInt() {
|
||||
return createAndValidateProperty(RegexPatterns.POSITIVE_INT, Math.abs(this.random.nextInt() + 1))
|
||||
}
|
||||
|
||||
T anyDouble() {
|
||||
return createAndValidateProperty(RegexPatterns.DOUBLE, this.random.nextInt(100) + this.random.nextDouble())
|
||||
}
|
||||
@@ -68,6 +72,10 @@ abstract class PatternValueDslProperty<T extends DslProperty> {
|
||||
return createAndValidateProperty(RegexPatterns.URL, "http://foo" + this.random.nextInt() + ".com")
|
||||
}
|
||||
|
||||
T anyHttpsUrl() {
|
||||
return createAndValidateProperty(RegexPatterns.HTTPS_URL, "https://baz" + this.random.nextInt() + ".com")
|
||||
}
|
||||
|
||||
T anyUuid() {
|
||||
return createAndValidateProperty(RegexPatterns.UUID, UUID.randomUUID().toString())
|
||||
}
|
||||
|
||||
@@ -36,11 +36,13 @@ class RegexPatterns {
|
||||
protected static final Pattern TRUE_OR_FALSE = Pattern.compile(/(true|false)/)
|
||||
protected static final Pattern ONLY_ALPHA_UNICODE = Pattern.compile(/[\p{L}]*/)
|
||||
protected static final Pattern NUMBER = Pattern.compile('-?(\\d*\\.\\d+|\\d+)')
|
||||
protected static final Pattern POSITIVE_INT = Pattern.compile('([1-9]\\d*)')
|
||||
protected static final Pattern DOUBLE = Pattern.compile('-?(\\d*\\.\\d+)')
|
||||
protected static final Pattern IP_ADDRESS = Pattern.compile('([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])')
|
||||
protected static final Pattern HOSTNAME_PATTERN = Pattern.compile('((http[s]?|ftp):/)/?([^:/\\s]+)(:[0-9]{1,5})?')
|
||||
protected static final Pattern EMAIL = Pattern.compile('[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}')
|
||||
protected static final Pattern URL = UrlHelper.URL
|
||||
protected static final Pattern HTTPS_URL = UrlHelper.HTTPS_URL
|
||||
protected static final Pattern UUID = Pattern.compile('[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}')
|
||||
protected static final Pattern ANY_DATE = Pattern.compile('(\\d\\d\\d\\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])')
|
||||
protected static final Pattern ANY_DATE_TIME = Pattern.compile('([0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])')
|
||||
@@ -61,6 +63,10 @@ class RegexPatterns {
|
||||
return NUMBER
|
||||
}
|
||||
|
||||
Pattern positiveInt() {
|
||||
return POSITIVE_INT
|
||||
}
|
||||
|
||||
Pattern anyBoolean() {
|
||||
return TRUE_OR_FALSE
|
||||
}
|
||||
@@ -85,6 +91,10 @@ class RegexPatterns {
|
||||
return URL
|
||||
}
|
||||
|
||||
Pattern httpsUrl() {
|
||||
return HTTPS_URL
|
||||
}
|
||||
|
||||
Pattern uuid(){
|
||||
return UUID
|
||||
}
|
||||
@@ -137,6 +147,8 @@ class UrlHelper {
|
||||
*/
|
||||
private static final String REGEX_SCHEME = "[A-Za-z][+-.\\w^_]*:"
|
||||
|
||||
private static final String HTTPS_REGEX_SCHEME = "https:"
|
||||
|
||||
// Example: "//".
|
||||
private static final String REGEX_AUTHORATIVE_DECLARATION = "/{2}"
|
||||
|
||||
@@ -162,6 +174,9 @@ class UrlHelper {
|
||||
//Example: "/user/heartrate?foo=bar#element1".
|
||||
private static final String REGEX_RESOURCE_PATH = "(?:/\\S*)?"
|
||||
|
||||
protected static final Pattern HTTPS_URL = Pattern.compile("^(?:" + HTTPS_REGEX_SCHEME + REGEX_AUTHORATIVE_DECLARATION +
|
||||
REGEX_USERINFO + REGEX_HOST + REGEX_PORT + REGEX_RESOURCE_PATH + ")\$")
|
||||
|
||||
protected static final Pattern URL = Pattern.compile("^(?:(?:" + REGEX_SCHEME + REGEX_AUTHORATIVE_DECLARATION + ")?" +
|
||||
REGEX_USERINFO + REGEX_HOST + REGEX_PORT + REGEX_RESOURCE_PATH + ")\$")
|
||||
}
|
||||
|
||||
@@ -126,6 +126,79 @@ class RegexPatternsSpec extends Specification {
|
||||
'http://.www.foo.bar./' || false
|
||||
}
|
||||
|
||||
def "should generate a regex for httpsUrl [#textToMatch] that is a match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == regexPatterns.httpsUrl().matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'ftp://asd.com:9090/asd/a?a=b' || false
|
||||
'https://foo.com/blah_blah/' || true
|
||||
'https://foo.com/blah_blah' || true
|
||||
'http://foo.com/blah_blah' || false
|
||||
'http://foo.com/blah_blah/' || false
|
||||
'https://foo.com/blah_blah_(wikipedia)' || true
|
||||
'https://foo.com/blah_blah_(wikipedia)_(again)' || true
|
||||
'https://www.example.com/wpstyle/?p=364' || true
|
||||
'https://www.example.com/foo/?bar=baz&inga=42&quux' || true
|
||||
'https://✪df.ws/123' || true
|
||||
'https://userid:password@example.com:8080' || true
|
||||
'https://userid:password@example.com:8080/' || true
|
||||
'https://userid@example.com' || true
|
||||
'https://userid@example.com/' || true
|
||||
'https://userid@example.com:8080' || true
|
||||
'https://userid@example.com:8080/' || true
|
||||
'https://userid:password@example.com' || true
|
||||
'https://userid:password@example.com/' || true
|
||||
'https://142.42.1.1/' || true
|
||||
'https://142.42.1.1:8080/' || true
|
||||
'https://⌘.ws' || true
|
||||
'https://⌘.ws/' || true
|
||||
'https://foo.com/blah_(wikipedia)#cite-1' || true
|
||||
'https://foo.com/blah_(wikipedia)_blah#cite-1' || true
|
||||
'https://foo.com/unicode_(✪)_in_parens' || true
|
||||
'https://foo.com/(something)?after=parens' || true
|
||||
'https://☺.damowmow.com/' || true
|
||||
'https://code.google.com/events/#&product=browser' || true
|
||||
'https://j.mp' || true
|
||||
'ftp://foo.bar/baz' || false
|
||||
'https://foo.bar/?q=Test%20URL-encoded%20stuff' || true
|
||||
'https://1337.net' || true
|
||||
'https://a.b-c.de' || true
|
||||
'https://223.255.255.254' || true
|
||||
'foo.com' || false
|
||||
'a.b.' || false
|
||||
'https://' || false
|
||||
'https://.' || false
|
||||
'https://..' || false
|
||||
'https://../' || false
|
||||
'https://?' || false
|
||||
'https://??' || false
|
||||
'https://??/' || false
|
||||
'https://#' || false
|
||||
'https://##' || false
|
||||
'https://##/' || false
|
||||
'https://foo.bar?q=Spaces should be encoded' || false
|
||||
'//' || false
|
||||
'//a' || false
|
||||
'///a' || false
|
||||
'///' || false
|
||||
'https:///a' || false
|
||||
'rdar://1234' || false
|
||||
'h://test' || false
|
||||
'https:// shouldfail.com' || false
|
||||
':// should fail' || false
|
||||
'https://foo.bar/foo(bar)baz quux' || false
|
||||
'https://-error-.invalid/' || false
|
||||
'https://-a.b.co' || false
|
||||
'https://a.b-.co' || false
|
||||
'https://1.1.1.1.1' || false
|
||||
'https://123.123.123' || false
|
||||
'https://3628126748' || false
|
||||
'https://.www.foo.bar/' || false
|
||||
'https://www.foo.bar./' || false
|
||||
'https://.www.foo.bar./' || false
|
||||
}
|
||||
|
||||
def "should generate a regex for a number [#textToMatch] that is a match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == regexPatterns.number().matcher(textToMatch).matches()
|
||||
@@ -138,6 +211,18 @@ class RegexPatternsSpec extends Specification {
|
||||
'1.' || false
|
||||
}
|
||||
|
||||
def "should generate a regex for a positive integer [#textToMatch] that is a match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == regexPatterns.positiveInt().matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'1' || true
|
||||
'12345' || true
|
||||
'-1' || false
|
||||
'0' || false
|
||||
'1.0' || false
|
||||
}
|
||||
|
||||
def "should generate a regex for a double [#textToMatch] that is a match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == regexPatterns.aDouble().matcher(textToMatch).matches()
|
||||
|
||||
Reference in New Issue
Block a user