-
-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hasPermittedSubclasses and hasNoPermittedSubclasses for Class ass…
…ertions - Fix #3263 Co-authored-by: Stefano Cordio <stefano_cordio@epam.com>
- Loading branch information
1 parent
2dac678
commit a0cb89b
Showing
8 changed files
with
292 additions
and
32 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
35 changes: 35 additions & 0 deletions
35
assertj-core/src/main/java/org/assertj/core/error/ShouldHavePermittedSubclasses.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; | ||
|
||
import java.util.Collection; | ||
|
||
public class ShouldHavePermittedSubclasses extends BasicErrorMessageFactory { | ||
|
||
public static ErrorMessageFactory shouldHavePermittedSubclasses(Class<?> actual, | ||
Class<?>[] permittedSubclasses, | ||
Collection<Class<?>> missingSubclasses) { | ||
return new ShouldHavePermittedSubclasses(actual, permittedSubclasses, missingSubclasses); | ||
} | ||
|
||
private ShouldHavePermittedSubclasses(Class<?> actual, Class<?>[] permittedSubclasses, Collection<Class<?>> missingSubclasses) { | ||
super("%n" + | ||
"Expecting%n" + | ||
" %s%n" + | ||
"to have these permitted subclasses:%n" + | ||
" %s%n" + | ||
"but the following ones were not found:%n" + | ||
" %s", | ||
actual, permittedSubclasses, missingSubclasses); | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
...e/src/test/java/org/assertj/core/api/classes/ClassAssert_hasPermittedSubclasses_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,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.classes; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.assertj.core.error.ShouldHavePermittedSubclasses.shouldHavePermittedSubclasses; | ||
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull; | ||
import static org.assertj.core.util.Arrays.array; | ||
import static org.assertj.core.util.AssertionsUtil.expectAssertionError; | ||
import static org.assertj.core.util.Lists.list; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ClassAssert_hasPermittedSubclasses_Test { | ||
|
||
@Test | ||
void should_fail_if_actual_is_null() { | ||
// GIVEN | ||
Class<?> actual = null; | ||
// WHEN | ||
AssertionError error = expectAssertionError(() -> assertThat(actual).hasPermittedSubclasses()); | ||
// THEN | ||
then(error).hasMessage(shouldNotBeNull().create()); | ||
} | ||
|
||
@Test | ||
void should_fail_if_actual_does_not_have_permitted_subclasses() { | ||
// WHEN | ||
AssertionError error = expectAssertionError(() -> assertThat(Object.class).hasPermittedSubclasses(String.class)); | ||
// THEN | ||
then(error).hasMessage(shouldHavePermittedSubclasses(Object.class, array(String.class), list(String.class)).create()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...-core/src/test/java/org/assertj/core/error/ShouldHavePermittedSubclasses_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,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.error; | ||
|
||
import static java.lang.String.format; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.assertj.core.error.ShouldHavePermittedSubclasses.shouldHavePermittedSubclasses; | ||
import static org.assertj.core.util.Arrays.array; | ||
import static org.assertj.core.util.Lists.list; | ||
|
||
import org.assertj.core.description.TextDescription; | ||
import org.assertj.core.presentation.StandardRepresentation; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ShouldHavePermittedSubclasses_create_Test { | ||
|
||
@Test | ||
void should_create_error_message() { | ||
// GIVEN | ||
ErrorMessageFactory factory = shouldHavePermittedSubclasses(ShouldHavePermittedSubclasses_create_Test.class, | ||
array(String.class, Number.class), | ||
list(Number.class)); | ||
// WHEN | ||
String message = factory.create(new TextDescription("Test"), new StandardRepresentation()); | ||
// THEN | ||
then(message).isEqualTo(format("[Test] %n" + | ||
"Expecting%n" + | ||
" org.assertj.core.error.ShouldHavePermittedSubclasses_create_Test%n" + | ||
"to have these permitted subclasses:%n" + | ||
" [java.lang.String, java.lang.Number]%n" + | ||
"but the following ones were not found:%n" + | ||
" [java.lang.Number]")); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...tests/assertj-core-java-17/src/test/java/org/assertj/core/tests/java17/AssertionUtil.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,26 @@ | ||
/* | ||
* 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.tests.java17; | ||
|
||
import org.assertj.core.api.ThrowableAssert; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.catchThrowableOfType; | ||
|
||
public class AssertionUtil { | ||
public static AssertionError expectAssertionError(ThrowableAssert.ThrowingCallable shouldRaiseAssertionError) { | ||
AssertionError error = catchThrowableOfType(shouldRaiseAssertionError, AssertionError.class); | ||
assertThat(error).as("The code under test should have raised an AssertionError").isNotNull(); | ||
return error; | ||
} | ||
} |
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
Oops, something went wrong.