Skip to content

Commit

Permalink
Make EqualsTester test that non-Strings are not equal to their String…
Browse files Browse the repository at this point in the history
… representation.

RELNOTES=Make EqualsTester test that non-Strings are not equal to their String representation.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=331970427
  • Loading branch information
graememorgan authored and nick-someone committed Sep 17, 2020
1 parent 6fff2f5 commit c9570ea
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
Expand Up @@ -127,6 +127,11 @@ private void testItems() {
"the Object#hashCode of " + item + " must be consistent",
item.hashCode(),
item.hashCode());
if (!(item instanceof String)) {
assertTrue(
item + " must not be Object#equals to its Object#toString representation",
!item.equals(item.toString()));
}
}
}

Expand Down
Expand Up @@ -272,6 +272,15 @@ public void testEqualityGroups() {
.testEquals();
}

public void testEqualityBasedOnToString() {
try {
new EqualsTester().addEqualityGroup(new EqualsBasedOnToString("foo")).testEquals();
fail();
} catch (AssertionFailedError e) {
assertTrue(e.getMessage().contains("toString representation"));
}
}

private static void assertErrorMessage(Throwable e, String message) {
// TODO(kevinb): use a Truth assertion here
if (!e.getMessage().contains(message)) {
Expand Down Expand Up @@ -422,4 +431,27 @@ public String toString() {
return name;
}
}

private static final class EqualsBasedOnToString {
private final String s;

private EqualsBasedOnToString(String s) {
this.s = s;
}

@Override
public boolean equals(Object obj) {
return obj != null && obj.toString().equals(toString());
}

@Override
public int hashCode() {
return s.hashCode();
}

@Override
public String toString() {
return s;
}
}
}
Expand Up @@ -42,6 +42,12 @@ public void testAddTwoEqualObjectsAtOnceWithNull() throws Exception {
testCase.testAddTwoEqualObjectsAtOnceWithNull();
}

public void testEqualityBasedOnToString() throws Exception {
com.google.common.testing.EqualsTesterTest testCase = new com.google.common.testing.EqualsTesterTest();
testCase.setUp();
testCase.testEqualityBasedOnToString();
}

public void testEqualityGroups() throws Exception {
com.google.common.testing.EqualsTesterTest testCase = new com.google.common.testing.EqualsTesterTest();
testCase.setUp();
Expand Down
5 changes: 5 additions & 0 deletions guava-testlib/src/com/google/common/testing/EqualsTester.java
Expand Up @@ -127,6 +127,11 @@ private void testItems() {
"the Object#hashCode of " + item + " must be consistent",
item.hashCode(),
item.hashCode());
if (!(item instanceof String)) {
assertTrue(
item + " must not be Object#equals to its Object#toString representation",
!item.equals(item.toString()));
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions guava-testlib/test/com/google/common/testing/EqualsTesterTest.java
Expand Up @@ -272,6 +272,15 @@ public void testEqualityGroups() {
.testEquals();
}

public void testEqualityBasedOnToString() {
try {
new EqualsTester().addEqualityGroup(new EqualsBasedOnToString("foo")).testEquals();
fail();
} catch (AssertionFailedError e) {
assertTrue(e.getMessage().contains("toString representation"));
}
}

private static void assertErrorMessage(Throwable e, String message) {
// TODO(kevinb): use a Truth assertion here
if (!e.getMessage().contains(message)) {
Expand Down Expand Up @@ -422,4 +431,27 @@ public String toString() {
return name;
}
}

private static final class EqualsBasedOnToString {
private final String s;

private EqualsBasedOnToString(String s) {
this.s = s;
}

@Override
public boolean equals(Object obj) {
return obj != null && obj.toString().equals(toString());
}

@Override
public int hashCode() {
return s.hashCode();
}

@Override
public String toString() {
return s;
}
}
}

0 comments on commit c9570ea

Please sign in to comment.