Skip to content

Commit

Permalink
Expands Warning.STRICT_INHERITANCE to remove the need to call usingGe…
Browse files Browse the repository at this point in the history
…tClass()
  • Loading branch information
jqno committed Jun 11, 2020
1 parent 770f77b commit af33695
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Renames the project's main branch to `main`.
- Replaces all references to the word 'black' to 'blue', including those in internal (but accessible) API's.
- `Warning.STRICT_INHERITANCE` now also takes away the requirement to call `#usingGetClass()`

### Fixed
- Testing a class with that has fields but doesn't override `equals` causes "Mutability: equals depends on mutable field" error. ([Issue 315](https://github.com/jqno/equalsverifier/issues/315))
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/nl/jqno/equalsverifier/Warning.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ public enum Warning {
STRICT_HASHCODE,

/**
* Disables some of the stricter inheritance tests; use at your own risk!
* Disables some of the stricter inheritance tests.
*
* <p>{@link EqualsVerifier}'s standard behaviour, if T is not final and neither are its {@code
* equals} and {@code hashCode} methods, is to require a reference to a subclass of T for which
* no instance can be equal to any instance of T, to make sure that subclasses that can redefine
* {@code equals} or {@code hashCode} don't break the contract.
* {@code equals} or {@code hashCode} don't break the contract; or it asks to call the {@code
* usingGetClass} method if T uses {@code getClass()} instead of {@code instanceof} in its
* {@code equals} method.
*
* <p>Some may find that too strict for their liking; suppressing this warning disables that
* test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private Object getEqualSuper(T reference) {
}

private void checkSubclass() {
if (typeIsFinal) {
if (typeIsFinal || config.getWarningsToSuppress().contains(Warning.STRICT_INHERITANCE)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static nl.jqno.equalsverifier.testhelpers.Util.defaultHashCode;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import nl.jqno.equalsverifier.testhelpers.ExpectedExceptionTestBase;
import nl.jqno.equalsverifier.testhelpers.types.Color;
import nl.jqno.equalsverifier.testhelpers.types.FinalMethodsPoint;
Expand All @@ -11,11 +12,18 @@
import org.junit.Test;

public class GetClassTest extends ExpectedExceptionTestBase {

@Test
public void succeed_whenEqualsUsesGetClassInsteadOfInstanceof_givenUsingGetClassIsUsed() {
EqualsVerifier.forClass(GetClassPoint.class).usingGetClass().verify();
}

@Test
public void
succeed_whenEqualsUsesGetClassInsteadOfInstanceof_givenWarningStrictInheritanceIsSuppressed() {
EqualsVerifier.forClass(GetClassPoint.class).suppress(Warning.STRICT_INHERITANCE).verify();
}

@Test
public void fail_whenEqualsUsesGetClassButForgetsToCheckNull_givenUsingGetClassIsUsed() {
expectFailureWithCause(
Expand Down

0 comments on commit af33695

Please sign in to comment.