Skip to content

Commit

Permalink
Rewrite another `ThrowableAssertAlternative#withMessage(String, Objec…
Browse files Browse the repository at this point in the history
…t...)` expression (#190)
  • Loading branch information
ivan-fedorov authored Aug 17, 2022
1 parent 9bc732b commit c39d125
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,32 @@ static final class AssertThatThrownByHasMessage {
}
}

static final class AssertThatThrownByHasMessageParameters {
@BeforeTemplate
@SuppressWarnings("AssertThatThrownBy" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(
Class<? extends Throwable> exceptionType,
ThrowingCallable throwingCallable,
String message,
@Repeated Object parameters) {
return assertThatExceptionOfType(exceptionType)
.isThrownBy(throwingCallable)
.withMessage(message, parameters);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(
Class<? extends Throwable> exceptionType,
ThrowingCallable throwingCallable,
String message,
@Repeated Object parameters) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(exceptionType)
.hasMessage(message, parameters);
}
}

// XXX: Drop this template in favour of a generic Error Prone check which flags
// `String.format(...)` arguments to a wide range of format methods.
static final class AbstractThrowableAssertHasMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
return assertThatIOException().isThrownBy(() -> {}).withMessage("foo");
}

@SuppressWarnings("AssertThatThrownByIOException")
AbstractObjectAssert<?, ?> testAssertThatThrownByIOExceptionHasMessageParameters() {
return assertThatIOException().isThrownBy(() -> {}).withMessage("foo %s", "bar");
}
Expand All @@ -114,6 +113,12 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
.withMessage("foo");
}

AbstractObjectAssert<?, ?> testAssertThatThrownByHasMessageParameters() {
return assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> {})
.withMessage("foo %s", "bar");
}

ImmutableSet<AbstractThrowableAssert<?, ? extends Throwable>>
testAbstractThrowableAssertHasMessage() {
return ImmutableSet.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
return assertThatThrownBy(() -> {}).isInstanceOf(IOException.class).hasMessage("foo");
}

@SuppressWarnings("AssertThatThrownByIOException")
AbstractObjectAssert<?, ?> testAssertThatThrownByIOExceptionHasMessageParameters() {
return assertThatThrownBy(() -> {}).isInstanceOf(IOException.class).hasMessage("foo %s", "bar");
}
Expand All @@ -135,6 +134,12 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
.hasMessage("foo");
}

AbstractObjectAssert<?, ?> testAssertThatThrownByHasMessageParameters() {
return assertThatThrownBy(() -> {})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("foo %s", "bar");
}

ImmutableSet<AbstractThrowableAssert<?, ? extends Throwable>>
testAbstractThrowableAssertHasMessage() {
return ImmutableSet.of(
Expand Down

0 comments on commit c39d125

Please sign in to comment.