Skip to content

Commit

Permalink
Add isUpperCase/isLowerCase String/CharSequence assertions - Fix #1106
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Overdijk authored and joel-costigliola committed Mar 13, 2018
1 parent 7b3d997 commit 75f3919
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 27 deletions.
48 changes: 48 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractCharSequenceAssert.java
Expand Up @@ -1314,4 +1314,52 @@ public SELF isEqualToIgnoringNewLines(CharSequence expected) {
strings.assertIsEqualToIgnoringNewLines(info, actual, expected);
return myself;
}

/**
* 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
* assertThat(&quot;lego&quot;).isLowerCase();
* assertThat("").isLowerCase();
* assertThat(" ").isLowerCase();
* assertThat(".").isLowerCase();
* assertThat("7").isLowerCase();
* assertThat("a.7").isLowerCase();
*
* // assertions will fail
* assertThat(&quot;Lego&quot;).isLowerCase();
* assertThat(&quot;LEGO&quot;).isLowerCase();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code CharSequence} is not lowercase.
*/
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()}.
* <p>
* Example :
* <pre><code class='java'> // assertions will pass
* assertThat(&quot;LEGO&quot;).isUpperCase();
* assertThat("").isUpperCase();
* assertThat(" ").isUpperCase();
* assertThat(".").isUpperCase();
* assertThat("7").isUpperCase();
* assertThat("A.7").isUpperCase();
*
* // assertions will fail
* assertThat(&quot;Lego&quot;).isUpperCase();
* assertThat(&quot;lego&quot;).isUpperCase();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code CharSequence} is not uppercase.
*/
public SELF isUpperCase() {
strings.assertUpperCase(info, actual);
return myself;
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/assertj/core/error/ShouldBeLowerCase.java
Expand Up @@ -24,11 +24,11 @@ public class ShouldBeLowerCase extends BasicErrorMessageFactory {
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeLowerCase(Character actual) {
public static ErrorMessageFactory shouldBeLowerCase(Object actual) {
return new ShouldBeLowerCase(actual);
}

private ShouldBeLowerCase(Character actual) {
super("%nExpecting <%s> to be a lowercase character", actual);
private ShouldBeLowerCase(Object actual) {
super("%nExpecting <%s> to be a lowercase", actual);
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/assertj/core/error/ShouldBeUpperCase.java
Expand Up @@ -24,11 +24,11 @@ public class ShouldBeUpperCase extends BasicErrorMessageFactory {
* @param actual the actual value in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeUpperCase(Character actual) {
public static ErrorMessageFactory shouldBeUpperCase(Object actual) {
return new ShouldBeUpperCase(actual);
}

private ShouldBeUpperCase(Character actual) {
super("%nExpecting:<%s> to be a uppercase character", actual);
private ShouldBeUpperCase(Object actual) {
super("%nExpecting <%s> to be uppercase", actual);
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/assertj/core/internal/Strings.java
Expand Up @@ -23,8 +23,10 @@
import static org.assertj.core.error.ShouldBeEqualIgnoringNewLines.shouldBeEqualIgnoringNewLines;
import static org.assertj.core.error.ShouldBeEqualIgnoringWhitespace.shouldBeEqualIgnoringWhitespace;
import static org.assertj.core.error.ShouldBeEqualNormalizingWhitespace.shouldBeEqualNormalizingWhitespace;
import static org.assertj.core.error.ShouldBeLowerCase.shouldBeLowerCase;
import static org.assertj.core.error.ShouldBeNullOrEmpty.shouldBeNullOrEmpty;
import static org.assertj.core.error.ShouldBeSubstring.shouldBeSubstring;
import static org.assertj.core.error.ShouldBeUpperCase.shouldBeUpperCase;
import static org.assertj.core.error.ShouldContainCharSequence.shouldContain;
import static org.assertj.core.error.ShouldContainCharSequence.shouldContainIgnoringCase;
import static org.assertj.core.error.ShouldContainCharSequenceOnlyOnce.shouldContainOnlyOnce;
Expand Down Expand Up @@ -979,6 +981,18 @@ public void assertIsEqualToIgnoringNewLines(AssertionInfo info, CharSequence act
throw failures.failure(info, shouldBeEqualIgnoringNewLines(actual, expected));
}

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

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

private static String removeNewLines(CharSequence text) {
String normalizedText = normalizeNewlines(text);
return normalizedText.toString().replace("\n", "");
Expand Down
@@ -0,0 +1,31 @@
/*
* 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-2018 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;

public class CharSequenceAssert_isLowerCase_Test extends CharSequenceAssertBaseTest {

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

@Override
protected void verify_internal_effects() {
verify(strings).assertLowerCase(getInfo(assertions), getActual(assertions));
}
}
@@ -0,0 +1,31 @@
/*
* 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-2018 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;

public class CharSequenceAssert_isUpperCase_Test extends CharSequenceAssertBaseTest {

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

@Override
protected void verify_internal_effects() {
verify(strings).assertUpperCase(getInfo(assertions), getActual(assertions));
}
}
Expand Up @@ -12,12 +12,13 @@
*/
package org.assertj.core.error;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.error.ShouldBeLowerCase.shouldBeLowerCase;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;

import org.assertj.core.description.*;
import org.assertj.core.presentation.StandardRepresentation;
import org.junit.*;
import org.assertj.core.description.TextDescription;
import org.junit.Test;

/**
* Tests for <code>{@link ShouldBeLowerCase#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.
Expand All @@ -26,16 +27,15 @@
*/
public class ShouldBeLowerCase_create_Test {

private ErrorMessageFactory factory;

@Before
public void setUp() {
factory = shouldBeLowerCase('A');
@Test
public void should_create_error_message_for_character() {
String message = shouldBeLowerCase('A').create(new TextDescription("Test"), STANDARD_REPRESENTATION);
assertThat(message).isEqualTo(format("[Test] %nExpecting <'A'> to be a lowercase"));
}

@Test
public void should_create_error_message() {
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
assertThat(message).isEqualTo(String.format("[Test] %nExpecting <'A'> to be a lowercase character"));
public void should_create_error_message_for_string() {
String message = shouldBeLowerCase("ABC").create(new TextDescription("Test"), STANDARD_REPRESENTATION);
assertThat(message).isEqualTo(format("[Test] %nExpecting <\"ABC\"> to be a lowercase"));
}
}
Expand Up @@ -12,12 +12,14 @@
*/
package org.assertj.core.error;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.error.ShouldBeUpperCase.shouldBeUpperCase;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;

import org.assertj.core.description.*;
import org.assertj.core.description.TextDescription;
import org.assertj.core.presentation.StandardRepresentation;
import org.junit.*;
import org.junit.Test;

/**
* Tests for <code>{@link ShouldBeUpperCase#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.
Expand All @@ -26,16 +28,15 @@
*/
public class ShouldBeUpperCase_create_Test {

private ErrorMessageFactory factory;

@Before
public void setUp() {
factory = shouldBeUpperCase('a');
@Test
public void should_create_error_message_for_character() {
String message = shouldBeUpperCase('a').create(new TextDescription("Test"), new StandardRepresentation());
assertThat(message).isEqualTo(format("[Test] %nExpecting <'a'> to be uppercase"));
}

@Test
public void should_create_error_message() {
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
assertThat(message).isEqualTo(String.format("[Test] %nExpecting:<'a'> to be a uppercase character"));
public void should_create_error_message_for_string() {
String message = shouldBeUpperCase("abc").create(new TextDescription("Test"), STANDARD_REPRESENTATION);
assertThat(message).isEqualTo(format("[Test] %nExpecting <\"abc\"> to be uppercase"));
}
}
@@ -0,0 +1,57 @@
/*
* 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-2018 the original author or authors.
*/
package org.assertj.core.internal.strings;

import static org.assertj.core.error.ShouldBeLowerCase.shouldBeLowerCase;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.FailureMessages.actualIsNull;

import org.assertj.core.internal.StringsBaseTest;
import org.junit.Test;

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

@Test
public void should_pass_if_actual_is_lowercase() {
strings.assertLowerCase(someInfo(), "lego");
}

@Test
public void should_pass_if_actual_is_empty() {
strings.assertLowerCase(someInfo(), "");
}

@Test
public void should_fail_if_actual_is_not_fully_lowercase() {
thrown.expectAssertionError(shouldBeLowerCase("Lego"));
strings.assertLowerCase(someInfo(), "Lego");
}

@Test
public void should_fail_if_actual_is_uppercase() {
thrown.expectAssertionError(shouldBeLowerCase("LEGO"));
strings.assertLowerCase(someInfo(), "LEGO");
}

@Test
public void should_fail_if_actual_is_null() {
thrown.expectAssertionError(actualIsNull());
strings.assertLowerCase(someInfo(), null);
}

}
@@ -0,0 +1,57 @@
/*
* 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-2018 the original author or authors.
*/
package org.assertj.core.internal.strings;

import static org.assertj.core.error.ShouldBeUpperCase.shouldBeUpperCase;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.FailureMessages.actualIsNull;

import org.assertj.core.internal.StringsBaseTest;
import org.junit.Test;

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

@Test
public void should_pass_if_actual_is_uppercase() {
strings.assertUpperCase(someInfo(), "LEGO");
}

@Test
public void should_pass_if_actual_is_empty() {
strings.assertUpperCase(someInfo(), "");
}

@Test
public void should_fail_if_actual_is_not_only_uppercase() {
thrown.expectAssertionError(shouldBeUpperCase("Lego"));
strings.assertUpperCase(someInfo(), "Lego");
}

@Test
public void should_fail_if_actual_is_lowercase() {
thrown.expectAssertionError(shouldBeUpperCase("lego"));
strings.assertUpperCase(someInfo(), "lego");
}

@Test
public void should_fail_if_actual_is_null() {
thrown.expectAssertionError(actualIsNull());
strings.assertUpperCase(someInfo(), null);
}

}

0 comments on commit 75f3919

Please sign in to comment.