Skip to content

Commit

Permalink
Adds test for forPackage with recursive data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jul 2, 2024
1 parent 83aea19 commit 3e071ff
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class MultipleTypeEqualsVerifierTest {
"nl.jqno.equalsverifier.testhelpers.packages.twoincorrect";
private static final String SUBCLASSES_PACKAGE =
"nl.jqno.equalsverifier.testhelpers.packages.subclasses";
private static final String SOME_RECURSIVE_PACKAGE =
"nl.jqno.equalsverifier.testhelpers.packages.somerecursive";
private static final String INCORRECT_M = INCORRECT_PACKAGE + ".IncorrectM";
private static final String INCORRECT_N = INCORRECT_PACKAGE + ".IncorrectN";
private static final String INCORRECT_O = INCORRECT_PACKAGE + ".subpackage.IncorrectO";
Expand Down Expand Up @@ -66,6 +68,15 @@ public void succeed_whenVerifyingAPackageWithASuperInterface_givenOneOfTheImplem
EqualsVerifier.forPackage(SUBCLASSES_PACKAGE, SuperI.class).verify();
}

@Test
public void doesNotReportNonrecursive_whenPackageContainsRecursiveAndNonrecursiveClasses() {
ExpectedException
.when(() -> EqualsVerifier.forPackage(SOME_RECURSIVE_PACKAGE).verify())
.assertFailure()
.assertMessageContains("Recursive datastructure", "RecursiveA", "RecursiveB")
.assertMessageDoesNotContain("Nonrecursive");
}

@Test
public void fail_whenVerifyingOneIncorrectClass() {
ExpectedException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nl.jqno.equalsverifier.testhelpers.packages.somerecursive;

public final class Nonrecursive {

private final int x;
private final int y;

public Nonrecursive(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Nonrecursive)) {
return false;
}
Nonrecursive p = (Nonrecursive) obj;
return p.x == x && p.y == y;
}

@Override
public int hashCode() {
return x + (31 * y);
}

@Override
public String toString() {
return getClass().getSimpleName() + ":" + x + "," + y;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nl.jqno.equalsverifier.testhelpers.packages.somerecursive;

import java.util.Objects;

public final class RecursiveA {

private final RecursiveB b;

public RecursiveA(RecursiveB b) {
this.b = b;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof RecursiveA)) {
return false;
}
RecursiveA p = (RecursiveA) obj;
return Objects.equals(b, p.b);
}

@Override
public int hashCode() {
return Objects.hash(b);
}

@Override
public String toString() {
return getClass().getSimpleName() + ":" + b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nl.jqno.equalsverifier.testhelpers.packages.somerecursive;

import java.util.Objects;

public final class RecursiveB {

private final RecursiveA a;

public RecursiveB(RecursiveA a) {
this.a = a;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof RecursiveB)) {
return false;
}
RecursiveB p = (RecursiveB) obj;
return Objects.equals(a, p.a);
}

@Override
public int hashCode() {
return Objects.hash(a);
}

@Override
public String toString() {
return getClass().getSimpleName() + ":" + a;
}
}

0 comments on commit 3e071ff

Please sign in to comment.