Skip to content

Commit

Permalink
#203 Stateless field cannot be ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Aug 5, 2018
1 parent 81c94eb commit 08ae679
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ private int countFalses(boolean... bools) {
}

private class SignificantFieldCheck implements FieldInspector.FieldCheck {
private final boolean skipTestBecause0AndNullBothHaveA0HashCode;
private final boolean skipCertainTestsThatDontMatterWhenValuesAreNull;

public SignificantFieldCheck(boolean skipTestBecause0AndNullBothHaveA0HashCode) {
this.skipTestBecause0AndNullBothHaveA0HashCode = skipTestBecause0AndNullBothHaveA0HashCode;
this.skipCertainTestsThatDontMatterWhenValuesAreNull = skipTestBecause0AndNullBothHaveA0HashCode;
}

@Override
Expand Down Expand Up @@ -185,7 +185,7 @@ private void assertEqualsAndHashCodeRelyOnSameFields(boolean equalsChanged, bool

if (equalsChanged != hashCodeChanged) {
boolean skipEqualsHasMoreThanHashCodeTest =
warningsToSuppress.contains(Warning.STRICT_HASHCODE) || skipTestBecause0AndNullBothHaveA0HashCode;
warningsToSuppress.contains(Warning.STRICT_HASHCODE) || skipCertainTestsThatDontMatterWhenValuesAreNull;
if (!skipEqualsHasMoreThanHashCodeTest) {
Formatter formatter = Formatter.of(
"Significant fields: equals relies on %%, but hashCode does not." +
Expand All @@ -211,7 +211,7 @@ private void assertFieldShouldBeIgnored(boolean equalToItself, boolean equalsCha
assertTrue(Formatter.of("Significant fields: equals does not use %%, or it is stateless.", fieldName),
fieldShouldBeIgnored || equalsChanged);
assertTrue(Formatter.of("Significant fields: equals should not use %%, but it does.", fieldName),
!fieldShouldBeIgnored || !equalsChanged);
!fieldShouldBeIgnored || !equalsChanged || skipCertainTestsThatDontMatterWhenValuesAreNull);
}
}

Expand All @@ -223,9 +223,9 @@ private boolean shouldAllFieldsBeUsed(FieldAccessor referenceAccessor) {

private boolean isFieldEligible(FieldAccessor referenceAccessor) {
return !referenceAccessor.fieldIsStatic() &&
!referenceAccessor.fieldIsTransient() &&
!referenceAccessor.fieldIsEmptyOrSingleValueEnum() &&
!classAccessor.fieldHasAnnotation(referenceAccessor.getField(), SupportedAnnotations.TRANSIENT);
!referenceAccessor.fieldIsTransient() &&
!referenceAccessor.fieldIsEmptyOrSingleValueEnum() &&
!classAccessor.fieldHasAnnotation(referenceAccessor.getField(), SupportedAnnotations.TRANSIENT);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ public void succeed_whenUsedFieldIsStateless_givenAllFieldsWarningIsSuppressed()
.verify();
}

@Test
public void succeed_whenUsedFieldIsStateless_givenStatelessFieldIsIgnored() {
EqualsVerifier.forClass(UsedStatelessContainer.class)
.withIgnoredFields("statelessField")
.verify();
}

@Test
public void succeed_whenClassIsStateless_givenAllFieldsWarningIsSuppressed() {
EqualsVerifier.forClass(Stateless.class)
Expand Down

0 comments on commit 08ae679

Please sign in to comment.