Skip to content

Commit

Permalink
Streamline overloaded assertion methods for Groovy
Browse files Browse the repository at this point in the history
Closes #2854
  • Loading branch information
krmahadevan committed Dec 18, 2022
1 parent 5ac0021 commit 89dc584
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-2854: overloaded assertEquals methods do not work from Groovy (Krishnan Mahadevan)

7.7.0
Fixed: GITHUB-2852: [SECURITY] Fix Zip Slip Vulnerability (Jonathan Leitschuh)
Expand Down
170 changes: 167 additions & 3 deletions testng-asserts/src/main/java/org/testng/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,18 @@ public static void assertEquals(double actual, Double expected, String message)
assertEquals(Double.valueOf(actual), expected, message);
}

/**
* Asserts that two doubles are equal. If they are not, an AssertionError, with the given message,
* is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEquals(Double actual, Double expected, String message) {
assertEquals(actual, (Object) expected, message);
}

/**
* Asserts that two doubles are equal. If they are not, an AssertionError is thrown.
*
Expand Down Expand Up @@ -782,8 +794,18 @@ public static void assertEquals(double actual, Double expected) {
assertEquals(Double.valueOf(actual), expected, null);
}

/**
* Asserts that two doubles are equal. If they are not, an AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEquals(Double actual, Double expected) {
assertEquals(actual, expected, null);
}

private static boolean areEqual(float actual, float expected, float delta) {
// handle infinity specially since subtracting to infinite values gives NaN and the
// handle infinity specially since subtracting to infinite values gives NaN and
// the following test fails
if (Float.isInfinite(expected)) {
if (!(expected == actual)) {
Expand Down Expand Up @@ -869,6 +891,18 @@ public static void assertEquals(float actual, Float expected, String message) {
assertEquals(Float.valueOf(actual), expected, message);
}

/**
* Asserts that two floats are equal. If they are not, an AssertionError, with the given message,
* is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEquals(Float actual, Float expected, String message) {
assertEquals(actual, (Object) expected, message);
}

/**
* Asserts that two floats are equal. If they are not, an AssertionError is thrown.
*
Expand Down Expand Up @@ -899,6 +933,16 @@ public static void assertEquals(float actual, Float expected) {
assertEquals(Float.valueOf(actual), expected, null);
}

/**
* Asserts that two floats are equal. If they are not, an AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEquals(Float actual, Float expected) {
assertEquals(actual, expected, null);
}

/**
* Asserts that two longs are equal. If they are not, an AssertionError, with the given message,
* is thrown.
Expand Down Expand Up @@ -931,8 +975,8 @@ public static void assertEquals(Long actual, long expected, String message) {
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEquals(long actual, Long expected, String message) {
assertEquals(Long.valueOf(actual), expected, message);
public static void assertEquals(Long actual, Long expected, String message) {
assertEquals(actual, (Object) expected, message);
}

/**
Expand Down Expand Up @@ -964,6 +1008,16 @@ public static void assertEquals(long actual, Long expected) {
assertEquals(Long.valueOf(actual), expected, null);
}

/**
* Asserts that two longs are equal. If they are not, an AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEquals(Long actual, Long expected) {
assertEquals(actual, expected, null);
}

/**
* Asserts that two booleans are equal. If they are not, an AssertionError, with the given
* message, is thrown.
Expand Down Expand Up @@ -1000,6 +1054,18 @@ public static void assertEquals(boolean actual, Boolean expected, String message
assertEquals(Boolean.valueOf(actual), expected, message);
}

/**
* Asserts that two booleans are equal. If they are not, an AssertionError, with the given
* message, is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEquals(Boolean actual, Boolean expected, String message) {
assertEquals(actual, (Object) expected, message);
}

/**
* Asserts that two booleans are equal. If they are not, an AssertionError is thrown.
*
Expand All @@ -1020,6 +1086,16 @@ public static void assertEquals(Boolean actual, boolean expected) {
assertEquals(actual, Boolean.valueOf(expected), null);
}

/**
* Asserts that two booleans are equal. If they are not, an AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEquals(Boolean actual, Boolean expected) {
assertEquals(actual, expected, null);
}

/**
* Asserts that two booleans are equal. If they are not, an AssertionError is thrown.
*
Expand Down Expand Up @@ -1066,6 +1142,18 @@ public static void assertEquals(byte actual, Byte expected, String message) {
assertEquals(Byte.valueOf(actual), expected, message);
}

/**
* Asserts that two bytes are equal. If they are not, an AssertionError, with the given message,
* is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEquals(Byte actual, Byte expected, String message) {
assertEquals(actual, (Object) expected, message);
}

/**
* Asserts that two bytes are equal. If they are not, an AssertionError is thrown.
*
Expand All @@ -1086,6 +1174,16 @@ public static void assertEquals(Byte actual, byte expected) {
assertEquals(actual, Byte.valueOf(expected), null);
}

/**
* Asserts that two bytes are equal. If they are not, an AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEquals(Byte actual, Byte expected) {
assertEquals(actual, expected, null);
}

/**
* Asserts that two bytes are equal. If they are not, an AssertionError is thrown.
*
Expand Down Expand Up @@ -1132,6 +1230,18 @@ public static void assertEquals(char actual, Character expected, String message)
assertEquals(Character.valueOf(actual), expected, message);
}

/**
* Asserts that two chars are equal. If they are not, an AssertionError, with the given message,
* is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEquals(Character actual, Character expected, String message) {
assertEquals(actual, (Object) expected, message);
}

/**
* Asserts that two chars are equal. If they are not, an AssertionError is thrown.
*
Expand Down Expand Up @@ -1162,6 +1272,16 @@ public static void assertEquals(char actual, Character expected) {
assertEquals(Character.valueOf(actual), expected, null);
}

/**
* Asserts that two chars are equal. If they are not, an AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEquals(Character actual, Character expected) {
assertEquals(actual, expected, null);
}

/**
* Asserts that two shorts are equal. If they are not, an AssertionError, with the given message,
* is thrown.
Expand Down Expand Up @@ -1198,6 +1318,18 @@ public static void assertEquals(short actual, Short expected, String message) {
assertEquals(Short.valueOf(actual), expected, message);
}

/**
* Asserts that two shorts are equal. If they are not, an AssertionError, with the given message,
* is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEquals(Short actual, Short expected, String message) {
assertEquals(actual, (Object) expected, message);
}

/**
* Asserts that two shorts are equal. If they are not, an AssertionError is thrown.
*
Expand Down Expand Up @@ -1228,6 +1360,16 @@ public static void assertEquals(short actual, Short expected) {
assertEquals(Short.valueOf(actual), expected, null);
}

/**
* Asserts that two shorts are equal. If they are not, an AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEquals(Short actual, Short expected) {
assertEquals(actual, expected, null);
}

/**
* Asserts that two ints are equal. If they are not, an AssertionError, with the given message, is
* thrown.
Expand Down Expand Up @@ -1264,6 +1406,18 @@ public static void assertEquals(int actual, Integer expected, String message) {
assertEquals(Integer.valueOf(actual), expected, message);
}

/**
* Asserts that two ints are equal. If they are not, an AssertionError, with the given message, is
* thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
public static void assertEquals(Integer actual, Integer expected, String message) {
assertEquals(actual, (Object) expected, message);
}

/**
* Asserts that two ints are equal. If they are not, an AssertionError is thrown.
*
Expand Down Expand Up @@ -1294,6 +1448,16 @@ public static void assertEquals(int actual, Integer expected) {
assertEquals(Integer.valueOf(actual), expected, null);
}

/**
* Asserts that two ints are equal. If they are not, an AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
*/
public static void assertEquals(Integer actual, Integer expected) {
assertEquals(actual, expected, null);
}

