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

Add methods for content equals comparision (#1432) #1485

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions src/main/java/org/junit/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,40 @@ public static void assertNotSame(Object unexpected, Object actual) {
assertNotSame(null, unexpected, actual);
}

/**
* Asserts that two {@link CharSequence} instances have same content.
* If they do not, an {@link AssertionError} is thrown. If
* <code>expected</code> and <code>actual</code> are <code>null</code>,
* they are considered same content.
*
* @param expected the expected contents
* @param actual the object to compare to
*/
public static void assertContentEquals(String expected, CharSequence actual) {
assertContentEquals(null, expected, actual);
}

/**
* Asserts that two {@link CharSequence} instances have same content.
* If they do not, an {@link AssertionError} is thrown with the given message. If
* <code>expected</code> and <code>actual</code> are <code>null</code>,
* they are considered same content.
*
* @param message the identifying message for the {@link AssertionError}
* @param expected the expected contents
* @param actual the object to compare to
*/
public static void assertContentEquals(String message, String expected, CharSequence actual) {
if (equalsRegardingNull(expected, actual)) {
return;
}
if (expected == null || actual == null) {
failNotEquals(message, expected, actual);
}

assertEquals(message, expected, actual.toString());
}

private static void failSame(String message) {
String formatted = "";
if (message != null) {
Expand Down
85 changes: 85 additions & 0 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertContentEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
Expand Down Expand Up @@ -956,6 +957,90 @@ public void expectThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() {
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
public void assertContentEqualsPass() throws Exception {
String expected = "StringValue";
CharSequence charSequence = new String("StringValue");
assertContentEquals(expected, charSequence);
}

@Test
public void assertContentEqualsPassBothNull() throws Exception {
String expected = null;
CharSequence charSequence = null;
assertContentEquals(expected, charSequence);
}

@Test
public void assertContentsActualNull() {
String expected = "StringValue";
CharSequence charSequence = null;
try {
assertContentEquals(expected, charSequence);
} catch (AssertionError exception) {
String expectedException = "expected:<StringValue> but was:<null>";
assertEquals(expectedException, exception.getMessage());
return;
}
fail("Expected an AssertionError");
}

@Test
public void assertContentsExpectedNull() {
String expected = null;
CharSequence charSequence = new String("StringValue");
try {
assertContentEquals(expected, charSequence);
} catch (AssertionError exception) {
String expectedException = "expected:<null> but was:<StringValue>";
assertEquals(expectedException, exception.getMessage());
return;
}
fail("Expected an AssertionError");
}

@Test
public void assertContentEqualsNotEqualButSameLength() {
String expected = "StringValue";
CharSequence charSequence = new String("NotTheSame!");
try {
assertContentEquals(expected, charSequence);
} catch (AssertionError exception) {
String expectedException = "expected:<[StringValue]> but was:<[NotTheSame!]>";
assertEquals(expectedException, exception.getMessage());
return;
}
fail("Expected an AssertionError");
}

@Test
public void assertContentEqualsNotEqualDifferentLength() {
String expected = "StringValue";
CharSequence charSequence = new String("NotTheSame");
try {
assertContentEquals(expected, charSequence);
} catch (AssertionError exception) {
String expectedException = "expected:<[StringValu]e> but was:<[NotTheSam]e>";
assertEquals(expectedException, exception.getMessage());
return;
}
fail("Expected an AssertionError");
}

@Test
public void assertContentEqualsPassCustomMessage() throws Exception {
String expected = "StringValue";
CharSequence charSequence = new String("StringValue");
assertContentEquals("My Message", expected, charSequence);
}

@Test
public void assertContentEqualsPassBothNullCustomMessage() throws Exception {
String expected = null;
CharSequence charSequence = null;
assertContentEquals("My Message", expected, charSequence);
}

private static class NestedException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
Expand Down