Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

MatchTuples Matcher

Eric Arellano edited this page Jul 6, 2017 · 3 revisions

MatchTuples lets you build a matcher, querying tupled output on value basis:

import static org.hamcrest.Matchers.*;
//...
OutputMatcher<Tuple2<String, Integer>> matcher =
				new MatchTuples<Tuple2<String, Integer>>("name", "age")
						.assertThat("name", isA(String.class))
						.assertThat("age", lessThan(45))
						.assertThat("age", greaterThan(30))
						.anyOfThem().onEachRecord();

Mapping tuples

The first step of working with MatchTuples is to name the values in the Tuple:

new MatchTuples<Tuple5<String,String,String,Integer,Long>("id",
                                                          "name",
                                                          "type",
                                                          "count",
                                                          "timestamp");

Now each value in the tuple is accessible by a String key. If you wan`t to reuse a mapping you can define it separately:

TupleMask<Tuple3<String,String,Integer>> mask = 
		TupleMask.create("name", "postcode", "age");

MatchTuples.fromMask(mask);

It's possible to map the tuple only partially, if you don't want to check all values. Declare a mapping, shorter than the actual arity of the tuple, or disregard fields by using ignore or null:

new MatchTuples<Tuple5<String,String,String,Integer,Long>("id", "name");
new MatchTuples<Tuple5<String,String,String,Integer,Long>(ignore, 
                                                          "name",
                                                          ignore, 
                                                          "count");

Asserting values

After mapping the Tuple you can add assertions to the keys:

new MatchTuples<Tuple2<String, Integer>>("name", "age")
		.assertThat("name", isA(String.class))
		.assertThat("age", lessThan(45))
		.assertThat("age", greaterThan(30))
  • .assertThat(key,matcher) takes a String key and a hamcrest matcher.

You you can utilize the whole library of hamcrest matchers and it's extensions.

Using quantifiers

The last step is specify how many matchers have to be positive for a record to pass, and how many records have to pass to make the test successful:

new MatchTuples<Tuple2<String, Integer>>("name", "age")
		.assertThat("name", isA(String.class))
		.assertThat("age", lessThan(45))
		.anyOfThem().onEachRecord();

.anyOfThem() states that at least one of the asserts have to be positive to declare a record verified. .onEachRecord() enforces each record produced by the output to be valid.

If no statement is made regarding the quantity of matching asserts, each specified assert has to match in order to make a record valid.

new MatchTuples<Tuple2<String, Integer>>("name", "age")
		.assertThat("name", isA(String.class))
		.assertThat("age", lessThan(45))
		.onEachRecord();

List of quantifiers

A variety of statements are available to define your expectations.

Defining how many assertions have to be true:

  • .anyOfThem() at least one assertion.
  • .oneOfThem() exactly one of assertion.
  • .exactlyNOfThem(number) exactly a number of assertions.
  • .atLeastNOfThem(number) at least a number of assertions.
  • .atMostNOfThem(number) at most a number of assertions.

Note: Each of them is applied if none of these methods have been defined.
A .noneOfThem() statement is not implemented because of it's ambiguity. If you wan't to this logic, negate all your assertions with the Not matcher from hamcrest: .assertThat("age", not(lessThan(45))).

Defining how many records have to match:

  • .onEachRecord() all records.
  • .onAnyRecord() at least one record.
  • .onOneRecord() exactly one record.
  • .noNoRecord() no record is allowed to match.
  • .onExactlyNRecords(number) exactly a number of records.
  • .onAtLeastNRecords(number) at least a number of records.
  • .onAtMostNRecords(number) at most a number of records.