Skip to content

Commit

Permalink
Rewrite Assertions.assertInstanceOf
Browse files Browse the repository at this point in the history
  • Loading branch information
giall committed Dec 15, 2022
1 parent df92cad commit 45406fc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -446,5 +447,43 @@ void after(ThrowingCallable runnable, Supplier<String> 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<T> {
@BeforeTemplate
void before(Object object, Class<T> clazz) {
assertInstanceOf(clazz, object);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object object, Class<T> clazz) {
assertThat(object).isInstanceOf(clazz);
}
}

static final class AssertInstanceOfWithMessage<T> {
@BeforeTemplate
void before(Object object, Class<T> clazz, String message) {
assertInstanceOf(clazz, object, message);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object object, Class<T> clazz, String message) {
assertThat(object).withFailMessage(message).isInstanceOf(clazz);
}
}

static final class AssertInstanceOfWithMessageSupplier<T> {
@BeforeTemplate
void before(Object object, Class<T> clazz, Supplier<String> messageSupplier) {
assertInstanceOf(clazz, object, messageSupplier);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object object, Class<T> clazz, Supplier<String> messageSupplier) {
assertThat(object).withFailMessage(messageSupplier).isInstanceOf(clazz);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 45406fc

Please sign in to comment.