Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doesNot[Start/End]WithWhitespace methods to CharSequence assertions #3441

Closed
wants to merge 7 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import static org.assertj.core.error.ShouldNotBeBlank.shouldNotBeBlank;
import static org.assertj.core.error.ShouldNotContainAnyWhitespaces.shouldNotContainAnyWhitespaces;
import static org.assertj.core.error.ShouldNotContainOnlyWhitespaces.shouldNotContainOnlyWhitespaces;
import static org.assertj.core.error.ShouldNotEndWithWhitespace.shouldNotEndWithWhitespace;
import static org.assertj.core.error.ShouldNotStartWithWhitespace.shouldNotStartWithWhitespace;
import static org.assertj.core.internal.Strings.doCommonCheckForCharSequence;
import static org.assertj.core.internal.Strings.removeAllWhitespaces;
import static org.assertj.core.util.IterableUtil.toArray;
Expand Down Expand Up @@ -2153,6 +2155,67 @@
return myself;
}

/**
* Verifies that the actual {@code CharSequence} does not start with whitespace.
* <p>
* Example:
* <pre><code class='java'>
* // assertions will pass
* assertThat(&quot;abc&quot;).doesNotStartWithWhitespace();
* assertThat(&quot;abc &quot;).doesNotStartWithWhitespace();
* assertThat(&quot;abc\t\t&quot;).doesNotStartWithWhitespace();
*
* // assertions will fail
* assertThat(&quot; abc&quot;).doesNotStartWithWhitespace();
* assertThat(&quot; abc &quot;).doesNotStartWithWhitespace();
* assertThat(&quot;\r\nabc&quot;).doesNotStartWithWhitespace();
* </code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code CharSequence} starts with whitespace.
* @see Character#isWhitespace(char) to know what are the whitespaces.
* @since 3.26.0
*/
public SELF doesNotStartWithWhitespace() {
assertDoesNotStartWithWhitespace(actual);
return myself;
}

private void assertDoesNotStartWithWhitespace(CharSequence actual) {
if (Character.isWhitespace(actual.codePoints().findFirst().getAsInt())) throwAssertionError(shouldNotStartWithWhitespace(actual));

Check warning on line 2185 in assertj-core/src/main/java/org/assertj/core/api/AbstractCharSequenceAssert.java

View workflow job for this annotation

GitHub Actions / Scan

Optional.get() is called without isPresent() check

`OptionalInt.getAsInt()` without 'isPresent()' check
}

/**
* Verifies that the actual {@code CharSequence} does not end with whitespace.
* <p>
* Example:
* <pre><code class='java'>
* // assertions will pass
* assertThat(&quot;abc&quot;).doesNotEndWithWhitespace();
* assertThat(&quot; abc&quot;).doesNotEndWithWhitespace();
* assertThat(&quot;\t\tabc&quot;).doesNotEndWithWhitespace();
*
* // assertions will fail
* assertThat(&quot;abc &quot;).doesNotEndWithWhitespace();
* assertThat(&quot; abc &quot;).doesNotEndWithWhitespace();
* assertThat(&quot;abc\r\n&quot;).doesNotEndWithWhitespace();
* </code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code CharSequence} ends with whitespace.
* @see Character#isWhitespace(char) to know what are the whitespaces.
* @since 3.26.0
*/
public SELF doesNotEndWithWhitespace() {
assertDoesNotEndWithWhitespace(actual);
return myself;
}

private void assertDoesNotEndWithWhitespace(CharSequence actual) {

if (Character.isWhitespace(actual.codePoints().reduce((v1, v2) -> v2).getAsInt())) throwAssertionError(shouldNotEndWithWhitespace(actual));

Check warning on line 2216 in assertj-core/src/main/java/org/assertj/core/api/AbstractCharSequenceAssert.java

View workflow job for this annotation

GitHub Actions / Scan

Optional.get() is called without isPresent() check

`OptionalInt.getAsInt()` without 'isPresent()' check
}

