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

Fix assertSame to match semantics of == for primitive types #1523

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
165 changes: 165 additions & 0 deletions src/main/java/org/junit/Assert.java
Expand Up @@ -772,6 +772,54 @@ public static void assertSame(String message, Object expected, Object actual) {
failNotSame(message, expected, actual);
}

/**
* Asserts that two boolean values are the same. If they are not, an
* {@link AssertionError} is thrown with the given message.
*
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
* okay)
* @param expected the expected object
* @param actual the value to compare to <code>expected</code>
*/
public static void assertSame(String message, boolean expected, boolean actual) {
if (expected == actual) {
return;
}
failNotSame(message, expected, actual);
}

/**
* Asserts that two long values are the same. If they are not, an
* {@link AssertionError} is thrown with the given message.
*
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
* okay)
* @param expected the expected object
* @param actual the value to compare to <code>expected</code>
*/
public static void assertSame(String message, long expected, long actual) {
if (expected == actual) {
return;
}
failNotSame(message, expected, actual);
}

/**
* Asserts that two double values are the same. If they are not, an
* {@link AssertionError} is thrown with the given message.
*
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
* okay)
* @param expected the expected object
* @param actual the value to compare to <code>expected</code>
*/
public static void assertSame(String message, double expected, double actual) {
if (expected == actual) {
return;
}
failNotSame(message, expected, actual);
}

/**
* Asserts that two objects refer to the same object. If they are not the
* same, an {@link AssertionError} without a message is thrown.
Expand All @@ -783,6 +831,87 @@ public static void assertSame(Object expected, Object actual) {
assertSame(null, expected, actual);
}

/**
* Asserts that two boolean values are the same. If they are not the
* same, an {@link AssertionError} without a message is thrown.
*
* @param expected the expected value
* @param actual the value to compare to <code>expected</code>
*/
public static void assertSame(boolean expected, boolean actual) {
assertSame(null, expected, actual);
}

/**
* Asserts that two long values are the same. If they are not the
* same, an {@link AssertionError} without a message is thrown.
*
* @param expected the expected value
* @param actual the value to compare to <code>expected</code>
*/
public static void assertSame(long expected, long actual) {
assertSame(null, expected, actual);
}

/**
* Asserts that two double values are the same. If they are not the
* same, an {@link AssertionError} without a message is thrown.
*
* @param expected the expected value
* @param actual the value to compare to <code>expected</code>
*/
public static void assertSame(double expected, double actual) {
assertSame(null, expected, actual);
}

/**
* Asserts that two boolean values are not the same. If they are the same value,
* an {@link AssertionError} is thrown with the given message.
*
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
* okay)
* @param unexpected the value you don't expect
* @param actual the value to compare to <code>unexpected</code>
*/
public static void assertNotSame(String message, boolean unexpected,
boolean actual) {
if (unexpected == actual) {
failSame(message);
}
}

/**
* Asserts that two long values are not the same. If they are the same value,
* an {@link AssertionError} is thrown with the given message.
*
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
* okay)
* @param unexpected the value you don't expect
* @param actual the value to compare to <code>unexpected</code>
*/
public static void assertNotSame(String message, long unexpected,
long actual) {
if (unexpected == actual) {
failSame(message);
}
}

/**
* Asserts that two double values are not the same. If they are the same value,
* an {@link AssertionError} is thrown with the given message.
*
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
* okay)
* @param unexpected the value you don't expect
* @param actual the value to compare to <code>unexpected</code>
*/
public static void assertNotSame(String message, double unexpected,
double actual) {
if (unexpected == actual) {
failSame(message);
}
}

/**
* Asserts that two objects do not refer to the same object. If they do
* refer to the same object, an {@link AssertionError} is thrown with the
Expand All @@ -800,6 +929,42 @@ public static void assertNotSame(String message, Object unexpected,
}
}

/**
* Asserts that two boolean values are not the same value. If they are
* the same, an {@link AssertionError} without a message is
* thrown.
*
* @param unexpected the value you don't expect
* @param actual the value to compare to <code>unexpected</code>
*/
public static void assertNotSame(boolean unexpected, boolean actual) {
assertNotSame(null, unexpected, actual);
}

/**
* Asserts that two long values are not the same value. If they are
* the same, an {@link AssertionError} without a message is
* thrown.
*
* @param unexpected the value you don't expect
* @param actual the value to compare to <code>unexpected</code>
*/
public static void assertNotSame(long unexpected, long actual) {
assertNotSame(null, unexpected, actual);
}

/**
* Asserts that two double values are not the same value. If they are
* the same, an {@link AssertionError} without a message is
* thrown.
*
* @param unexpected the value you don't expect
* @param actual the value to compare to <code>unexpected</code>
*/
public static void assertNotSame(double unexpected, double actual) {
assertNotSame(null, unexpected, actual);
}

/**
* Asserts that two objects do not refer to the same object. If they do
* refer to the same object, an {@link AssertionError} without a message is
Expand Down