From f96f1ebae057f7308c032eba30de4f9fce05a8b8 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Sat, 9 May 2015 16:33:05 +1000 Subject: [PATCH] #80 update the readmes for array like matching with min/max --- pact-jvm-consumer-groovy/README.md | 25 +++++++++++++++++++++++++ pact-jvm-consumer-junit/README.md | 28 ++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/pact-jvm-consumer-groovy/README.md b/pact-jvm-consumer-groovy/README.md index d0538dd206..8e32f53966 100644 --- a/pact-jvm-consumer-groovy/README.md +++ b/pact-jvm-consumer-groovy/README.md @@ -250,6 +250,31 @@ Defines a matcher that accepts ISO and SMTP timestamps. If the value is not prov Defines a matcher that accepts UUIDs. A random one will be generated if no value is provided. +### Ensuring all items in a list match an example (2.2.0+) + +Lots of the time you might not know the number of items that will be in a list, but you want to ensure that the list +has a minimum or maximum size and that each item in the list matches a given example. You can do this with the `eachLike`, +`minLike` and `maxLike` functions. + +| function | description | +|----------|-------------| +| `eachLike()` | Ensure that each item in the list matches the provided example | +| `maxLike(integer max)` | Ensure that each item in the list matches the provided example and the list is no bigger than the provided max | +| `minLike(integer min)` | Ensure that each item in the list matches the provided example and the list is no smaller than the provided min | + +For example: + +```groovy + withBody { + users minLike(1) { + id identifier + name string('Fred') + } + } +``` + +This will ensure that the user list is never empty and that each user has an identifier that is a number and a name that is a string. + ## Changing the directory pact files are written to (2.1.9+) By default, pact files are written to `target/pacts`, but this can be overwritten with the `pact.rootDir` system property. diff --git a/pact-jvm-consumer-junit/README.md b/pact-jvm-consumer-junit/README.md index 9e862714ff..14ff9fafa6 100644 --- a/pact-jvm-consumer-junit/README.md +++ b/pact-jvm-consumer-junit/README.md @@ -228,6 +228,32 @@ PactDslJsonBody body = new PactDslJsonBody() .timestamp(); ``` +#### Ensuring all items in a list match an example (2.2.0+) + +Lots of the time you might not know the number of items that will be in a list, but you want to ensure that the list +has a minimum or maximum size and that each item in the list matches a given example. You can do this with the `arrayLike`, +`minArrayLike` and `maxArrayLike` functions. + +| function | description | +|----------|-------------| +| `arrayLike` | Ensure that each item in the list matches the provided example | +| `maxArrayLike` | Ensure that each item in the list matches the provided example and the list is no bigger than the provided max | +| `minArrayLike` | Ensure that each item in the list matches the provided example and the list is no smaller than the provided min | + +For example: + +```java + DslPart body = new PactDslJsonBody() + .minArrayLike("users") + .id() + .stringType("name") + .closeObject() + .closeArray(); +``` + +This will ensure that the users list is never empty and that each user has an identifier that is a number and a name that is a string. + + ### Matching on paths (version 2.1.5+) You can use regular expressions to match incoming requests. The DSL has a `matchPath` method for this. You can provide @@ -247,8 +273,6 @@ For example: .body("{\"hello\": \"harry\"}") ``` -## - ## Debugging pact failures When the test runs, Pact will start a mock provider that will listen for requests and match them against the expectations