-
-
Notifications
You must be signed in to change notification settings - Fork 690
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add isPrimitive and isNotPrimitive Class assertions
- Loading branch information
1 parent
3fbb1d0
commit 98e3acd
Showing
8 changed files
with
320 additions
and
3 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
36 changes: 36 additions & 0 deletions
36
assertj-core/src/main/java/org/assertj/core/error/ShouldBePrimitive.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,36 @@ | ||
/* | ||
* 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.error; | ||
|
||
/** | ||
* Creates an error message indicating that an assertion that verifies that a class is (or not) a primitive data type, | ||
* i.e, byte, short, int, long, float, double, char and boolean. | ||
* | ||
* @author Manuel Gutierrez | ||
*/ | ||
public class ShouldBePrimitive extends BasicErrorMessageFactory { | ||
|
||
/** | ||
* Creates a new instance of <code>{@link ShouldBePrimitive }</code>. | ||
* | ||
* @param actual the actual value in the failed assertion. | ||
* @return the created {@code ErrorMessageFactory}. | ||
*/ | ||
public static BasicErrorMessageFactory shouldBePrimitive(Class<?> actual) { | ||
return new ShouldBePrimitive(actual); | ||
} | ||
|
||
private ShouldBePrimitive(Class<?> actual) { | ||
super("%nExpecting %s to be a primitive type", actual); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
assertj-core/src/main/java/org/assertj/core/error/ShouldNotBePrimitive.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,36 @@ | ||
/* | ||
* 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.error; | ||
|
||
/** | ||
* Creates an error message indicating that an assertion that verifies that a class is (or not) a primitive data type, | ||
* i.e, byte, short, int, long, float, double, char and boolean. | ||
* | ||
* @author Manuel Gutierrez | ||
*/ | ||
public class ShouldNotBePrimitive extends BasicErrorMessageFactory { | ||
|
||
/** | ||
* Creates a new instance of <code>{@link ShouldNotBePrimitive}</code>. | ||
* | ||
* @param actual the actual value in the failed assertion. | ||
* @return the created {@code ErrorMessageFactory}. | ||
*/ | ||
public static BasicErrorMessageFactory shouldNotBePrimitive(Class<?> actual) { | ||
return new ShouldNotBePrimitive(actual); | ||
} | ||
|
||
private ShouldNotBePrimitive(Class<?> actual) { | ||
super("%nExpecting %s not to be a primitive type", actual); | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
assertj-core/src/test/java/org/assertj/core/api/classes/ClassAssert_isNotPrimitive_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,60 @@ | ||
/* | ||
* 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.classes; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.assertj.core.error.ShouldNotBePrimitive.shouldNotBePrimitive; | ||
import static org.assertj.core.util.AssertionsUtil.expectAssertionError; | ||
import static org.assertj.core.util.FailureMessages.actualIsNull; | ||
|
||
import org.assertj.core.api.ClassAssert; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
/** | ||
* Tests for <code>{@link ClassAssert#isNotPrimitive()}</code>. | ||
* | ||
* @author Manuel Gutierrez | ||
*/ | ||
class ClassAssert_isNotPrimitive_Test { | ||
|
||
@Test | ||
void should_pass_if_actual_is_an_object() { | ||
// GIVEN | ||
Class<?> actual = Object.class; | ||
// WHEN/THEN | ||
then(actual).isNotPrimitive(); | ||
} | ||
|
||
@Test | ||
void should_fail_if_actual_is_null() { | ||
// GIVEN | ||
Class<?> actual = null; | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isNotPrimitive()); | ||
// THEN | ||
then(assertionError).hasMessage(actualIsNull()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(classes = { byte.class, short.class, int.class, long.class, float.class, double.class, boolean.class, | ||
char.class }) | ||
void should_fail_if_actual_is_a_primitive_type(Class<?> primitiveClass) { | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(primitiveClass).isNotPrimitive()); | ||
// THEN | ||
then(assertionError).hasMessage(shouldNotBePrimitive(primitiveClass).create()); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
assertj-core/src/test/java/org/assertj/core/api/classes/ClassAssert_isPrimitive_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,59 @@ | ||
/* | ||
* 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.classes; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.assertj.core.error.ShouldBePrimitive.shouldBePrimitive; | ||
import static org.assertj.core.util.AssertionsUtil.expectAssertionError; | ||
import static org.assertj.core.util.FailureMessages.actualIsNull; | ||
|
||
import org.assertj.core.api.ClassAssert; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
/** | ||
* Tests for <code>{@link ClassAssert#isPrimitive()} ()}</code>. | ||
* | ||
* @author Manuel Gutierrez | ||
*/ | ||
class ClassAssert_isPrimitive_Test { | ||
|
||
@Test | ||
void should_pass_if_actual_is_an_object() { | ||
// GIVEN | ||
Class<?> actual = Object.class; | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isPrimitive()); | ||
// THEN | ||
then(assertionError).hasMessage(shouldBePrimitive(actual).create()); | ||
} | ||
|
||
@Test | ||
void should_fail_if_actual_is_null() { | ||
// GIVEN | ||
Class<?> actual = null; | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isPrimitive()); | ||
// THEN | ||
then(assertionError).hasMessage(actualIsNull()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(classes = { byte.class, short.class, int.class, long.class, float.class, double.class, boolean.class, | ||
char.class }) | ||
void should_pass_if_actual_is_a_primitive_type(Class<?> primitiveClass) { | ||
assertThat(primitiveClass).isPrimitive(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
assertj-core/src/test/java/org/assertj/core/error/ShouldBePrimitive_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,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-2023 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.ShouldBePrimitive.shouldBePrimitive; | ||
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION; | ||
|
||
import org.assertj.core.description.TextDescription; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ShouldBePrimitive_create_Test { | ||
|
||
@Test | ||
void should_create_error_message() { | ||
// GIVEN | ||
ErrorMessageFactory factory = shouldBePrimitive(Object.class); | ||
// WHEN | ||
String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION); | ||
// THEN | ||
then(message).isEqualTo(format("[Test] %nExpecting java.lang.Object to be a primitive type")); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
assertj-core/src/test/java/org/assertj/core/error/ShouldNotBePrimitive_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,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-2023 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.ShouldNotBePrimitive.shouldNotBePrimitive; | ||
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION; | ||
|
||
import org.assertj.core.description.TextDescription; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ShouldNotBePrimitive_create_Test { | ||
|
||
@Test | ||
void should_create_error_message() { | ||
// GIVEN | ||
ErrorMessageFactory factory = shouldNotBePrimitive(int.class); | ||
// WHEN | ||
String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION); | ||
// THEN | ||
then(message).isEqualTo(format("[Test] %nExpecting int not to be a primitive type")); | ||
} | ||
|
||
} |