gh-245: Created new DslProperty creation methods (#247)
Added: - iso8601WithOffset which handles iso8601 date time with timezone date validation - nonBlankString which handles string that aren't "" and also just whitespace - nonEmptyString which handles "" strings - anyOf(String...) which handles enumerated style attributes for validation fixes #245 changed the name of iso8601DatetimeWithTimezone to anyIso8601WithOffset per code review suggestions
This commit is contained in:
committed by
Marcin Grzejszczak
parent
77f42a6f98
commit
ebf09dfda3
@@ -83,6 +83,23 @@ abstract class PatternValueDslProperty<T extends DslProperty> {
|
||||
return createAndValidateProperty(RegexPatterns.ANY_TIME, "12:2$d:3$d")
|
||||
}
|
||||
|
||||
T anyIso8601WithOffset() {
|
||||
int d = this.random.nextInt(8) + 1
|
||||
return createAndValidateProperty(RegexPatterns.ISO8601_WITH_OFFSET, "201$d-0$d-1${d}T12:23:34.123Z")
|
||||
}
|
||||
|
||||
T anyNonBlankString() {
|
||||
return createAndValidateProperty(RegexPatterns.NON_BLANK, randomString(20))
|
||||
}
|
||||
|
||||
T anyNonEmptyString() {
|
||||
return createAndValidateProperty(RegexPatterns.NON_EMPTY, randomString(20))
|
||||
}
|
||||
|
||||
T anyOf(String... values){
|
||||
return createAndValidateProperty(RegexPatterns.anyOf(values), values[0])
|
||||
}
|
||||
|
||||
private static String randomString(int length) {
|
||||
char[] characterSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray()
|
||||
Random random = new Random()
|
||||
|
||||
@@ -44,6 +44,13 @@ class RegexPatterns {
|
||||
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])')
|
||||
protected static final Pattern ANY_TIME = Pattern.compile('(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])')
|
||||
protected static final Pattern NON_EMPTY = Pattern.compile(/.+/)
|
||||
protected static final Pattern NON_BLANK = Pattern.compile(/.*(\S+|\R).*|!^\R*$/)
|
||||
protected static final Pattern ISO8601_WITH_OFFSET = 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])(\.\d{3})?(Z|[+-][01]\d:[0-5]\d)/)
|
||||
|
||||
protected static Pattern anyOf(String... values){
|
||||
return Pattern.compile(values.collect({"^$it\$"}).join("|"))
|
||||
}
|
||||
|
||||
String onlyAlphaUnicode() {
|
||||
return ONLY_ALPHA_UNICODE.pattern()
|
||||
@@ -89,6 +96,18 @@ class RegexPatterns {
|
||||
return ANY_TIME.pattern()
|
||||
}
|
||||
|
||||
String iso8601WithOffset() {
|
||||
return ISO8601_WITH_OFFSET.pattern()
|
||||
}
|
||||
|
||||
String nonEmpty() {
|
||||
return NON_EMPTY.pattern()
|
||||
}
|
||||
|
||||
String nonBlank() {
|
||||
return NON_BLANK.pattern()
|
||||
}
|
||||
|
||||
// end::regexps[]
|
||||
|
||||
static String multipartParam(Object name, Object value) {
|
||||
|
||||
@@ -146,4 +146,47 @@ class RegexPatternsSpec extends Specification {
|
||||
"23:60:45" || false
|
||||
"23:59:60" || false
|
||||
}
|
||||
|
||||
def "should generate a regex with iso8601DateTimeWithTimezone [#textToMatch] in YYYY-MM-DDTHH:mm:ss.SSSZZ format that should match [#shouldMatch]"(){
|
||||
expect:
|
||||
shouldMatch == Pattern.compile(regexPatterns.iso8601WithOffset()).matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'2014-03-01T12:23:45Z' || true
|
||||
'2014-03-01T12:23:45+01:00' || true
|
||||
'2014-03-01T12:23:45.123Z' || true
|
||||
'2014-03-01T12:23:45.123+01:00' || true
|
||||
'2014-03-01T12:23:45' || false
|
||||
'2014-03-01T12:23:45.123' || false
|
||||
}
|
||||
|
||||
def "should generate a regex for a non blank string [#textToMatch] that should match [#shouldMatch]"(){
|
||||
expect:
|
||||
shouldMatch == Pattern.compile(regexPatterns.nonBlank()).matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'Not Empty' || true
|
||||
'' || false
|
||||
' ' || false
|
||||
}
|
||||
|
||||
def "should generate a regex for a non empty string [#textToMatch] that should match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == Pattern.compile(regexPatterns.nonEmpty()).matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'Not Empty' || true
|
||||
'' || false
|
||||
' ' || true
|
||||
}
|
||||
|
||||
def "should generate a regex for an enumerated value [#textToMatch] that should match [#shouldMatch]"(){
|
||||
expect:
|
||||
shouldMatch == RegexPatterns.anyOf('foo', 'bar').matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'foo' || true
|
||||
'bar' || true
|
||||
'baz' || false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user