Skip to content

Commit

Permalink
Fixes incorrect error with multiple permitted subtypes for sealed type
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Mar 16, 2023
1 parent 6921fe6 commit de6358a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Fixes incorrect error message when a sealed type has multiple permitted subtypes. ([Issue 786](https://github.com/jqno/equalsverifier/issues/786))

## [3.14] - 2023-02-27

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public void fail_whenSealeadParentHasAnIncorrectImplementationOfEquals() {
.assertFailure();
}

@Test
void succeed_whenSealedParentHasTwoChildren_a() {
EqualsVerifier.forClass(SealedChildA.class).verify();
}

@Test
void succeed_whenSealedParentHasTwoChildren_b() {
EqualsVerifier.forClass(SealedChildB.class).verify();
}

public abstract static sealed class SealedParentWithFinalChild permits FinalSealedChild {

private final int i;
Expand Down Expand Up @@ -196,4 +206,46 @@ public IncorrectSealedChild(int i) {
super(i);
}
}

public abstract static sealed class SealedParentWithTwoChildren {

final String value;

protected SealedParentWithTwoChildren(String value) {
this.value = value;
}

@Override
public boolean equals(Object other) {
return (
other != null &&
(this.getClass() == other.getClass()) &&
Objects.equals(this.value, ((SealedParentWithTwoChildren) other).value)
);
}

@Override
public int hashCode() {
return Objects.hashCode(this.value);
}

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

public static final class SealedChildA extends SealedParentWithTwoChildren {

SealedChildA(String value) {
super(value);
}
}

public static final class SealedChildB extends SealedParentWithTwoChildren {

SealedChildB(String value) {
super(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void check() {

private void checkSuperclass() {
ClassAccessor<? super T> superAccessor = classAccessor.getSuperAccessor();
if (superAccessor.isEqualsInheritedFromObject()) {
if (superAccessor.isEqualsInheritedFromObject() || superAccessor.isSealed()) {
return;
}

Expand Down

0 comments on commit de6358a

Please sign in to comment.