From 45406fc92a82664ff4bcd53fda5d74c773384629 Mon Sep 17 00:00:00 2001 From: Christos Giallouros Date: Thu, 15 Dec 2022 12:59:03 +0200 Subject: [PATCH] Rewrite Assertions.assertInstanceOf --- .../refasterrules/JUnitToAssertJRules.java | 41 ++++++++++++++++++- .../JUnitToAssertJRulesTestInput.java | 12 ++++++ .../JUnitToAssertJRulesTestOutput.java | 12 ++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/JUnitToAssertJRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/JUnitToAssertJRules.java index c6ca8c9802f..7650a73da77 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/JUnitToAssertJRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/JUnitToAssertJRules.java @@ -7,6 +7,7 @@ import static org.assertj.core.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; @@ -446,5 +447,43 @@ void after(ThrowingCallable runnable, Supplier messageSupplier) { // XXX: Rewrite org.junit.jupiter.api.Assertions.assertTimeout // XXX: Rewrite org.junit.jupiter.api.Assertions.assertTimeoutPreemptively - // XXX: Rewrite org.junit.jupiter.api.Assertions.assertInstanceOf + + static final class AssertInstanceOf { + @BeforeTemplate + void before(Object object, Class clazz) { + assertInstanceOf(clazz, object); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object object, Class clazz) { + assertThat(object).isInstanceOf(clazz); + } + } + + static final class AssertInstanceOfWithMessage { + @BeforeTemplate + void before(Object object, Class clazz, String message) { + assertInstanceOf(clazz, object, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object object, Class clazz, String message) { + assertThat(object).withFailMessage(message).isInstanceOf(clazz); + } + } + + static final class AssertInstanceOfWithMessageSupplier { + @BeforeTemplate + void before(Object object, Class clazz, Supplier messageSupplier) { + assertInstanceOf(clazz, object, messageSupplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object object, Class clazz, Supplier messageSupplier) { + assertThat(object).withFailMessage(messageSupplier).isInstanceOf(clazz); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestInput.java index 9c07c7879a3..f991c67c651 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestInput.java @@ -147,4 +147,16 @@ void testAssertDoesNotThrowWithMessage() { void testAssertDoesNotThrowWithMessageSupplier() { assertDoesNotThrow(() -> {}, () -> "foo"); } + + void testAssertInstanceOf() { + assertInstanceOf(Object.class, new Object()); + } + + void testAssertInstanceOfWithMessage() { + assertInstanceOf(Object.class, new Object(), "foo"); + } + + void testAssertInstanceOfWithMessageSupplier() { + assertInstanceOf(Object.class, new Object(), () -> "foo"); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestOutput.java index 0cb792e451e..496ddf5905f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestOutput.java @@ -153,4 +153,16 @@ void testAssertDoesNotThrowWithMessage() { void testAssertDoesNotThrowWithMessageSupplier() { assertThatCode(() -> {}).withFailMessage(() -> "foo").doesNotThrowAnyException(); } + + void testAssertInstanceOf() { + assertThat(new Object()).isInstanceOf(Object.class); + } + + void testAssertInstanceOfWithMessage() { + assertThat(new Object()).withFailMessage("foo").isInstanceOf(Object.class); + } + + void testAssertInstanceOfWithMessageSupplier() { + assertThat(new Object()).withFailMessage(() -> "foo").isInstanceOf(Object.class); + } }