Skip to content

Commit

Permalink
Merge pull request #454 from viktor-klymenko/issue-453-add_should_not…
Browse files Browse the repository at this point in the history
…_match

Add ability to assert that matched element is not present in collection (#453)
  • Loading branch information
YamStranger authored Jun 15, 2016
2 parents 3e547d4 + 1010a8d commit 02ae8c1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public static <T> void shouldMatch(T bean, BeanMatcher... matchers) {
}
}

public static <T> void shouldNotMatch(List<T> items, BeanMatcher... matchers) {
if (matches(items, matchers)) {
throw new AssertionError("Found unneeded matching elements for " + join(matchers)
+ NEW_LINE
+"Elements where " + join(items));
}
}

private static String descriptionOf(Object bean) {

if (isAMap(bean)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,46 @@ public void should_check_field_uniqueness_when_not_unique() {
shouldMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent);
}

@Test
public void should_check_field_similarities() {
List<Person> persons = Arrays.asList(billoddie, tim, graeme, billoddie);

BeanMatcher containsTwoEntries = the_count(is(2));
BeanMatcher lastNamesAreDifferent = each("lastName").isDifferent();
BeanMatcher firstNameIsBill = the("firstName", is("Bill"));

shouldNotMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent);
}

@Test(expected = AssertionError.class)
public void should_check_field_similarities_when_all_unique() {
List<Person> persons = Arrays.asList(billoddie, tim, graeme, billkidd);

BeanMatcher containsTwoEntries = the_count(is(2));
BeanMatcher lastNamesAreDifferent = each("lastName").isDifferent();
BeanMatcher firstNameIsBill = the("firstName", is("Bill"));

shouldNotMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent);
}

@Test
public void should_check_element_is_not_present() {
List<Person> persons = Arrays.asList(billoddie, tim, graeme, billoddie);

BeanMatcher firstNameIsJohn = the("firstName", is("John"));

shouldNotMatch(persons, firstNameIsJohn);
}

@Test(expected = AssertionError.class)
public void should_check_element_is_not_present_with_failing_case() {
List<Person> persons = Arrays.asList(billoddie, tim, graeme, billkidd);

BeanMatcher firstNameIsBill = the("firstName", is("Bill"));

shouldNotMatch(persons, firstNameIsBill);
}

@Test
public void should_check_multiple_different_types_of_matches() {
List<Person> persons = Arrays.asList(billoddie, tim, graeme, billkidd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,26 @@ public void should_check_field_uniqueness() {
BeanMatcherAsserts.shouldMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent);
}

@Test
public void should_check_field_absence() {
List<Map<String,String>> persons = Arrays.asList(mappedPerson("Bill", "Oddie"),
mappedPerson("Bill", "Kidd"),
mappedPerson("Graeam", "Garden"),
mappedPerson("Tim", "Brooke-Taylor"));

BeanMatcher firstNameIsJohn = BeanMatchers.the("firstName", is("John"));
BeanMatcherAsserts.shouldNotMatch(persons, firstNameIsJohn);
}

@Test(expected = AssertionError.class)
public void should_check_field_absence_with_failing_case() {
List<Map<String,String>> persons = Arrays.asList(mappedPerson("Bill", "Oddie"),
mappedPerson("Bill", "Kidd"),
mappedPerson("Graeam", "Garden"),
mappedPerson("Tim", "Brooke-Taylor"));

BeanMatcher firstNameIsBill = BeanMatchers.the("firstName", is("Bill"));
BeanMatcherAsserts.shouldNotMatch(persons, firstNameIsBill);
}

}

0 comments on commit 02ae8c1

Please sign in to comment.