Git Product home page Git Product logo

hamcrest-compose's Introduction

Hamcrest Compose

This library provides Hamcrest matchers to easily build composite matchers for objects. For example:

public static Matcher<Person> personEqualTo(Person expected) {
	return compose("a person with", hasFeature("title", Person::getTitle, equalTo(expected.getTitle())))
		.and(hasFeature("first name", Person::getFirstName, equalTo(expected.getFirstName())))
		.and(hasFeature("last name", Person::getLastName, equalTo(expected.getLastName())));
}

See the demo module to run this example. The PersonMatchersTest test case defines the behaviour of this matcher.

Getting started

Hamcrest Compose is available in the Maven Central repository. Start by adding a dependency to your Maven project:

<dependency>
	<groupId>org.hobsoft.hamcrest</groupId>
	<artifactId>hamcrest-compose</artifactId>
	<version>0.1.0</version>
	<scope>test</scope>
</dependency>

Usage

Hamcrest Compose provides two matchers:

ComposeMatchers.compose

This factory method builds a composite matcher that logically ANDs a number of other matchers. For example:

assertThat("ham", compose(startsWith("h")).and(containsString("a")).and(endsWith("m")));

This differs from Hamcrest's composite matchers allOf and both in the following ways:

  • It does not short circuit. This means that all mismatches are reported, not just the first one.
  • It does not describe itself using parenthesis. This produces more readable descriptions.
  • It supports an optional description to help describe the composition
  • It does not repeat the matcher's description when describing a mismatch

ComposeMatchers.hasFeature

This factory method builds a matcher that matches a 'feature' of an object. A feature is any value that can be obtained from the object by a Function. Typically this is a lambda such as a method reference, for example:

assertThat(person, hasFeature(Person::getFirstName, equalTo("ham")));

By default this matcher will describe itself and any mismatches by using the toString method of the feature function. When using lambdas this is not particularly informative so a feature description can be specified:

assertThat(person, hasFeature("a person with first name", Person::getFirstName, equalTo("ham")));

This feature description is also used to describe any mismatches. To specify a feature name for the mismatch only:

assertThat(person, hasFeature("a person with first name", "first name", Person::getFirstName, equalTo("ham")));

Links

License

Build Status

hamcrest-compose's People

Contributors

gilesp avatar markhobson avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.