An open API service indexing awesome lists of open source software.

https://github.com/agrison/vavr-matchers

Hamcrest matchers for Vavr
https://github.com/agrison/vavr-matchers

hamcrest hamcrest-extensions hamcrest-matchers java vavr

Last synced: 12 days ago
JSON representation

Hamcrest matchers for Vavr

Awesome Lists containing this project

README

          

# vavr-matchers

![vavr-matchers](https://github.com/agrison/vavr-matchers/workflows/build/badge.svg)
[![codecov](https://codecov.io/gh/agrison/vavr-matchers/branch/master/graph/badge.svg?token=lkICxLZmij)](https://codecov.io/gh/agrison/vavr-matchers)

This library contains [Vavr](https://github.com/vavr-io/vavr) matchers for [hamcrest](https://github.com/hamcrest/JavaHamcrest).

## Install

```xml

me.grison
vavr-matchers
1.3

```

## Usage

```java
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 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 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.

## Matchers

### Option

| 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 |

### Try

| 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` |

### Either

| 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` |

### Traversable

| 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 |

### Set

| 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 |

### Map

| 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 |

### Future

| 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` |

### Lazy

| 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` |

### Tuple

| 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` |

### Validation

| 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` |

## Contribute

It is a work in progress, so don't hesitate to contribute and add more matchers.

## Thanks

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.

## Self-promotion

This library is also covered in my book [Practical Vavr](https://leanpub.com/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.