-
Notifications
You must be signed in to change notification settings - Fork 59
MatchTuples Matcher
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();
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");
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 aString
key and a hamcrest matcher.
You you can utilize the whole library of hamcrest matchers and it's extensions.
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();
A variety of statements are available to define your expectations.
-
.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 theNot
matcher from hamcrest:.assertThat("age", not(lessThan(45)))
.
-
.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.