forked from assertj/assertj
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add doesNot[Start/End]WithWhitespaces to CharSequenceAssert
Fix assertj#3433 Author: Lim Wonjae
- Loading branch information
1 parent
1d8484a
commit 3cf3900
Showing
8 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
assertj-core/src/main/java/org/assertj/core/error/ShouldNotEndWithWhitespaces.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ShouldNotEndWithWhitespaces extends BasicErrorMessageFactory { | ||
|
||
/** | ||
* Creates a new <code>{@link ShouldNotEndWithWhitespaces}</code>. | ||
* @param actual the actual value in the failed assertion. | ||
* @return the created {@code ErrorMessageFactory}. | ||
*/ | ||
public static ErrorMessageFactory shouldNotEndWithWhitespaces(CharSequence actual) { | ||
return new ShouldNotEndWithWhitespaces(actual); | ||
} | ||
|
||
private ShouldNotEndWithWhitespaces(Object actual) { | ||
super("%n" + | ||
"Expecting string not to end with whitespaces but found one, string was:%n" + | ||
" %s", actual); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
assertj-core/src/main/java/org/assertj/core/error/ShouldNotStartWithWhitespaces.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ShouldNotStartWithWhitespaces extends BasicErrorMessageFactory { | ||
|
||
/** | ||
* Creates a new <code>{@link ShouldNotStartWithWhitespaces}</code>. | ||
* @param actual the actual value in the failed assertion. | ||
* @return the created {@code ErrorMessageFactory}. | ||
*/ | ||
public static ErrorMessageFactory shouldNotStartWithWhitespaces(CharSequence actual) { | ||
return new ShouldNotStartWithWhitespaces(actual); | ||
} | ||
|
||
private ShouldNotStartWithWhitespaces(Object actual) { | ||
super("%n" + | ||
"Expecting string not to start with whitespaces but found one, string was:%n" + | ||
" %s", actual); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
.../org/assertj/core/api/charsequence/CharSequenceAssert_doesNotEndWithWhitespaces_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 static org.apache.commons.text.StringEscapeUtils.unescapeJava; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.assertj.core.api.charsequence.CharSequenceAssert_doesNotStartWithWhitespaces_Test.WHITESPACES; | ||
import static org.assertj.core.error.ShouldNotEndWithWhitespaces.shouldNotEndWithWhitespaces; | ||
import static org.assertj.core.util.AssertionsUtil.expectAssertionError; | ||
import static org.assertj.core.util.FailureMessages.actualIsNull; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
/** | ||
* @author Lim Wonjae | ||
*/ | ||
class CharSequenceAssert_doesNotEndWithWhitespaces_Test { | ||
|
||
@Test | ||
void should_fail_if_actual_is_null() { | ||
// GIVEN | ||
String actual = null; | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).doesNotStartWithWhitespaces()); | ||
// THEN | ||
then(assertionError).hasMessage(actualIsNull()); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
protected void should_pass_if_actual_does_not_end_with_whitespaces(String actual) { | ||
assertThat(actual).doesNotEndWithWhitespaces(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
protected void should_fail_if_actual_ends_with_whitespaces(String actual) { | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).doesNotEndWithWhitespaces()); | ||
// THEN | ||
then(assertionError).hasMessage(shouldNotEndWithWhitespaces(actual).create()); | ||
} | ||
|
||
static Stream<String> should_fail_if_actual_ends_with_whitespaces() { | ||
return Stream.of(WHITESPACES).map(whitespace -> unescapeJava("abc" + whitespace)); | ||
} | ||
|
||
static Stream<String> should_pass_if_actual_does_not_end_with_whitespaces() { | ||
return Stream.concat(Stream.of("<abc>", " ?", "\t\t\"", ""), | ||
Stream.of(WHITESPACES).map(whitespace -> unescapeJava(whitespace + "abc"))); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...rg/assertj/core/api/charsequence/CharSequenceAssert_doesNotStartWithWhitespaces_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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 static org.apache.commons.text.StringEscapeUtils.unescapeJava; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.assertj.core.error.ShouldNotStartWithWhitespaces.shouldNotStartWithWhitespaces; | ||
import static org.assertj.core.util.AssertionsUtil.expectAssertionError; | ||
import static org.assertj.core.util.FailureMessages.actualIsNull; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
/** | ||
* @author Lim Wonjae | ||
*/ | ||
class CharSequenceAssert_doesNotStartWithWhitespaces_Test { | ||
|
||
// java whitespace according to Character.html#isWhitespace(int) | ||
static final String[] WHITESPACES = { | ||
"\\u0020", | ||
" ", | ||
"\t ", | ||
"\\u0009", | ||
"\n", | ||
"\\u000A", | ||
"\f", | ||
"\\u000C", | ||
"\r", | ||
"\\u000D", | ||
"\r \t", | ||
"\\u001C", | ||
"\\u001C", | ||
"\\u001D", | ||
"\\u001E", | ||
"\\u001F", | ||
"\\u000B", // VERTICAL TABULATION. | ||
"\\u1680", | ||
"\\u2000", | ||
"\\u2001", | ||
"\\u2002", | ||
"\\u2003", | ||
"\\u2004", | ||
"\\u2005", | ||
"\\u2006", | ||
"\\u2008", | ||
"\\u2009", | ||
"\\u200A", | ||
"\\u205F", | ||
"\\u2028", // LINE SEPARATOR | ||
"\\u2029", // PARAGRAPH SEPARATOR | ||
"\\u3000" | ||
}; | ||
|
||
@Test | ||
void should_fail_if_actual_is_null() { | ||
// GIVEN | ||
String actual = null; | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).doesNotStartWithWhitespaces()); | ||
// THEN | ||
then(assertionError).hasMessage(actualIsNull()); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
protected void should_pass_if_actual_does_not_start_with_whitespaces(String actual) { | ||
assertThat(actual).doesNotStartWithWhitespaces(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
protected void should_fail_if_actual_starts_with_whitespaces(String actual) { | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).doesNotStartWithWhitespaces()); | ||
// THEN | ||
then(assertionError).hasMessage(shouldNotStartWithWhitespaces(actual).create()); | ||
} | ||
|
||
static Stream<String> should_fail_if_actual_starts_with_whitespaces() { | ||
return Stream.of(WHITESPACES).map(whitespace -> unescapeJava(whitespace + "abc")); | ||
} | ||
|
||
static Stream<String> should_pass_if_actual_does_not_start_with_whitespaces() { | ||
return Stream.concat(Stream.of("<abc>", "? ", ""), | ||
Stream.of(WHITESPACES).map(whitespace -> unescapeJava("abc" + whitespace))); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...tj-core/src/test/java/org/assertj/core/error/ShouldNotEndWithWhitespaces_create_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.ShouldNotEndWithWhitespaces.shouldNotEndWithWhitespaces; | ||
|
||
import org.assertj.core.description.TextDescription; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* author: Lim Wonjae | ||
*/ | ||
class ShouldNotEndWithWhitespaces_create_Test { | ||
|
||
@Test | ||
void should_create_error_message() { | ||
// GIVEN | ||
ErrorMessageFactory factory = shouldNotEndWithWhitespaces("abc %d"); | ||
// WHEN | ||
String message = factory.create(new TextDescription("Test")); | ||
// THEN | ||
then(message).isEqualTo(String.format("[Test] %n" + | ||
"Expecting string not to end with whitespaces but found one, string was:%n" + | ||
" \"abc %%d\"")); | ||
} | ||
|
||
} |
Oops, something went wrong.