static Equality instanceOf(Object that, Class<?> type) { if (that == null) { return nullValue(); } if (!type.isInstance(that)) { return typeNotEqual(type, that.getClass()); } return equal(); }
@Override protected boolean matchesSafely(List<T> thatList) { if (thisList == thatList) { return true; } if (thisList.size() != thatList.size()) { return false; } List<Equality> equalityList = new ArrayList<>(); Iterator<T> thisIter = thisList.iterator(); Iterator<T> thatIter = thatList.iterator(); int index = -1; while (thisIter.hasNext()) { index++; Testable thisObj = thisIter.next(); T thatObj = thatIter.next(); if (thisObj == null && thatObj != null) { equalityList.add(Equality.elementNotExpected(index, thatObj)); break; } equalityList.add(thisObj.isEqualTo(thatObj)); } Reporter reporter = Reporter.start(); for (Equality equality : equalityList) { if (!equality.isEqual()) { equality.accept(reporter); } } return reporter.lineList().isEmpty(); }
public static Equality of(String name, Iterable<?> a, Iterable<?> b) { if (a == b) { return Equality.equal(); resultList.add(Equality.nullValue()); return new IterableEquality(name, resultList); resultList.add(Equality.elementMissing(index, nextA)); break; ? ((Testable) nextA).isEqualTo(nextB) : Objects.equals(nextA, nextB) ? Equality.equal() : Equality.elementNotEqual(index, nextA, nextB); resultList.add(equality); index++; Object nextB = iterB.next(); resultList.add(Equality.elementNotExpected(index, nextB));
@Override public void accept(Reporter reporter) { reporter.name(name); for (Equality equality : resultList) { equality.accept(reporter); } }
@Override public boolean isEqual() { boolean isEqual = true; for (Equality equality : list) { if (!equality.isEqual()) { isEqual = false; break; } } return isEqual; }
public static Equality of(String name, Optional<?> a, Optional<?> b) { if (a == b) { return Equality.equal(); } if (b == null) { return new OptionalEquality(name, Equality.nullValue()); } Object maybeA = a.orElse(null); Object maybeB = b.orElse(null); Equality result = maybeA instanceof Testable ? ((Testable) maybeA).isEqualTo(maybeB) : Objects.equals(maybeA, maybeB) ? Equality.equal() : Equality.fail(Objects.toString(maybeA), Objects.toString(maybeB)); return new OptionalEquality(name, result); }
@SuppressWarnings("unchecked") public Equality test(T a, Object b) { try { if (b == null) { return Equality.nullValue(); } T that = (T) b; return tester.test(a, that); } catch (ClassCastException e) { Class<? extends Object> typeA = a.getClass(); Class<? extends Object> typeB = b.getClass(); return Equality.typeNotEqual(typeA, typeB); } }
@Override public Equality isEqualTo(Object that) { return Equality.equal(); }
public final SELF isEqualTo(List<T> thatList) { if (subject() == thatList) { return self(); } hasSize(thatList.size()); List<Equality> equalityList = new ArrayList<>(); Iterator<T> thisIter = subject().iterator(); Iterator<T> thatIter = thatList.iterator(); int index = -1; while (thisIter.hasNext()) { index++; Testable thisObj = (Testable) thisIter.next(); T thatObj = thatIter.next(); if (thisObj == null && thatObj != null) { equalityList.add(Equality.elementNotExpected(index, thatObj)); break; } equalityList.add(thisObj.isEqualTo(thatObj)); } Reporter reporter = Reporter.start(); for (Equality equality : equalityList) { if (!equality.isEqual()) { equality.accept(reporter); } } reporter.test(); return self(); }
public static Equality of(String name, Iterable<?> a, Iterable<?> b) { if (a == b) { return Equality.equal(); resultList.add(Equality.nullValue()); return new IterableEquality(name, resultList); resultList.add(Equality.elementMissing(index, nextA)); break; ? ((Testable) nextA).isEqualTo(nextB) : Objects.equals(nextA, nextB) ? Equality.equal() : Equality.elementNotEqual(index, nextA, nextB); resultList.add(equality); index++; Object nextB = iterB.next(); resultList.add(Equality.elementNotExpected(index, nextB));
@Override public void accept(Reporter reporter) { reporter.name(name); for (Equality equality : resultList) { equality.accept(reporter); } }
@Override public boolean isEqual() { boolean isEqual = true; for (Equality equality : resultList) { if (!equality.isEqual()) { isEqual = false; break; } } return isEqual; }
public static Equality of(String name, Optional<?> a, Optional<?> b) { if (a == b) { return Equality.equal(); } if (b == null) { return new OptionalEquality(name, Equality.nullValue()); } Object maybeA = a.orElse(null); Object maybeB = b.orElse(null); Equality result = maybeA instanceof Testable ? ((Testable) maybeA).isEqualTo(maybeB) : Objects.equals(maybeA, maybeB) ? Equality.equal() : Equality.fail(Objects.toString(maybeA), Objects.toString(maybeB)); return new OptionalEquality(name, result); }
@SuppressWarnings("unchecked") public Equality test(T a, Object b) { try { if (b == null) { return Equality.nullValue(); } T that = (T) b; return tester.test(a, that); } catch (ClassCastException e) { Class<? extends Object> typeA = a.getClass(); Class<? extends Object> typeB = b.getClass(); return Equality.typeNotEqual(typeA, typeB); } }
public final SELF isEqualTo(COL thatList) { if (subject() == thatList) { return self(); } hasSize(thatList.size()); List<Equality> equalityList = new ArrayList<>(); Iterator<T> thisIter = subject().iterator(); Iterator<T> thatIter = thatList.iterator(); int index = -1; while (thisIter.hasNext()) { index++; Testable thisObj = (Testable) thisIter.next(); T thatObj = thatIter.next(); if (thisObj == null && thatObj != null) { equalityList.add(Equality.elementNotExpected(index, thatObj)); break; } equalityList.add(thisObj.isEqualTo(thatObj)); } Reporter reporter = Reporter.start(); for (Equality equality : equalityList) { if (!equality.isEqual()) { equality.accept(reporter); } } reporter.test(); return self(); }
static Equality instanceOf(Object that, Class<?> type) { if (that == null) { return nullValue(); } if (!type.isInstance(that)) { return typeNotEqual(type, that.getClass()); } return equal(); }
private boolean isEqual(T thatObj) { Equality equality = thisObj.isEqualTo(thatObj); equality.accept(reporter); return reporter.lineList().isEmpty(); }