-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #927 from jqno/sealed-interface-recursion
Sealed interface recursion
- Loading branch information
Showing
12 changed files
with
290 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...t/java/nl/jqno/equalsverifier/integration/extended_contract/SealedTypesRecursionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package nl.jqno.equalsverifier.integration.extended_contract; | ||
|
||
import java.util.Objects; | ||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class SealedTypesRecursionTest { | ||
|
||
@Test | ||
public void dontThrowStackOverflowError_whenOnlyPermittedSubclassInSealedInterfaceRefersBackToContainer() { | ||
// A container with a field of a sealed interface. | ||
// The sealed interface permits only 1 type, which refers back to the container. | ||
ExpectedException | ||
.when(() -> EqualsVerifier.forClass(SealedContainer.class).verify()) | ||
.assertFailure() | ||
.assertMessageContains( | ||
"Recursive datastructure", | ||
"Add prefab values for one of the following types", | ||
"SealedContainer", | ||
"SealedInterface" | ||
); | ||
} | ||
|
||
@Test | ||
public void dontThrowStackOverflowError_whenOnlyPermittedRecordInSealedInterfaceRefersBackToContainer() { | ||
// A container with a field of a sealed interface. | ||
// The sealed interface permits only 1 type, which is a record that refers back to the container. | ||
ExpectedException | ||
.when(() -> EqualsVerifier.forClass(SealedRecordContainer.class).verify()) | ||
.assertFailure() | ||
.assertMessageContains( | ||
"Recursive datastructure", | ||
"Add prefab values for one of the following types", | ||
"SealedRecordContainer", | ||
"SealedRecordInterface" | ||
); | ||
} | ||
|
||
static final class SealedContainer { | ||
|
||
public final SealedInterface sealed; | ||
|
||
public SealedContainer(SealedInterface sealed) { | ||
this.sealed = sealed; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(sealed); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (!(obj instanceof SealedContainer)) { | ||
return false; | ||
} | ||
SealedContainer other = (SealedContainer) obj; | ||
return Objects.equals(sealed, other.sealed); | ||
} | ||
} | ||
|
||
sealed interface SealedInterface permits OnlyPermittedImplementation {} | ||
|
||
static final class OnlyPermittedImplementation implements SealedInterface { | ||
|
||
public final SealedContainer container; | ||
|
||
public OnlyPermittedImplementation(SealedContainer container) { | ||
this.container = container; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(container); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (!(obj instanceof OnlyPermittedImplementation)) { | ||
return false; | ||
} | ||
OnlyPermittedImplementation other = (OnlyPermittedImplementation) obj; | ||
return Objects.equals(container, other.container); | ||
} | ||
} | ||
|
||
static final class SealedRecordContainer { | ||
|
||
public final SealedRecordInterface sealed; | ||
|
||
public SealedRecordContainer(SealedRecordInterface sealed) { | ||
this.sealed = sealed; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(sealed); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (!(obj instanceof SealedRecordContainer)) { | ||
return false; | ||
} | ||
SealedRecordContainer other = (SealedRecordContainer) obj; | ||
return Objects.equals(sealed, other.sealed); | ||
} | ||
} | ||
|
||
sealed interface SealedRecordInterface permits OnlyPermittedRecordImplementation {} | ||
|
||
static final record OnlyPermittedRecordImplementation(SealedRecordContainer container) | ||
implements SealedRecordInterface {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.