@SuppressWarnings(value: [unused, CodeNarc.UnusedMethodParameter, CodeNarc.DuplicateStringLiteral]) @CompileStatic class ContractsMatch extends Object
Defines Hamcrest matcher based DBC (Design-by-Contracts) methods with enhanced messages containing textual representation of checked item and used Hamcrest matcher.
For example, the code fragmentrequireMatch("123".trim(), is(emptyString()))will result in
AssertionError
with the message
Require violation detected - matcher does not match - [item: 123.trim(), expected: is(emptyString()), actual: 123]Implementation uses Groovy macro methods to extract textual representation of tested item and used matcher, which is very convenient to have in
AssertionError
messages.
Constructor and description |
---|
ContractsMatch() |
Type Params | Return Type | Name and description |
---|---|---|
|
static Expression |
requireMatch(MacroContext macroContext, Expression itemExpression, Expression matcherExpression) Groovy macro method that implements DBC precondition check based on Hamcrest matchers. |
|
static Expression |
requireMatchWhenNotNull(MacroContext macroContext, Expression itemExpression, Expression matcherExpression) |
Groovy macro method that implements DBC precondition check based on Hamcrest matchers.
It adds convenient messages containing textual representation checked item and used matcher. At the implementation level, when this Groovy macro method is expanded and replaced, we end up with the method call to ContractsMatchBase.requireMatchBase.