diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index 959208f32f..e8e95dbb7e 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -146,7 +146,7 @@ org.junit.jupiter junit-jupiter-api - test + provided org.junit.jupiter 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 new file mode 100644 index 0000000000..7d89b6ec29 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/JUnitToAssertJRules.java @@ -0,0 +1,521 @@ +package tech.picnic.errorprone.refasterrules; + +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +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; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.google.common.collect.ImmutableSet; +import com.google.errorprone.annotations.DoNotCall; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; +import java.util.function.Supplier; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.function.Executable; +import org.junit.jupiter.api.function.ThrowingSupplier; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +/** + * Refaster rules to replace JUnit assertions with AssertJ equivalents. + * + *

Note that, while both libraries throw an {@link AssertionError} in case of an assertion + * failure, the exact subtype used generally differs. + */ +// XXX: Not all `org.assertj.core.api.Assertions` methods have an associated Refaster rule yet; +// expand this class. +// XXX: Introduce a `@Matcher` on `Executable` and `ThrowingSupplier` expressions, such that they +// are only matched if they are also compatible with the `ThrowingCallable` functional interface. +// When implementing such a matcher, note that expressions with a non-void return type such as +// `() -> toString()` match both `ThrowingSupplier` and `ThrowingCallable`, but `() -> "constant"` +// is only compatible with the former. +@OnlineDocumentation +final class JUnitToAssertJRules { + private JUnitToAssertJRules() {} + + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of( + Assertions.class, + assertDoesNotThrow(() -> null), + assertInstanceOf(null, null), + assertThrows(null, null), + assertThrowsExactly(null, null), + (Runnable) () -> assertFalse(true), + (Runnable) () -> assertNotNull(null), + (Runnable) () -> assertNotSame(null, null), + (Runnable) () -> assertNull(null), + (Runnable) () -> assertSame(null, null), + (Runnable) () -> assertTrue(true)); + } + + static final class ThrowNewAssertionError { + @BeforeTemplate + void before() { + Assertions.fail(); + } + + @AfterTemplate + @DoNotCall + void after() { + throw new AssertionError(); + } + } + + static final class FailWithMessage { + @BeforeTemplate + T before(String message) { + return Assertions.fail(message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + T after(String message) { + return fail(message); + } + } + + static final class FailWithMessageAndThrowable { + @BeforeTemplate + T before(String message, Throwable throwable) { + return Assertions.fail(message, throwable); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + T after(String message, Throwable throwable) { + return fail(message, throwable); + } + } + + static final class FailWithThrowable { + @BeforeTemplate + void before(Throwable throwable) { + Assertions.fail(throwable); + } + + @AfterTemplate + @DoNotCall + void after(Throwable throwable) { + throw new AssertionError(throwable); + } + } + + static final class AssertThatIsTrue { + @BeforeTemplate + void before(boolean actual) { + assertTrue(actual); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(boolean actual) { + assertThat(actual).isTrue(); + } + } + + static final class AssertThatWithFailMessageStringIsTrue { + @BeforeTemplate + void before(boolean actual, String message) { + assertTrue(actual, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(boolean actual, String message) { + assertThat(actual).withFailMessage(message).isTrue(); + } + } + + static final class AssertThatWithFailMessageSupplierIsTrue { + @BeforeTemplate + void before(boolean actual, Supplier supplier) { + assertTrue(actual, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(boolean actual, Supplier supplier) { + assertThat(actual).withFailMessage(supplier).isTrue(); + } + } + + static final class AssertThatIsFalse { + @BeforeTemplate + void before(boolean actual) { + assertFalse(actual); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(boolean actual) { + assertThat(actual).isFalse(); + } + } + + static final class AssertThatWithFailMessageStringIsFalse { + @BeforeTemplate + void before(boolean actual, String message) { + assertFalse(actual, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(boolean actual, String message) { + assertThat(actual).withFailMessage(message).isFalse(); + } + } + + static final class AssertThatWithFailMessageSupplierIsFalse { + @BeforeTemplate + void before(boolean actual, Supplier supplier) { + assertFalse(actual, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(boolean actual, Supplier supplier) { + assertThat(actual).withFailMessage(supplier).isFalse(); + } + } + + static final class AssertThatIsNull { + @BeforeTemplate + void before(Object actual) { + assertNull(actual); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual) { + assertThat(actual).isNull(); + } + } + + static final class AssertThatWithFailMessageStringIsNull { + @BeforeTemplate + void before(Object actual, String message) { + assertNull(actual, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, String message) { + assertThat(actual).withFailMessage(message).isNull(); + } + } + + static final class AssertThatWithFailMessageSupplierIsNull { + @BeforeTemplate + void before(Object actual, Supplier supplier) { + assertNull(actual, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Supplier supplier) { + assertThat(actual).withFailMessage(supplier).isNull(); + } + } + + static final class AssertThatIsNotNull { + @BeforeTemplate + void before(Object actual) { + assertNotNull(actual); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual) { + assertThat(actual).isNotNull(); + } + } + + static final class AssertThatWithFailMessageStringIsNotNull { + @BeforeTemplate + void before(Object actual, String message) { + assertNotNull(actual, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, String message) { + assertThat(actual).withFailMessage(message).isNotNull(); + } + } + + static final class AssertThatWithFailMessageSupplierIsNotNull { + @BeforeTemplate + void before(Object actual, Supplier supplier) { + assertNotNull(actual, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Supplier supplier) { + assertThat(actual).withFailMessage(supplier).isNotNull(); + } + } + + static final class AssertThatIsSameAs { + @BeforeTemplate + void before(Object actual, Object expected) { + assertSame(expected, actual); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Object expected) { + assertThat(actual).isSameAs(expected); + } + } + + static final class AssertThatWithFailMessageStringIsSameAs { + @BeforeTemplate + void before(Object actual, Object expected, String message) { + assertSame(expected, actual, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Object expected, String message) { + assertThat(actual).withFailMessage(message).isSameAs(expected); + } + } + + static final class AssertThatWithFailMessageSupplierIsSameAs { + @BeforeTemplate + void before(Object actual, Object expected, Supplier supplier) { + assertSame(expected, actual, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Object expected, Supplier supplier) { + assertThat(actual).withFailMessage(supplier).isSameAs(expected); + } + } + + static final class AssertThatIsNotSameAs { + @BeforeTemplate + void before(Object actual, Object expected) { + assertNotSame(expected, actual); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Object expected) { + assertThat(actual).isNotSameAs(expected); + } + } + + static final class AssertThatWithFailMessageStringIsNotSameAs { + @BeforeTemplate + void before(Object actual, Object expected, String message) { + assertNotSame(expected, actual, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Object expected, String message) { + assertThat(actual).withFailMessage(message).isNotSameAs(expected); + } + } + + static final class AssertThatWithFailMessageSupplierIsNotSameAs { + @BeforeTemplate + void before(Object actual, Object expected, Supplier supplier) { + assertNotSame(expected, actual, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Object expected, Supplier supplier) { + assertThat(actual).withFailMessage(supplier).isNotSameAs(expected); + } + } + + static final class AssertThatThrownByIsExactlyInstanceOf { + @BeforeTemplate + void before(Executable throwingCallable, Class clazz) { + assertThrowsExactly(clazz, throwingCallable); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable, Class clazz) { + assertThatThrownBy(throwingCallable).isExactlyInstanceOf(clazz); + } + } + + static final class AssertThatThrownByWithFailMessageStringIsExactlyInstanceOf< + T extends Throwable> { + @BeforeTemplate + void before(Executable throwingCallable, Class clazz, String message) { + assertThrowsExactly(clazz, throwingCallable, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable, Class clazz, String message) { + assertThatThrownBy(throwingCallable).withFailMessage(message).isExactlyInstanceOf(clazz); + } + } + + static final class AssertThatThrownByWithFailMessageSupplierIsExactlyInstanceOf< + T extends Throwable> { + @BeforeTemplate + void before(Executable throwingCallable, Class clazz, Supplier supplier) { + assertThrowsExactly(clazz, throwingCallable, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable, Class clazz, Supplier supplier) { + assertThatThrownBy(throwingCallable).withFailMessage(supplier).isExactlyInstanceOf(clazz); + } + } + + static final class AssertThatThrownByIsInstanceOf { + @BeforeTemplate + void before(Executable throwingCallable, Class clazz) { + assertThrows(clazz, throwingCallable); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable, Class clazz) { + assertThatThrownBy(throwingCallable).isInstanceOf(clazz); + } + } + + static final class AssertThatThrownByWithFailMessageStringIsInstanceOf { + @BeforeTemplate + void before(Executable throwingCallable, Class clazz, String message) { + assertThrows(clazz, throwingCallable, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable, Class clazz, String message) { + assertThatThrownBy(throwingCallable).withFailMessage(message).isInstanceOf(clazz); + } + } + + static final class AssertThatThrownByWithFailMessageSupplierIsInstanceOf { + @BeforeTemplate + void before(Executable throwingCallable, Class clazz, Supplier supplier) { + assertThrows(clazz, throwingCallable, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable, Class clazz, Supplier supplier) { + assertThatThrownBy(throwingCallable).withFailMessage(supplier).isInstanceOf(clazz); + } + } + + static final class AssertThatCodeDoesNotThrowAnyException { + @BeforeTemplate + void before(Executable throwingCallable) { + assertDoesNotThrow(throwingCallable); + } + + @BeforeTemplate + void before(ThrowingSupplier throwingCallable) { + assertDoesNotThrow(throwingCallable); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable) { + assertThatCode(throwingCallable).doesNotThrowAnyException(); + } + } + + static final class AssertThatCodeWithFailMessageStringDoesNotThrowAnyException { + @BeforeTemplate + void before(Executable throwingCallable, String message) { + assertDoesNotThrow(throwingCallable, message); + } + + @BeforeTemplate + void before(ThrowingSupplier throwingCallable, String message) { + assertDoesNotThrow(throwingCallable, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable, String message) { + assertThatCode(throwingCallable).withFailMessage(message).doesNotThrowAnyException(); + } + } + + static final class AssertThatCodeWithFailMessageSupplierDoesNotThrowAnyException { + @BeforeTemplate + void before(Executable throwingCallable, Supplier supplier) { + assertDoesNotThrow(throwingCallable, supplier); + } + + @BeforeTemplate + void before(ThrowingSupplier throwingCallable, Supplier supplier) { + assertDoesNotThrow(throwingCallable, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(ThrowingCallable throwingCallable, Supplier supplier) { + assertThatCode(throwingCallable).withFailMessage(supplier).doesNotThrowAnyException(); + } + } + + static final class AssertThatIsInstanceOf { + @BeforeTemplate + void before(Object actual, Class clazz) { + assertInstanceOf(clazz, actual); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Class clazz) { + assertThat(actual).isInstanceOf(clazz); + } + } + + static final class AssertThatWithFailMessageStringIsInstanceOf { + @BeforeTemplate + void before(Object actual, Class clazz, String message) { + assertInstanceOf(clazz, actual, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Class clazz, String message) { + assertThat(actual).withFailMessage(message).isInstanceOf(clazz); + } + } + + static final class AssertThatWithFailMessageSupplierIsInstanceOf { + @BeforeTemplate + void before(Object actual, Class clazz, Supplier supplier) { + assertInstanceOf(clazz, actual, supplier); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Object actual, Class clazz, Supplier supplier) { + assertThat(actual).withFailMessage(supplier).isInstanceOf(clazz); + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java index 1129ca80f6..c60bb8b286 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java @@ -50,6 +50,7 @@ final class RefasterRulesTest { ImmutableSortedSetRules.class, IntStreamRules.class, JUnitRules.class, + JUnitToAssertJRules.class, LongStreamRules.class, MapEntryRules.class, MapRules.class, 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 new file mode 100644 index 0000000000..d1a68ad7a9 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestInput.java @@ -0,0 +1,173 @@ +package tech.picnic.errorprone.refasterrules; + +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; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Assertions; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class JUnitToAssertJRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of( + Assertions.class, + assertDoesNotThrow(() -> null), + assertInstanceOf(null, null), + assertThrows(null, null), + assertThrowsExactly(null, null), + (Runnable) () -> assertFalse(true), + (Runnable) () -> assertNotNull(null), + (Runnable) () -> assertNotSame(null, null), + (Runnable) () -> assertNull(null), + (Runnable) () -> assertSame(null, null), + (Runnable) () -> assertTrue(true)); + } + + void testThrowNewAssertionError() { + Assertions.fail(); + } + + Object testFailWithMessage() { + return Assertions.fail("foo"); + } + + Object testFailWithMessageAndThrowable() { + return Assertions.fail("foo", new IllegalStateException()); + } + + void testFailWithThrowable() { + Assertions.fail(new IllegalStateException()); + } + + void testAssertThatIsTrue() { + assertTrue(true); + } + + void testAssertThatWithFailMessageStringIsTrue() { + assertTrue(true, "foo"); + } + + void testAssertThatWithFailMessageSupplierIsTrue() { + assertTrue(true, () -> "foo"); + } + + void testAssertThatIsFalse() { + assertFalse(true); + } + + void testAssertThatWithFailMessageStringIsFalse() { + assertFalse(true, "foo"); + } + + void testAssertThatWithFailMessageSupplierIsFalse() { + assertFalse(true, () -> "foo"); + } + + void testAssertThatIsNull() { + assertNull(new Object()); + } + + void testAssertThatWithFailMessageStringIsNull() { + assertNull(new Object(), "foo"); + } + + void testAssertThatWithFailMessageSupplierIsNull() { + assertNull(new Object(), () -> "foo"); + } + + void testAssertThatIsNotNull() { + assertNotNull(new Object()); + } + + void testAssertThatWithFailMessageStringIsNotNull() { + assertNotNull(new Object(), "foo"); + } + + void testAssertThatWithFailMessageSupplierIsNotNull() { + assertNotNull(new Object(), () -> "foo"); + } + + void testAssertThatIsSameAs() { + assertSame("foo", "bar"); + } + + void testAssertThatWithFailMessageStringIsSameAs() { + assertSame("foo", "bar", "baz"); + } + + void testAssertThatWithFailMessageSupplierIsSameAs() { + assertSame("foo", "bar", () -> "baz"); + } + + void testAssertThatIsNotSameAs() { + assertNotSame("foo", "bar"); + } + + void testAssertThatWithFailMessageStringIsNotSameAs() { + assertNotSame("foo", "bar", "baz"); + } + + void testAssertThatWithFailMessageSupplierIsNotSameAs() { + assertNotSame("foo", "bar", () -> "baz"); + } + + void testAssertThatThrownByIsExactlyInstanceOf() { + assertThrowsExactly(IllegalStateException.class, () -> {}); + } + + void testAssertThatThrownByWithFailMessageStringIsExactlyInstanceOf() { + assertThrowsExactly(IllegalStateException.class, () -> {}, "foo"); + } + + void testAssertThatThrownByWithFailMessageSupplierIsExactlyInstanceOf() { + assertThrowsExactly(IllegalStateException.class, () -> {}, () -> "foo"); + } + + void testAssertThatThrownByIsInstanceOf() { + assertThrows(IllegalStateException.class, () -> {}); + } + + void testAssertThatThrownByWithFailMessageStringIsInstanceOf() { + assertThrows(IllegalStateException.class, () -> {}, "foo"); + } + + void testAssertThatThrownByWithFailMessageSupplierIsInstanceOf() { + assertThrows(IllegalStateException.class, () -> {}, () -> "foo"); + } + + void testAssertThatCodeDoesNotThrowAnyException() { + assertDoesNotThrow(() -> {}); + assertDoesNotThrow(() -> toString()); + } + + void testAssertThatCodeWithFailMessageStringDoesNotThrowAnyException() { + assertDoesNotThrow(() -> {}, "foo"); + assertDoesNotThrow(() -> toString(), "bar"); + } + + void testAssertThatCodeWithFailMessageSupplierDoesNotThrowAnyException() { + assertDoesNotThrow(() -> {}, () -> "foo"); + assertDoesNotThrow(() -> toString(), () -> "bar"); + } + + void testAssertThatIsInstanceOf() { + assertInstanceOf(Object.class, new Object()); + } + + void testAssertThatWithFailMessageStringIsInstanceOf() { + assertInstanceOf(Object.class, new Object(), "foo"); + } + + void testAssertThatWithFailMessageSupplierIsInstanceOf() { + 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 new file mode 100644 index 0000000000..c41d0246ac --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/JUnitToAssertJRulesTestOutput.java @@ -0,0 +1,183 @@ +package tech.picnic.errorprone.refasterrules; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +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; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.google.common.collect.ImmutableSet; +import org.junit.jupiter.api.Assertions; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class JUnitToAssertJRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of( + Assertions.class, + assertDoesNotThrow(() -> null), + assertInstanceOf(null, null), + assertThrows(null, null), + assertThrowsExactly(null, null), + (Runnable) () -> assertFalse(true), + (Runnable) () -> assertNotNull(null), + (Runnable) () -> assertNotSame(null, null), + (Runnable) () -> assertNull(null), + (Runnable) () -> assertSame(null, null), + (Runnable) () -> assertTrue(true)); + } + + void testThrowNewAssertionError() { + throw new AssertionError(); + } + + Object testFailWithMessage() { + return fail("foo"); + } + + Object testFailWithMessageAndThrowable() { + return fail("foo", new IllegalStateException()); + } + + void testFailWithThrowable() { + throw new AssertionError(new IllegalStateException()); + } + + void testAssertThatIsTrue() { + assertThat(true).isTrue(); + } + + void testAssertThatWithFailMessageStringIsTrue() { + assertThat(true).withFailMessage("foo").isTrue(); + } + + void testAssertThatWithFailMessageSupplierIsTrue() { + assertThat(true).withFailMessage(() -> "foo").isTrue(); + } + + void testAssertThatIsFalse() { + assertThat(true).isFalse(); + } + + void testAssertThatWithFailMessageStringIsFalse() { + assertThat(true).withFailMessage("foo").isFalse(); + } + + void testAssertThatWithFailMessageSupplierIsFalse() { + assertThat(true).withFailMessage(() -> "foo").isFalse(); + } + + void testAssertThatIsNull() { + assertThat(new Object()).isNull(); + } + + void testAssertThatWithFailMessageStringIsNull() { + assertThat(new Object()).withFailMessage("foo").isNull(); + } + + void testAssertThatWithFailMessageSupplierIsNull() { + assertThat(new Object()).withFailMessage(() -> "foo").isNull(); + } + + void testAssertThatIsNotNull() { + assertThat(new Object()).isNotNull(); + } + + void testAssertThatWithFailMessageStringIsNotNull() { + assertThat(new Object()).withFailMessage("foo").isNotNull(); + } + + void testAssertThatWithFailMessageSupplierIsNotNull() { + assertThat(new Object()).withFailMessage(() -> "foo").isNotNull(); + } + + void testAssertThatIsSameAs() { + assertThat("bar").isSameAs("foo"); + } + + void testAssertThatWithFailMessageStringIsSameAs() { + assertThat("bar").withFailMessage("baz").isSameAs("foo"); + } + + void testAssertThatWithFailMessageSupplierIsSameAs() { + assertThat("bar").withFailMessage(() -> "baz").isSameAs("foo"); + } + + void testAssertThatIsNotSameAs() { + assertThat("bar").isNotSameAs("foo"); + } + + void testAssertThatWithFailMessageStringIsNotSameAs() { + assertThat("bar").withFailMessage("baz").isNotSameAs("foo"); + } + + void testAssertThatWithFailMessageSupplierIsNotSameAs() { + assertThat("bar").withFailMessage(() -> "baz").isNotSameAs("foo"); + } + + void testAssertThatThrownByIsExactlyInstanceOf() { + assertThatThrownBy(() -> {}).isExactlyInstanceOf(IllegalStateException.class); + } + + void testAssertThatThrownByWithFailMessageStringIsExactlyInstanceOf() { + assertThatThrownBy(() -> {}) + .withFailMessage("foo") + .isExactlyInstanceOf(IllegalStateException.class); + } + + void testAssertThatThrownByWithFailMessageSupplierIsExactlyInstanceOf() { + assertThatThrownBy(() -> {}) + .withFailMessage(() -> "foo") + .isExactlyInstanceOf(IllegalStateException.class); + } + + void testAssertThatThrownByIsInstanceOf() { + assertThatThrownBy(() -> {}).isInstanceOf(IllegalStateException.class); + } + + void testAssertThatThrownByWithFailMessageStringIsInstanceOf() { + assertThatThrownBy(() -> {}).withFailMessage("foo").isInstanceOf(IllegalStateException.class); + } + + void testAssertThatThrownByWithFailMessageSupplierIsInstanceOf() { + assertThatThrownBy(() -> {}) + .withFailMessage(() -> "foo") + .isInstanceOf(IllegalStateException.class); + } + + void testAssertThatCodeDoesNotThrowAnyException() { + assertThatCode(() -> {}).doesNotThrowAnyException(); + assertThatCode(() -> toString()).doesNotThrowAnyException(); + } + + void testAssertThatCodeWithFailMessageStringDoesNotThrowAnyException() { + assertThatCode(() -> {}).withFailMessage("foo").doesNotThrowAnyException(); + assertThatCode(() -> toString()).withFailMessage("bar").doesNotThrowAnyException(); + } + + void testAssertThatCodeWithFailMessageSupplierDoesNotThrowAnyException() { + assertThatCode(() -> {}).withFailMessage(() -> "foo").doesNotThrowAnyException(); + assertThatCode(() -> toString()).withFailMessage(() -> "bar").doesNotThrowAnyException(); + } + + void testAssertThatIsInstanceOf() { + assertThat(new Object()).isInstanceOf(Object.class); + } + + void testAssertThatWithFailMessageStringIsInstanceOf() { + assertThat(new Object()).withFailMessage("foo").isInstanceOf(Object.class); + } + + void testAssertThatWithFailMessageSupplierIsInstanceOf() { + assertThat(new Object()).withFailMessage(() -> "foo").isInstanceOf(Object.class); + } +} diff --git a/pom.xml b/pom.xml index 830a7652ae..d3e7f2e673 100644 --- a/pom.xml +++ b/pom.xml @@ -686,10 +686,6 @@ - - -