Skip to content

Instantly share code, notes, and snippets.

@wadouk
Created October 18, 2013 14:35
Show Gist options
  • Save wadouk/7042485 to your computer and use it in GitHub Desktop.
Save wadouk/7042485 to your computer and use it in GitHub Desktop.
Expressive assert on List<Date> to use with onProperty
package hellofest;
import static hellofest.DateEquals.areEquals;
import static java.util.Arrays.asList;
import static org.fest.assertions.Assertions.assertThat;
import java.util.Date;
import java.util.List;
import org.junit.Test;
public class AssertMeThis {
@Test
public void hello() {
final Date date1 = new Date();
final Date date2 = new Date();
final List<Date> actual = asList(date1,date2);
assertThat(actual).is(sameList(date1,date2));
}
}
package hellofest;
import static java.util.Arrays.asList;
import static org.fest.assertions.Formatting.*;
import java.util.Date;
import java.util.List;
import org.fest.assertions.Condition;
public class DateEquals extends Condition<List<?>> {
private List<Date> dates;
public DateEquals(String description) {
super(description);
}
public static DateEquals sameList(Date... actual) {
final DateEquals condition = new DateEquals(format("%s", actual));
condition.setDates(asList(actual));
return condition;
}
@Override
public boolean matches(List<?> value) {
boolean matches = true;
if (value.size() != dates.size())
return false;
for (int i = 0; i < value.size(); i++) {
long l1 = ((Date) value.get(i)).getTime();
long l2 = dates.get(i).getTime();
boolean currentMatch = (l1 == l2);
matches = matches && currentMatch;
}
return matches;
}
public void setDates(List<Date> dates) {
this.dates = dates;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment