Skip to content

Commit

Permalink
Add varargs overload to Assert#hasString and Assert#doesNotHaveString
Browse files Browse the repository at this point in the history
This overload will delegate to String#format internally, similar to
how StringAssert#isEqualTo allows for string templating expansion.
This removes the need to write manual formatting in tests that
need to consume a fixture placeholder.
  • Loading branch information
ascopes committed Feb 4, 2023
1 parent e2f1831 commit 78c90b5
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,27 @@ public SELF hasToString(String expectedToString) {
return myself;
}

/** {@inheritDoc} */
@Override
public SELF hasToString(String expectedStringTemplate, Object... args) {
requireNonNull(expectedStringTemplate, "The expectedStringTemplate must not be null");
return hasToString(String.format(expectedStringTemplate, args));
}

/** {@inheritDoc} */
@Override
public SELF doesNotHaveToString(String otherToString) {
objects.assertDoesNotHaveToString(info, actual, otherToString);
return myself;
}

/** {@inheritDoc} */
@Override
public SELF doesNotHaveToString(String expectedStringTemplate, Object... args) {
requireNonNull(expectedStringTemplate, "The expectedStringTemplate must not be null");
return doesNotHaveToString(String.format(expectedStringTemplate, args));
}

/** {@inheritDoc} */
@Override
public SELF doesNotHaveSameClassAs(Object other) {
Expand Down
38 changes: 38 additions & 0 deletions assertj-core/src/main/java/org/assertj/core/api/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,25 @@ public interface Assert<SELF extends Assert<SELF, ACTUAL>, ACTUAL> extends Descr
*/
SELF hasToString(String expectedToString);

/**
* Verifies that actual {@code actual.toString()} is equal to the given {@code String} when
* {@link String#format formatted} with the given arguments.
* <p>
* Example :
* <pre><code class='java'> Foo foo = new Foo();
* FooWrapper wrapper = new FooWrapper(foo);
*
* assertThat(wrapper).hasToString("FooWrapper[foo=%s]", foo);
* </code></pre>
*
* @param expectedStringTemplate the format string to use.
* @param args the arguments to interpolate into the format string.
* @return this assertion object.
* @throws AssertionError if {@code actual.toString()} result is not equal to the given {@code String}.
* @throws AssertionError if actual is {@code null}.
*/
SELF hasToString(String expectedStringTemplate, Object... args);

/**
* Verifies that actual {@code actual.toString()} is not equal to the given {@code String}.
* <p>
Expand All @@ -490,6 +509,25 @@ public interface Assert<SELF extends Assert<SELF, ACTUAL>, ACTUAL> extends Descr
*/
SELF doesNotHaveToString(String otherToString);

/**
* Verifies that actual {@code actual.toString()} is not equal to the given {@code String}.
* <p>
* Example :
* <pre><code class='java'> Foo foo = new Foo();
* Bar bar = new Bar();
* FooBarWrapper wrapper = new FooBarWrapper(bar);
*
* assertThat(wrapper).doesNotHaveToString("FooBarWrapper[%s]", foo);
* </code></pre>
*
* @param expectedStringTemplate the format string to use.
* @param args the arguments to interpolate into the format string.
* @return this assertion object.
* @throws AssertionError if {@code actual.toString()} result is equal to the given {@code String}.
* @throws AssertionError if actual is {@code null}.
*/
SELF doesNotHaveToString(String expectedStringTemplate, Object... args);

/**
* Verifies that the actual value does not have the same class as the given object.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2023 the original author or authors.
*/
package org.assertj.core.api.abstract_;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;

import org.assertj.core.api.AbstractAssertBaseTest;
import org.assertj.core.api.ConcreteAssert;
import org.junit.jupiter.api.Test;

/**
* @author Ashley Scopes
*/
class AbstractAssert_doesNotHaveToString_format_Test extends AbstractAssertBaseTest {

@Override
protected ConcreteAssert invoke_api_method() {
return assertions.doesNotHaveToString("%s %s", "some", "description");
}

@Override
protected void verify_internal_effects() {
verify(objects).assertDoesNotHaveToString(getInfo(assertions), getActual(assertions), "some description");
}

@Test
void nullStringArgumentsThrowAnException() {
assertThatThrownBy(() -> assertions.doesNotHaveToString(null, "foo", "bar"))
.isInstanceOf(NullPointerException.class)
.hasMessage("The expectedStringTemplate must not be null");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2023 the original author or authors.
*/
package org.assertj.core.api.abstract_;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;

import org.assertj.core.api.AbstractAssertBaseTest;
import org.assertj.core.api.ConcreteAssert;
import org.junit.jupiter.api.Test;

/**
* {@author Ashley Scopes}
*/
class AbstractAssert_hasToString_format_Test extends AbstractAssertBaseTest {

@Override
protected ConcreteAssert invoke_api_method() {
return assertions.hasToString("%s %s", "some", "description");
}

@Override
protected void verify_internal_effects() {
verify(objects).assertHasToString(getInfo(assertions), getActual(assertions), "some description");
}

@Test
void nullStringArgumentsThrowAnException() {
assertThatThrownBy(() -> assertions.hasToString(null, "foo", "bar"))
.isInstanceOf(NullPointerException.class)
.hasMessage("The expectedStringTemplate must not be null");
}
}

0 comments on commit 78c90b5

Please sign in to comment.