/**
* Asserts that an object isn't null. If it is, an AssertionError is thrown.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void assertEqualsBoxedUnboxedLong() {
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
assertEquals(Long.valueOf(b), a, "");
}

@Test(description = "GITHUB-2652")
Expand Down
8 changes: 7 additions & 1 deletion testng-core/src/test/groovy/test/groovy/GroovyTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import org.testng.ITestContext
import org.testng.ITestNGListener
import org.testng.TestListenerAdapter
import org.testng.annotations.Test
import org.testng.reporters.JUnitXMLReporter
import test.SimpleBaseTest
import test.groovy.issue2854.AssertionsTestSample

import static org.assertj.core.api.Assertions.assertThat

Expand Down Expand Up @@ -53,4 +53,10 @@ class GroovyTest extends SimpleBaseTest {
assertThat testMethodNames containsExactly "test1", "test2"
}

@Test(description = "GITHUB-2854")
void ensureAssertionsWork() {
def testng = create AssertionsTestSample
testng.run()
assertThat testng.status isEqualTo(0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package test.groovy.issue2854

import org.testng.annotations.Test

import static org.testng.Assert.assertEquals

class AssertionsTestSample {

@Test
void testAssertEqualsWorksWithBooleans() {
assertEquals(true, true)
assertEquals(true, true, "Sample Message")
}

@Test
void testAssertEqualsWorksWithBytes() {
assertEquals(Byte.valueOf((byte) 10), Byte.valueOf((byte) 10))
assertEquals(Byte.valueOf((byte) 10), Byte.valueOf((byte) 10), "Sample Message")
}

@Test
void testAssertEqualsWorksWithChars() {
assertEquals(Character.valueOf((char) 10), Character.valueOf((char) 10))
assertEquals(Character.valueOf((char) 10), Character.valueOf((char) 10), "Sample Message")
}

@Test
void testAssertEqualsWorksWithShorts() {
assertEquals(Short.valueOf((short) 10), Short.valueOf((short) 10))
assertEquals(Short.valueOf((short) 10), Short.valueOf((short) 10), "Sample Message")
}

@Test
void testAssertEqualsWorksWithInts() {
assertEquals(10, 10)
assertEquals(10, 10, "Sample Message")
}

@Test
void testAssertEqualsWorksWithLongs() {
assertEquals(Long.valueOf((long) 10), Long.valueOf((long) 10))
assertEquals(Long.valueOf((long) 10), Long.valueOf((long) 10), "Sample Message")
}

@Test
void testAssertEqualsWorksWithFloats() {
assertEquals(Float.valueOf((float) 10), Float.valueOf((float) 10))
assertEquals(Float.valueOf((float) 10), Float.valueOf((float) 10), "Sample Message")
}

@Test
void testAssertEqualsWorksWithDoubles() {
assertEquals(Double.valueOf((double) 10), Double.valueOf((double) 10))
assertEquals(Double.valueOf((double) 10), Double.valueOf((double) 10), "Sample Message")
}

}

0 comments on commit 89dc584

Please # to comment.