Added the byCommand matcher

with this change the users can pass their own custom methods for the given matcher

hopefully fixes #217
This commit is contained in:
Marcin Grzejszczak
2017-02-10 13:30:09 +01:00
parent 576330652f
commit 47102d722b
12 changed files with 194 additions and 108 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.cloud.contract.spec.internal
import groovy.transform.CompileStatic
/**
* Represents a property that will become an executable method in the
* generated tests
@@ -27,7 +26,7 @@ import groovy.transform.CompileStatic
@CompileStatic
class ExecutionProperty {
private static final String PLACEHOLDER_VALUE = '\\$it'
private static final String PLACEHOLDER_VALUE = '$it'
final String executionCommand
@@ -40,7 +39,7 @@ class ExecutionProperty {
* the code that represents that method execution
*/
String insertValue(String valueToInsert) {
return executionCommand.replaceAll(PLACEHOLDER_VALUE, valueToInsert)
return executionCommand.replace(PLACEHOLDER_VALUE, valueToInsert)
}
@Override

View File

@@ -38,10 +38,14 @@ enum MatchingType {
* Verification if the value for the given path matches the
* provided regex
*/
REGEX
REGEX,
/**
* The user can provide custom command to execute
*/
COMMAND
static boolean regexRelated(MatchingType type) {
if (type == EQUALITY || type == TYPE ) {
if (type == EQUALITY || type == TYPE || type == COMMAND ) {
return false
}
return true

View File

@@ -18,6 +18,10 @@ class ResponseBodyMatchers extends BodyMatchers {
return new MatchingTypeValue(type: MatchingType.TYPE)
}
MatchingTypeValue byCommand(String execute) {
return new MatchingTypeValue(MatchingType.COMMAND, new ExecutionProperty(execute))
}
MatchingTypeValue byType(@DelegatesTo(MatchingTypeValueHolder) Closure closure) {
MatchingTypeValueHolder matchingTypeValue = new MatchingTypeValueHolder()
closure.delegate = matchingTypeValue

View File

@@ -32,4 +32,16 @@ class ExecutionPropertySpec extends Specification {
'commandToExecute(someObject.itsValue)' == commandWithInsertedValue
}
def 'should insert passed value with a $ sign in place of $it placeholder'() {
given:
String commandToExecute = 'commandToExecute($it)'
ExecutionProperty executionProperty = new ExecutionProperty(commandToExecute)
and:
String valueToInsert = '$.someObject.itsValue'
when:
String commandWithInsertedValue = executionProperty.insertValue(valueToInsert)
then:
'commandToExecute($.someObject.itsValue)' == commandWithInsertedValue
}
}

View File

@@ -13,6 +13,7 @@ class MatchingTypeSpec extends Specification {
type | expected
MatchingType.EQUALITY | false
MatchingType.TYPE | false
MatchingType.COMMAND | false
MatchingType.REGEX | true
MatchingType.DATE | true
MatchingType.TIME | true