cactoos-matchers

Extension for cactoos providing object-oriented hamcrest matchers.

License

License

MIT
Categories

Categories

cactoos General Purpose Libraries Utility
GroupId

GroupId

org.llorllale
ArtifactId

ArtifactId

cactoos-matchers
Last Version

Last Version

0.25
Release Date

Release Date

Type

Type

jar
Description

Description

cactoos-matchers
Extension for cactoos providing object-oriented hamcrest matchers.
Project URL

Project URL

https://github.com/llorllale/cactoos-matchers
Project Organization

Project Organization

jcabi.com
Source Code Management

Source Code Management

https://github.com/llorllale/cactoos-matchers

Download cactoos-matchers

How to add to project

<!-- https://jarcasting.com/artifacts/org.llorllale/cactoos-matchers/ -->
<dependency>
    <groupId>org.llorllale</groupId>
    <artifactId>cactoos-matchers</artifactId>
    <version>0.25</version>
</dependency>
// https://jarcasting.com/artifacts/org.llorllale/cactoos-matchers/
implementation 'org.llorllale:cactoos-matchers:0.25'
// https://jarcasting.com/artifacts/org.llorllale/cactoos-matchers/
implementation ("org.llorllale:cactoos-matchers:0.25")
'org.llorllale:cactoos-matchers:jar:0.25'
<dependency org="org.llorllale" name="cactoos-matchers" rev="0.25">
  <artifact name="cactoos-matchers" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.llorllale', module='cactoos-matchers', version='0.25')
)
libraryDependencies += "org.llorllale" % "cactoos-matchers" % "0.25"
[org.llorllale/cactoos-matchers "0.25"]

Dependencies

compile (1)

Group / Artifact Type Version
org.hamcrest : hamcrest jar 2.2

test (3)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.6.2
org.junit.jupiter : junit-jupiter-engine jar 5.6.2
org.mockito : mockito-core jar 3.5.0

Project Modules

There are no modules declared in this project.

Managed by Zerocracy Donate via Zerocracy

EO principles respected here DevOps By Rultor.com

Build Status Javadoc PDD status Maven Central License

Test Coverage SonarQube

All Contributors

What it is

cactoos-matchers is an object-oriented wrapper around hamcrest's matchers.

Principles

Design principles behind cactoos-matchers.

How to use

This library depends on cactoos and hamcrest. Get the latest version here:

<dependency>
  <groupId>org.llorllale</groupId>
  <artifactId>cactoos-matchers</artifactId>
  <version>${version}</version>
</dependency>

Java version required: 1.8+.

cactoos-matchers versus Hamcrest + JUnit

cactoos-matchers Hamcrest (static method) Hamcrest (object) JUnit
Assertion MatcherAssert.assertThat - Assert.assertThat
Throws - - @expected + ExpectedException
EndsWith Matchers.endsWith StringEndsWith -
StartsWith Matcers.startsWith StringStartsWith -
TextIs Matchers.is IsEqual -
HasLines - - -
MatchesRegex - - -
TextHasString Matchers.stringContainsInOrder StringContains -
FuncApplies - - -
HasValues Matchers.containsInAnyOrder IsIterableContainingInAnyOrder -
HasValuesMatching Matchers.containsInAnyOrder IsIterableContainingInAnyOrder -
InputHasContent - - -
IsTrue - - Assert.assertTrue
Matches - - -
RunsInThreads - - -
ScalarHasValue - - -

How to use

Use our matchers inside your JUnit @Test case. We also provide Assertion<T> in which you can compose the behavior under test and the test case's matcher, and attempt to affirm it.

We provide matchers for several different domains:

Text

Examples:

@Test
public void textHasPrefix() {
  final String prefix = "Application startup";
  new Assertion<>(
    "must have the prefix",
    new TextOf(new File("some.log")),
    new StartsWith(prefix)
  ).affirm();
}  

@Test
public void csvLineHasCorrectFormat() throws Exception {
  final String fields = "^[^|]+|[^|]+|[^|]+$";
  new Assertion<>(
    "must match the expected pattern",
    new FirstOf<>(
      line -> true,
      new Split(
        new TextOf(new File("report.csv")),
        "\n"
      ),
      () -> new TextOf("")
    ).value(),
    new MatchesRegex(fields)
  ).affirm();
}

@Test
public void textIsBlank(){
  new Assertion<>(
    "must be blank",
    new UncheckedText(    
      new TextOf(
        new File("file.txt")
      )
    ).asString(),
    new IsBlank()
  ).affirm();
}

Concurrency

Examples:

RunsInThreads

/**
 * Want some assurance that your object is thread-safe?
 */
@Test
public void threadSafety() {
  new Assertion<>(
    "must be able to modify the map concurrently",
    map -> {
      boolean success = true;
      try {
        map.forEach(
          (key, value) -> {
            map.remove(key);
            final Random r = new Random();
            map.put(r.nextInt(), r.nextInt());
          }
        );
      } catch (ConcurrentModificationException ex) {
        success = false;
      }
      return success;
    },
    new RunsInThreads<>(new ConcurrentHashMap<>(), 100)
  ).affirm();
}

Matching errors

Examples:

@Test
public void throwIllegalArgumentExceptionIfLessThan10() throws Exception {
  final Func<Integer, Integer> test = input -> {
    if (input < 10) {
      throw new IllegalArgumentException();
    }
    return input * 10;
  };
  new Assertion<>(
    "must throw illegalargumentexception if input is less than 10",
    () -> test.apply(5),
    new Throws<>(IllegalArgumentException.class)
  ).affirm();
}

Meta-matching: a matcher to test your matchers

Examples:

Use Matches to test matchers themselves:

@Test
public void matchExactString() {
  new Assertion<>(
    "must match the exact text",
    new TextIs("abc"),                // matcher being tested
    new Matches<>(new TextOf("abc"))  // reference against which the matcher is tested
  ).affirm();
}

How to contribute?

Just fork the repo and send us a pull request.

Request to add yourself to the list of contributors below with the @all-contributors bot.

Make sure your branch builds without any warnings/issues:

mvn clean install -Pqulice

Note: Checkstyle is used as a static code analyze tool with checks list in GitHub precommits.

License (MIT)

Copyright (c) for portions of project cactoos-matchers are held by Yegor Bugayenko, 2017-2018, as part of project cactoos. All other copyright for project cactoos-matchers are held by George Aristy, 2018-2020.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributors โœจ


George Aristy

๐Ÿ’ป

Yegor Bugayenko

๐Ÿ’ป

Roman Proshin

๐Ÿ’ป

Yurii Dubinka

๐Ÿ’ป

andreoss

๐Ÿ’ป

Victor Noรซl

๐Ÿ’ป

Dominik

๐Ÿ’ป

Stefano Cristalli

๐Ÿ’ป

Fevzi Anifieiev

๐Ÿ’ป

Paulo Lobo

๐Ÿ’ป

Vedran Grgo Vatavuk

๐Ÿ’ป

Alexander

๐Ÿ’ป

vzurauskas

๐Ÿ’ป

jsoroka

๐Ÿ’ป
org.llorllale

jcabi.com

Small java components

Versions

Version
0.25
0.24
0.23
0.22
0.21
0.20
0.19
0.18
0.17
0.16
0.15
0.14
0.13
0.12
0.11
0.10
0.9.1
0.9
0.0.1