Index: src/main/java/org/fest/assertions/StringAssert.java =================================================================== --- src/main/java/org/fest/assertions/StringAssert.java (revision 708) +++ src/main/java/org/fest/assertions/StringAssert.java (working copy) @@ -1,16 +1,15 @@ /* * Created on Dec 26, 2006 - * - * 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 - * + * + * 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. - * + * + * 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 @2006-2009 the original author or authors. */ package org.fest.assertions; @@ -21,9 +20,9 @@ import org.fest.util.Strings; /** - * Understands assertion methods for Strings. To create a new instance of this class use the - * method {@link Assertions#assertThat(String)}. - * + * Understands assertion methods for Strings. To create a new instance of this class use the method + * {@link Assertions#assertThat(String)}. + * * @author Yvonne Wang * @author David DIDIER */ @@ -38,23 +37,27 @@ } /** {@inheritDoc} */ + @Override public StringAssert as(String description) { description(description); return this; } /** {@inheritDoc} */ + @Override public StringAssert describedAs(String description) { return as(description); } /** {@inheritDoc} */ + @Override public StringAssert as(Description description) { description(description); return this; } /** {@inheritDoc} */ + @Override public StringAssert describedAs(Description description) { return as(description); } @@ -67,6 +70,7 @@ * @throws AssertionError if the actual String does not satisfy the given condition. * @see #is(Condition) */ + @Override public StringAssert satisfies(Condition condition) { assertSatisfies(condition); return this; @@ -80,6 +84,7 @@ * @throws AssertionError if the actual String satisfies the given condition. * @see #isNot(Condition) */ + @Override public StringAssert doesNotSatisfy(Condition condition) { assertDoesNotSatisfy(condition); return this; @@ -93,6 +98,7 @@ * @throws AssertionError if the actual String does not satisfy the given condition. * @since 1.2 */ + @Override public StringAssert is(Condition condition) { assertIs(condition); return this; @@ -106,6 +112,7 @@ * @throws AssertionError if the actual String satisfies the given condition. * @since 1.2 */ + @Override public StringAssert isNot(Condition condition) { assertIsNot(condition); return this; @@ -116,6 +123,7 @@ * @throws AssertionError if the actual String is null. * @throws AssertionError if the actual String is not empty. */ + @Override public void isEmpty() { isNotNull(); if (Strings.isEmpty(actual)) return; @@ -127,6 +135,7 @@ * Verifies that the actual String is null or empty. * @throws AssertionError if the actual String is not null or not empty. */ + @Override public final void isNullOrEmpty() { if (Strings.isEmpty(actual)) return; failIfCustomMessageIsSet(); @@ -139,6 +148,7 @@ * @throws AssertionError if the actual String is null. * @throws AssertionError if the actual String is null or empty. */ + @Override public StringAssert isNotEmpty() { isNotNull(); if (!Strings.isEmpty(actual)) return this; @@ -152,17 +162,33 @@ * @return this assertion object. * @throws AssertionError if the actual String is not equal to the given one. */ + @Override public StringAssert isEqualTo(String expected) { assertEqualTo(expected); return this; } /** + * Verifies that the actual String is equal to the given one ignoring case. + * @param expected the given String to compare the actual String to. + * @return this assertion object. + * @throws AssertionError if the actual String is not equal to the given one ignoring case. + */ + public StringAssert isEqualToIgnoringCase(String expected) { + if (actual == null && expected == null) return this; + isNotNull(); + if (actual.equalsIgnoreCase(expected)) return this; + failIfCustomMessageIsSet(); + throw failure(concat(actual(), " should be equal to :", inBrackets(expected), " ignoring case")); + } + + /** * Verifies that the actual String is not equal to the given one. * @param other the given String to compare the actual String to. * @return this assertion object. * @throws AssertionError if the actual String is equal to the given one. */ + @Override public StringAssert isNotEqualTo(String other) { assertNotEqualTo(other); return this; @@ -173,6 +199,7 @@ * @return this assertion object. * @throws AssertionError if the actual String is null. */ + @Override public StringAssert isNotNull() { assertNotNull(); return this; @@ -184,6 +211,7 @@ * @return this assertion object. * @throws AssertionError if the actual String is the same as the given one. */ + @Override public StringAssert isNotSameAs(String other) { assertNotSameAs(other); return this; @@ -195,6 +223,7 @@ * @return this assertion object. * @throws AssertionError if the actual String is not the same as the given one. */ + @Override public StringAssert isSameAs(String expected) { assertSameAs(expected); return this; @@ -204,20 +233,22 @@ * Verifies that the number of characters in the actual String is equal to the given one. * @param expected the expected number of characters in the actual String. * @return this assertion object. - * @throws AssertionError if the number of characters of the actual String is not equal to the given - * one. + * @throws AssertionError if the number of characters of the actual String is not equal to the given one. */ + @Override public StringAssert hasSize(int expected) { int actualSize = actualGroupSize(); if (actualSize == expected) return this; failIfCustomMessageIsSet(); - throw failure(concat("expected size:", inBrackets(expected)," but was:", inBrackets(actualSize), " for String:", actual())); + throw failure(concat("expected size:", inBrackets(expected), " but was:", inBrackets(actualSize), " for String:", + actual())); } /** * Returns the number of elements in the actual String. * @return the number of elements in the actual String. */ + @Override protected int actualGroupSize() { isNotNull(); return actual.length(); @@ -310,6 +341,7 @@ } /** {@inheritDoc} */ + @Override public StringAssert overridingErrorMessage(String message) { replaceDefaultErrorMessagesWith(message); return this; Index: src/test/java/org/fest/assertions/StringAssert_isEqualToIgnoringCase_Test.java =================================================================== --- src/test/java/org/fest/assertions/StringAssert_isEqualToIgnoringCase_Test.java (revision 0) +++ src/test/java/org/fest/assertions/StringAssert_isEqualToIgnoringCase_Test.java (revision 0) @@ -0,0 +1,108 @@ +/* + * Created on Jan 10, 2007 + * + * 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 @2007-2009 the original author or authors. + */ +package org.fest.assertions; + +import static org.fest.assertions.CommonFailures.expectErrorIfObjectIsNull; +import static org.fest.assertions.CommonFailures.expectErrorWithDescriptionIfObjectIsNull; +import static org.fest.test.ExpectedFailure.expectAssertionError; + +import org.junit.BeforeClass; +import org.junit.Test; + +import org.fest.test.CodeToTest; + +/** + * Tests for {@link StringAssert#isEqualToIgnoringCase(String)}. + * + * @author Joel Costigliola + */ +public class StringAssert_isEqualToIgnoringCase_Test { + + private static String jedi; + + @BeforeClass + public static void setUpOnce() { + jedi = "Luke"; + } + + @Test + public void should_pass_if_actual_and_expected_are_equal() { + new StringAssert(jedi).isEqualToIgnoringCase("Luke"); + } + + @Test + public void should_pass_if_actual_and_expected_are_equal_ignoring_case() { + new StringAssert(jedi).isEqualToIgnoringCase("luke"); + } + + @Test + public void should_pass_if_both_actual_and_expected_are_null() { + new StringAssert(null).isEqualToIgnoringCase(null); + } + + @Test + public void should_fail_if_actual_is_null() { + expectErrorIfObjectIsNull(new CodeToTest() { + public void run() { + new StringAssert(null).isEqualToIgnoringCase("Yoda"); + } + }); + } + + @Test + public void should_fail_and_display_description_of_assertion_if_actual_is_null() { + expectErrorWithDescriptionIfObjectIsNull(new CodeToTest() { + public void run() { + new StringAssert(null).as("A Test").isEqualToIgnoringCase("Yoda"); + } + }); + } + + @Test + public void should_fail_if_actual_and_expected_are_not_equal_ignoring_case() { + expectAssertionError("<'Luke'> should be equal to :<'Yoda'> ignoring case").on(new CodeToTest() { + public void run() { + new StringAssert(jedi).isEqualToIgnoringCase("Yoda"); + } + }); + } + + @Test + public void should_fail_and_display_description_of_assertion_if_actual_and_expected_are_not_equal_ignoring_case() { + expectAssertionError("[A Test] <'Luke'> should be equal to :<'Yoda'> ignoring case").on(new CodeToTest() { + public void run() { + new StringAssert(jedi).as("A Test").isEqualToIgnoringCase("Yoda"); + } + }); + } + + @Test + public void should_fail_with_custom_message_if_actual_and_expected_are_not_equal_ignoring_case() { + expectAssertionError("My custom message").on(new CodeToTest() { + public void run() { + new StringAssert(jedi).overridingErrorMessage("My custom message").isEqualToIgnoringCase("Yoda"); + } + }); + } + + @Test + public void should_fail_with_custom_message_ignoring_description_of_assertion_if_actual_and_expected_are_not_equal_ignoring_case() { + expectAssertionError("My custom message").on(new CodeToTest() { + public void run() { + new StringAssert(jedi).as("A Test").overridingErrorMessage("My custom message").isEqualToIgnoringCase("Yoda"); + } + }); + } +}