truth0/truth — pull request #20 by cgruber (2012-07-27 07:23:59+00:00)
Title: Invert iteration assertions, so one can test things about the contents of iterations more easily using Subjects.

A feature that allows the developer to test propositions not merely about the collection object, but about the contents of the elements of an iterable, collectively. So instead of doing things like:

for (String s : myCol) {
ASSERT.that(s).contains("foo");
}

or

for (Integer i : myCol) {
ASSERT.that(i).isNotEqualTo(5);
}

You can do

ASSERT.in(myCol).thatEach(INTEGER).isNotEqualTo(4);

and

ASSERT.in(data).thatEach(STRING).contains("foo");

... where INTEGER and STRING are appropriately typed SubjectFactory objects.

This is important, not for these cases of integer and string, for which there are stock Subjects (and subjectfactories), but for custom cases where one either has a custom type over which one is iterating, or, even more tricky, where one has custom stubjects to extend the functionality of standard subjects. So one could have a custom subject that handles string, and do

ASSERT.in(data).thatEach(STRING_URL).isValidURL();

where STRING_URL is a constant containing a custom SubjectFactory that returns a custom Subject which adds isValidURL() as a proposition.

This handles a case that often needs hamcrest matchers or the like to do custom predicates over data sets, but does so in an IDE-completion-friendly (truthy) way.