This library contains Vavr matchers for hamcrest.
<dependency>
<groupId>me.grison</groupId>
<artifactId>vavr-matchers</artifactId>
<version>1.3</version>
</dependency>
import static me.grison.vavr.matchers.VavrMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
class AllTests {
@Test
public void testTry() {
Try<Integer> age = Try.of(() -> 30);
// ensure the Try is a success and its value is less than 40
assertThat(age, isSuccess(lessThan(40)));
}
@Test
public void testTraversable() {
List<Integer> ages = List.of(28, 35, 36, 40);
// ensure not empty
assertThat(ages, not(isEmpty()));
// ensure length is 4
assertThat(ages, hasLength(4));
// ensure it contains 35
assertThat(ages, contains(35));
// ensure it contains at least a value less than 30
assertThat(ages, contains(lessThan(30)));
// ensure that all values are less than 50
assertThat(ages, allMatch(lessThan(50)));
}
}
See below for all available matchers.
Assertion | Description |
---|---|
isDefined() | Verifies that an Option is defined |
isDefined(Matcher) | Verifies that an Option is defined and its content matches a Matcher |
isEmpty() | Verifies that an Option is undefined |
Assertion | Description |
---|---|
isSuccess() | Verifies that a Try is a Success |
isSuccess(Matcher) | Verifies that a Try is a Success and its content matches a Matcher |
isFailure() | Verifies that a Try is a Failure |
isFailure(Class<E extends Throwable>) | Verifies that a Try is a Failure and its cause is a specific Throwable |
Assertion | Description |
---|---|
isRight() | Verifies that an Either is a Right |
isRight(Matcher) | Verifies that an Either is a Right and its content matches a Matcher |
isLeft() | Verifies that an Either is a Left |
isLeft(Matcher) | Verifies that an Either is a Left and its content matches a Matcher |
Assertion | Description |
---|---|
isEmpty() | Verifies that a Traversable is empty |
hasLength(int) | Verifies that a Traversable has a specific length |
hasLength(Matcher) | Verifies that a Traversable has a length matching a Matcher |
contains(T) | Verifies that a Traversable contain a specific element |
contains(Matcher) | Verifies that a Traversable contain a specific element matching a Matcher |
containsSubList(T...) | Verifies that a Traversable contain a specific sublist (with no other elements in between) |
containsSubList(Traversable) | Verifies that a Traversable contain a specific sublist (with no other elements in between) |
containsInAnyOrder(T...) | Verifies that a Traversable contain the given elements |
containsInAnyOrder(Traversable) | Verifies that a Traversable contain the given elements |
containsInOrder(T...) | Verifies that a Traversable contain the given elements in order (with possibly other elements in between) |
containsInOrder(Traversable) | Verifies that a Traversable contain the given elements in order (with possibly other elements in between) |
allMatch(Matcher) | Verifies that a Traversable contain only elements matching a Matcher |
isSorted() | Verifies that a Traversable is sorted |
isReverseSorted() | Verifies that a Traversable is reverse sorted |
startsWith(T...) | Verifies that a Traversable starts with the given elements |
startsWith(Traversable) | Verifies that a Traversable starts with the given elements |
endsWith(T...) | Verifies that a Traversable ends with the given elements |
endsWith(Traversable) | Verifies that a Traversable ends with the given elements |
isUnique() | Verifies that a Traversable contains no duplicates |
Assertion | Description |
---|---|
containsSubSet(T...) | Verifies that a Set contains at least the given elements |
containsSubSet(Traversable) | Verifies that a Set contains at least the given elements |
isSubSetOf(T...) | Verifies that a Set is a subset of the given elements |
isSubSetOf(Traversable) | Verifies that a Set is a subset of the given elements |
Assertion | Description |
---|---|
containsKeys(T...) | Verifies that a Map contains at least the given keys |
containsKeys(Traversable) | Verifies that a Map contains at least the given keys |
containsValues(T...) | Verifies that a Map contains at least the given values |
containsValues(Traversable) | Verifies that a Map contains at least the given values |
contains(T key, U value) | Verifies that a Map contains at least the given entry |
Assertion | Description |
---|---|
isCancelled() | Verifies that a Future is cancelled |
isCompleted() | Verifies that a Future is completed |
isCompleted(Matcher) | Verifies that a Future is completed and its content matches a Matcher |
Assertion | Description |
---|---|
isEvaluated() | Verifies that a Lazy has been evaluated |
isEvaluated(Matcher) | Verifies that a Lazy has been evaluated and its content matches a Matcher |
Assertion | Description |
---|---|
hasArity(int) | Verifies that a Tuple has a specific arity |
hasArity(Matcher) | Verifies that a Tuple has a specific arity matching a Matcher |
Assertion | Description |
---|---|
isValid() | Verifies that a Validation is valid |
isValid(Matcher) | Verifies that a Validation is valid and its content matches a Matcher |
isInvalid() | Verifies that a Validation is invalid |
isInvalid(Matcher) | Verifies that a Validation is invalid and its content matches a Matcher |
It is a work in progress, so don't hesitate to contribute and add more matchers.
I needed this library and discovered an existing one from Vincent Ambo (@tazjin): https://github.com/tazjin/vavr-matchers
Unfortunately it has been archived for years and couldn't fork it, so as I needed new functionalities here comes this library.
This library is also covered in my book Practical Vavr.
Practical Vavr is all about making you want to use Vavr in your day to day Java programming.
If you want to improve the quality of your code by using a well-thought and beautifully designed functional programming library for Java, then you need to adopt Vavr, and this book will help you put it in practice.