Skip to content

Commit 13e8054

Browse files
nymanjensronshapiro
authored andcommitted
Add StringSubject.ignoringCase().isEqualTo().
Note: The other methods on CaseInsensitiveStringComparison will follow later changes. RELNOTES=Added `StringSubject.ignoringCase()` ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=195954968
1 parent acf70ef commit 13e8054

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

core/src/main/java/com/google/common/truth/StringSubject.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.truth.Fact.fact;
2021
import static com.google.common.truth.Fact.simpleFact;
2122

2223
import com.google.common.annotations.GwtIncompatible;
@@ -172,4 +173,50 @@ public void doesNotContainMatch(String regex) {
172173
failWithActual("expected not to contain a match for", regex);
173174
}
174175
}
176+
177+
/**
178+
* Returns a {@link StringSubject}-like instance that will ignore the case of the characters.
179+
*
180+
* <p>Character equality ignoring case is defined as follows: Characters are equal according to
181+
* the {@code ==} operator before or after calling {@code
182+
* Character.toLowerCase(Character.toUpperCase(character))} on each character. Note that this is
183+
* independent of any locale.
184+
*/
185+
public CaseInsensitiveStringComparison ignoringCase() {
186+
return new CaseInsensitiveStringComparison();
187+
}
188+
189+
/** Case insensitive propositions for string subjects. */
190+
public final class CaseInsensitiveStringComparison {
191+
private CaseInsensitiveStringComparison() {}
192+
193+
/**
194+
* Fails if the subject is not equal to the given sequence (while ignoring case). For the
195+
* purposes of this comparison, two strings are equal if any of the following is true:
196+
*
197+
* <ul>
198+
* <li>they are equal according to {@link String#equalsIgnoreCase}
199+
* <li>they are both null
200+
* </ul>
201+
*
202+
* <p>Example: "abc" is equal to "ABC", but not to "abcd".
203+
*/
204+
public void isEqualTo(CharSequence expected) {
205+
if (actual() == null) {
206+
if (expected != null) {
207+
failWithoutActual(
208+
fact("expected a string that is equal to", expected),
209+
butWas(),
210+
simpleFact("(case is ignored)"));
211+
}
212+
} else {
213+
if (expected == null) {
214+
failWithoutActual(
215+
fact("expected", "null (null reference)"), butWas(), simpleFact("(case is ignored)"));
216+
} else if (!actual().equalsIgnoreCase(expected.toString())) {
217+
failWithoutActual(fact("expected", expected), butWas(), simpleFact("(case is ignored)"));
218+
}
219+
}
220+
}
221+
}
175222
}

core/src/test/java/com/google/common/truth/StringSubjectTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.common.truth;
1717

18+
import static com.google.common.truth.ExpectFailure.assertThat;
1819
import static com.google.common.truth.Truth.assertThat;
1920
import static org.junit.Assert.fail;
2021

@@ -139,7 +140,7 @@ public void stringEqualityToNull() {
139140

140141
@Test
141142
public void stringEqualityFail() {
142-
expectFailureWhenTestingThat("abc").isEqualTo("abd");
143+
expectFailureWhenTestingThat("abc").isEqualTo("ABC");
143144
assertThat(expectFailure.getFailure()).isInstanceOf(ComparisonFailureWithFacts.class);
144145
}
145146

@@ -251,6 +252,40 @@ public void stringDoesNotContainMatchPattern() {
251252
assertFailureValue("expected not to contain a match for", ".*b.*");
252253
}
253254

255+
@Test
256+
public void stringEqualityIgnoringCase() {
257+
assertThat("café").ignoringCase().isEqualTo("CAFÉ");
258+
}
259+
260+
@Test
261+
public void stringEqualityIgnoringCaseWithNullSubject() {
262+
assertThat((String) null).ignoringCase().isEqualTo(null);
263+
}
264+
265+
@Test
266+
public void stringEqualityIgnoringCaseFail() {
267+
expectFailureWhenTestingThat("abc").ignoringCase().isEqualTo("abd");
268+
269+
assertFailureValue("expected", "abd");
270+
assertThat(expectFailure.getFailure()).factKeys().contains("(case is ignored)");
271+
}
272+
273+
@Test
274+
public void stringEqualityIgnoringCaseFailWithNullSubject() {
275+
expectFailureWhenTestingThat((String) null).ignoringCase().isEqualTo("abc");
276+
277+
assertFailureValue("expected a string that is equal to", "abc");
278+
assertThat(expectFailure.getFailure()).factKeys().contains("(case is ignored)");
279+
}
280+
281+
@Test
282+
public void stringEqualityIgnoringCaseFailWithNullString() {
283+
expectFailureWhenTestingThat("abc").ignoringCase().isEqualTo(null);
284+
285+
assertFailureValue("expected", "null (null reference)");
286+
assertThat(expectFailure.getFailure()).factKeys().contains("(case is ignored)");
287+
}
288+
254289
private StringSubject expectFailureWhenTestingThat(String actual) {
255290
return expectFailure.whenTesting().that(actual);
256291
}

0 commit comments

Comments
 (0)