Skip to content

Commit

Permalink
feat: add isPrimitive and isNotPrimitive Class assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Gutierrez authored and joel-costigliola committed Sep 30, 2023
1 parent 3fbb1d0 commit 98e3acd
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.assertj.core.error.ShouldBeAssignableTo.shouldBeAssignableTo;
import static org.assertj.core.error.ShouldBeInterface.shouldBeInterface;
import static org.assertj.core.error.ShouldBeInterface.shouldNotBeInterface;
import static org.assertj.core.error.ShouldBePrimitive.shouldBePrimitive;
import static org.assertj.core.error.ShouldBeRecord.shouldBeRecord;
import static org.assertj.core.error.ShouldBeRecord.shouldNotBeRecord;
import static org.assertj.core.error.ShouldBeSealed.shouldBeSealed;
Expand All @@ -36,6 +37,7 @@
import static org.assertj.core.error.ShouldHaveRecordComponents.shouldHaveRecordComponents;
import static org.assertj.core.error.ShouldHaveSuperclass.shouldHaveSuperclass;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.error.ShouldNotBePrimitive.shouldNotBePrimitive;
import static org.assertj.core.util.Arrays.array;
import static org.assertj.core.util.Sets.newLinkedHashSet;

Expand Down Expand Up @@ -1164,4 +1166,59 @@ private static boolean isSealed(Class<?> actual) {
}
}

/**
* Verifies that the actual {@code Class} is a primitive type.
* <p>
* Example:
* <pre><code class='java'> // these assertions succeed:
* assertThat(byte.class).isPrimitive();
* assertThat(short.class).isPrimitive();
* assertThat(int.class).isPrimitive();
* assertThat(long.class).isPrimitive();
* assertThat(float.class).isPrimitive();
* assertThat(double.class).isPrimitive();
* assertThat(boolean.class).isPrimitive();
* assertThat(char.class).isPrimitive();
*
* // this assertion fails as Object is not a primitive type:
* assertThat(Object.class).isPrimitive(); </code></pre>
*
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} is not a primitive type.
* @see Class#isPrimitive()
*/
public SELF isPrimitive() {
isNotNull();
if (!actual.isPrimitive()) throw assertionError(shouldBePrimitive(actual));
return myself;
}

/**
* Verifies that the actual {@code Class} is not a primitive type.
* <p>
* Example:
* <pre><code class='java'> // this assertion succeeds as Object is not a primitive type:
* assertThat(Object.class).isNotPrimitive();
*
* // these assertions fail:
* assertThat(byte.class).isNotPrimitive();
* assertThat(short.class).isNotPrimitive();
* assertThat(int.class).isNotPrimitive();
* assertThat(long.class).isNotPrimitive();
* assertThat(float.class).isNotPrimitive();
* assertThat(double.class).isNotPrimitive();
* assertThat(boolean.class).isNotPrimitive();
* assertThat(char.class).isNotPrimitive(); </code></pre>
*
* @return {@code this} assertions object
* @throws AssertionError if {@code actual} is {@code null}.
* @throws AssertionError if the actual {@code Class} is a primitive type.
* @see Class#isPrimitive()
*/
public SELF isNotPrimitive() {
isNotNull();
if (actual.isPrimitive()) throw assertionError(shouldNotBePrimitive(actual));
return myself;
}
}
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public static Classes instance() {
return INSTANCE;
}

private Failures failures = Failures.instance();
private ComparisonStrategy comparisonStrategy = StandardComparisonStrategy.instance();
private final Failures failures = Failures.instance();
private final ComparisonStrategy comparisonStrategy = StandardComparisonStrategy.instance();

/**
* Verifies that the actual {@code Class} is assignable from all the {@code others} classes.
Expand Down Expand Up @@ -400,5 +400,4 @@ private static void assertNotNull(AssertionInfo info, Class<?> actual) {
private static void classParameterIsNotNull(Class<?> clazz) {
requireNonNull(clazz, "The class to compare actual with should not be null");
}

}
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());
}
}
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();
}
}
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"));
}

}
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"));
}

}

0 comments on commit 98e3acd

Please sign in to comment.