Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For #942: Fixed TextEnvelope.equals() misbehavior. #952

Merged
merged 2 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/main/java/org/cactoos/text/TextEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,23 @@ public final int hashCode() {
return new UncheckedScalar<>(this.origin).value().hashCode();
}

// @todo #942:30min Refactor TextEnvelope.equals(). Current implementation
// of TextEnvelope.equals() uses some things that we must avoid, like more
// than one return on method, instance of usage and typecasting. Refactor
// this method so we can get rid of these smelly things.
//
@Override
@SuppressWarnings("PMD.OnlyOneReturn")
public final boolean equals(final Object obj) {
return new UncheckedScalar<>(this.origin).value().equals(obj);
if (this == obj) {
return true;
}
if (!(obj instanceof Text)) {
return false;
}
final Text that = (Text) obj;
return new UncheckedText(this).asString().equals(
new UncheckedText(that).asString()
);
}
}
23 changes: 20 additions & 3 deletions src/test/java/org/cactoos/text/TextEnvelopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,32 @@ public void testAsString() throws Exception {
}
/**
* Test for {@link TextEnvelope#equals(Object)} method. Must assert
* that the envelope value is equal to its string value.
* that the envelope value is equal another text representing the same
* value.
*/
@Test
public void testEquals() {
final String text = "equals";
MatcherAssert.assertThat(
"Envelope value does not match its represented String value",
"Envelope does not match text representing the same value",
new TextEnvelopeDummy(text),
new IsEqual<>(text)
new IsEqual<>(new TextOf(text))
);
}

/**
* Test for {@link TextEnvelope#equals(Object)} method. Must assert
* that the envelope value is equal another text representing the same
* value (in this case a {@link JoinedText}).
*/
@Test
public void testEqualsOtherText() {
MatcherAssert.assertThat(
"Envelope does not match another text representing the same value",
new TextEnvelopeDummy("isequaltoanothertext"),
new IsEqual<>(
new JoinedText("", "is", "equal", "to", "another", "text")
)
);
}
/**
Expand Down