Skip to content

Commit

Permalink
Add isMixedCase to CharSequence assertions (#2246)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Cordio <stefano.cordio@gmail.com>
  • Loading branch information
nach-o-man and scordio committed Aug 13, 2021
1 parent bce5cfd commit 5fc69d0
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 14 deletions.
39 changes: 37 additions & 2 deletions src/main/java/org/assertj/core/api/AbstractCharSequenceAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,8 @@ public SELF isEqualToIgnoringNewLines(CharSequence expected) {
}

/**
* Verifies that the actual {@code CharSequence} is a lowercase {@code CharSequence} by comparing it to a lowercase {@code actual} built with {@link String#toLowerCase()}.
* Verifies that the actual {@code CharSequence} is a lowercase {@code CharSequence} by comparing it to
* a lowercase {@code actual} built with {@link String#toLowerCase()}.
* <p>
* Example:
* <pre><code class='java'> // assertions will pass
Expand All @@ -1594,14 +1595,46 @@ public SELF isEqualToIgnoringNewLines(CharSequence expected) {
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code CharSequence} is not lowercase.
* @see #isMixedCase()
* @see #isUpperCase()
*/
public SELF isLowerCase() {
strings.assertLowerCase(info, actual);
return myself;
}

/**
* Verifies that the actual {@code CharSequence} is a uppercase {@code CharSequence} by comparing it to an uppercase {@code actual} built with {@link String#toUpperCase()}.
* Verifies that the actual {@code CharSequence} is a mixed case {@code CharSequence}, i.e.,
* neither uppercase nor lowercase.
* <p>
* If actual is empty or contains only case-independent characters, the assertion will pass.
* <p>
* Example:
* <pre><code class='java'> // assertions will pass
* assertThat(&quot;Capitalized&quot;).isMixedCase();
* assertThat(&quot;camelCase&quot;).isMixedCase();
* assertThat(&quot;rAndOMcAse1234&quot;).isMixedCase();
* assertThat(&quot;1@3$567&quot;).isMixedCase();
* assertThat(&quot;&quot;).isMixedCase();
*
* // assertions will fail
* assertThat(&quot;I AM GROOT!&quot;).isMixedCase();
* assertThat(&quot;please be quiet&quot;).isMixedCase();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code CharSequence} is not mixed case.
* @see #isLowerCase()
* @see #isUpperCase()
* @since 3.21.0
*/
public SELF isMixedCase() {
strings.assertMixedCase(info, actual);
return myself;
}

/**
* Verifies that the actual {@code CharSequence} is an uppercase {@code CharSequence} by comparing it to
* an uppercase {@code actual} built with {@link String#toUpperCase()}.
* <p>
* Example:
* <pre><code class='java'> // assertions will pass
Expand All @@ -1618,6 +1651,8 @@ public SELF isLowerCase() {
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code CharSequence} is not uppercase.
* @see #isLowerCase()
* @see #isMixedCase()
*/
public SELF isUpperCase() {
strings.assertUpperCase(info, actual);
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldBeMixedCase.java
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-2021 the original author or authors.
*/
package org.assertj.core.error;

/**
* Creates an error message that indicates an assertion that verifies that a {@code CharSequence} is mixed case failed.
*
* @author Andrey Kuzmin
*/
public class ShouldBeMixedCase extends BasicErrorMessageFactory {

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

private ShouldBeMixedCase(CharSequence actual) {
super("%nExpecting %s to be mixed case", actual);
}

}
20 changes: 16 additions & 4 deletions src/main/java/org/assertj/core/internal/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.assertj.core.error.ShouldBeEqualNormalizingUnicode.shouldBeEqualNormalizingUnicode;
import static org.assertj.core.error.ShouldBeEqualNormalizingWhitespace.shouldBeEqualNormalizingWhitespace;
import static org.assertj.core.error.ShouldBeLowerCase.shouldBeLowerCase;
import static org.assertj.core.error.ShouldBeMixedCase.shouldBeMixedCase;
import static org.assertj.core.error.ShouldBeNullOrEmpty.shouldBeNullOrEmpty;
import static org.assertj.core.error.ShouldBeSubstring.shouldBeSubstring;
import static org.assertj.core.error.ShouldBeUpperCase.shouldBeUpperCase;
Expand Down Expand Up @@ -1203,14 +1204,25 @@ public void assertIsEqualToIgnoringNewLines(AssertionInfo info, CharSequence act

public void assertLowerCase(AssertionInfo info, CharSequence actual) {
assertNotNull(info, actual);
if (actual.equals(actual.toString().toLowerCase())) return;
throw failures.failure(info, shouldBeLowerCase(actual));
if (!isLowerCase(actual)) throw failures.failure(info, shouldBeLowerCase(actual));
}

private boolean isLowerCase(CharSequence actual) {
return actual.equals(actual.toString().toLowerCase());
}

public void assertUpperCase(AssertionInfo info, CharSequence actual) {
assertNotNull(info, actual);
if (actual.equals(actual.toString().toUpperCase())) return;
throw failures.failure(info, shouldBeUpperCase(actual));
if (!isUpperCase(actual)) throw failures.failure(info, shouldBeUpperCase(actual));
}

private boolean isUpperCase(CharSequence actual) {
return actual.equals(actual.toString().toUpperCase());
}

public void assertMixedCase(AssertionInfo info, CharSequence actual) {
assertNotNull(info, actual);
if (isLowerCase(actual) != isUpperCase(actual)) throw failures.failure(info, shouldBeMixedCase(actual));
}

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

import static org.mockito.Mockito.verify;

import org.assertj.core.api.CharSequenceAssert;
import org.assertj.core.api.CharSequenceAssertBaseTest;

class CharSequenceAssert_isMixedCase_Test extends CharSequenceAssertBaseTest {

@Override
protected CharSequenceAssert invoke_api_method() {
return assertions.isMixedCase();
}

@Override
protected void verify_internal_effects() {
verify(strings).assertMixedCase(getInfo(assertions), getActual(assertions));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link ShouldBeLowerCase#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.
*
* @author Alex Ruiz
*/
class ShouldBeLowerCase_create_Test {
Expand All @@ -42,4 +40,5 @@ void should_create_error_message_for_string() {
// THEN
then(message).isEqualTo(format("[Test] %nExpecting \"ABC\" to be a lowercase"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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-2021 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.ShouldBeMixedCase.shouldBeMixedCase;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;

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

class ShouldBeMixedCase_create_Test {

@Test
void should_create_error_message() {
// WHEN
String message = shouldBeMixedCase("ABC").create(new TextDescription("Test"), STANDARD_REPRESENTATION);
// THEN
then(message).isEqualTo(format("[Test] %nExpecting \"ABC\" to be mixed case"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link ShouldBeUpperCase#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.
*
* @author Alex Ruiz
*/
class ShouldBeUpperCase_create_Test {
Expand All @@ -42,4 +40,5 @@ void should_create_error_message_for_string() {
// THEN
then(message).isEqualTo(format("[Test] %nExpecting \"abc\" to be uppercase"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link org.assertj.core.internal.Strings#assertLowerCase(org.assertj.core.api.AssertionInfo, CharSequence)} </code>.
*
* @author Marcel Overdijk
*/
class Strings_assertIsLowerCase_Test extends StringsBaseTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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-2021 the original author or authors.
*/
package org.assertj.core.internal.strings;

import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldBeMixedCase.shouldBeMixedCase;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

import org.assertj.core.internal.StringsBaseTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* @author Andrey Kuzmin
*/
class Strings_assertIsMixedCase_Test extends StringsBaseTest {

@Test
void should_fail_if_actual_is_null() {
// WHEN
AssertionError assertionError = expectAssertionError(() -> strings.assertMixedCase(someInfo(), null));
// THEN
then(assertionError).hasMessage(shouldNotBeNull().create());
}

@ParameterizedTest
@ValueSource(strings = {
"I AM GROOT!",
"P",
})
void should_fail_if_actual_is_uppercase(CharSequence actual) {
// WHEN
AssertionError assertionError = expectAssertionError(() -> strings.assertMixedCase(someInfo(), actual));
// THEN
then(assertionError).hasMessage(shouldBeMixedCase(actual).create());
}

@ParameterizedTest
@ValueSource(strings = {
"please be quiet",
"p",
})
void should_fail_if_actual_is_lowercase(CharSequence actual) {
// WHEN
AssertionError assertionError = expectAssertionError(() -> strings.assertMixedCase(someInfo(), actual));
// THEN
then(assertionError).hasMessage(shouldBeMixedCase(actual).create());
}

@ParameterizedTest
@ValueSource(strings = {
"",
" ",
"anExampleOfCamelCaseString",
"anEx4mpl3OfC4m3lC4s3Str1ng!", // with numbers and special characters
"@$#24^", // only numbers and special characters
})
void should_pass_if_actual_is_mixed_case(CharSequence actual) {
// WHEN/THEN
strings.assertMixedCase(someInfo(), actual);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link org.assertj.core.internal.Strings#assertUpperCase(org.assertj.core.api.AssertionInfo, CharSequence)} </code>.
*
* @author Marcel Overdijk
*/
class Strings_assertIsUpperCase_Test extends StringsBaseTest {
Expand Down

0 comments on commit 5fc69d0

Please sign in to comment.