private static boolean isBlank(CharSequence actual) {
return isNullOrEmpty(actual) || strictlyContainsWhitespaces(actual);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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-2024 the original author or authors.
*/
package org.assertj.core.error;

/**
* Creates an error message indicating that an assertion that verifies that a {@link CharSequence}
* does not end with whitespace characters.
*/
public class ShouldNotEndWithWhitespace extends BasicErrorMessageFactory {

/**
* Creates a new <code>{@link ShouldNotEndWithWhitespace}</code>.
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldNotEndWithWhitespace(CharSequence actual) {
return new ShouldNotEndWithWhitespace(actual);
}

private ShouldNotEndWithWhitespace(Object actual) {
super("%n" +
"Expecting string not to end with whitespace but found one, string was:%n" +
" %s", actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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-2024 the original author or authors.
*/
package org.assertj.core.error;

/**
* Creates an error message indicating that an assertion that verifies that a {@link CharSequence}
* does not start with whitespace characters.
*/
public class ShouldNotStartWithWhitespace extends BasicErrorMessageFactory {

/**
* Creates a new <code>{@link ShouldNotStartWithWhitespace}</code>.
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldNotStartWithWhitespace(CharSequence actual) {
return new ShouldNotStartWithWhitespace(actual);
}

private ShouldNotStartWithWhitespace(Object actual) {
super("%n" +
"Expecting string not to start with whitespace but found one, string was:%n" +
" %s", actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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-2024 the original author or authors.
*/
package org.assertj.core.api.charsequence;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldNotEndWithWhitespace.shouldNotEndWithWhitespace;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

/**
* @author Lim Wonjae
*/
class CharSequenceAssert_doesNotEndWithWhitespace_Test {

@ParameterizedTest
@ValueSource(strings = {"abc", " ?", " ab", "\t\t\""})
protected void should_pass_if_actual_does_not_end_with_whitespace(String actual) {
//When + Then
assertThat(actual).doesNotEndWithWhitespace();
}

@ParameterizedTest
@ValueSource(strings = {"abc ", "abc\t", "abc\n", "abc\r"})
protected void should_fail_if_actual_ends_with_whitespace(String actual) {
//When
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).doesNotEndWithWhitespace());
//Then
then(assertionError).hasMessage(shouldNotEndWithWhitespace(actual).create());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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-2024 the original author or authors.
*/
package org.assertj.core.api.charsequence;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldNotStartWithWhitespace.shouldNotStartWithWhitespace;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

/**
* @author Lim Wonjae
*/
class CharSequenceAssert_doesNotStartWithWhitespace_Test {

@ParameterizedTest
@ValueSource(strings = {"abc", "? ", "ab ", "\"\t\t"})
protected void should_pass_if_actual_does_not_start_with_whitespace(String actual) {
//When + Then
assertThat(actual).doesNotStartWithWhitespace();
}

@ParameterizedTest
@ValueSource(strings = {" abc", "\tabc ", "\nabc", "\rabc"})
protected void should_fail_if_actual_starts_with_whitespace(String actual) {
//When
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).doesNotStartWithWhitespace());
//Then
then(assertionError).hasMessage(shouldNotStartWithWhitespace(actual).create());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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-2024 the original author or authors.
*/
package org.assertj.core.error;

import org.assertj.core.description.TextDescription;
import org.assertj.core.presentation.StandardRepresentation;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldNotEndWithWhitespace.shouldNotEndWithWhitespace;

/**
* author: Lim Wonjae
*/
class ShouldNotEndWithWhitespace_create_Test {

@Test
void should_create_error_message() {
//Given
ErrorMessageFactory factory = shouldNotEndWithWhitespace("abc");

//When
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());

//Then
then(message).isEqualTo(String.format("[Test] " + "%n" +
"Expecting string not to end with whitespace but found one, string was:%n" +
" \"%s\"", "abc"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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-2024 the original author or authors.
*/
package org.assertj.core.error;

import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldNotStartWithWhitespace.shouldNotStartWithWhitespace;

import org.assertj.core.description.TextDescription;
import org.assertj.core.presentation.StandardRepresentation;
import org.junit.jupiter.api.Test;

/**
* author: Lim Wonjae
*/
class ShouldNotStartWithWhitespace_create_Test {

@Test
void should_create_error_message() {
//Given
ErrorMessageFactory factory = shouldNotStartWithWhitespace("abc");

//When
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());

//Then
then(message).isEqualTo(String.format("[Test] " + "%n" +
"Expecting string not to start with whitespace but found one, string was:%n" +
" \"%s\"", "abc"));
}
}