|
17 | 17 |
|
18 | 18 | import static com.google.common.base.Preconditions.checkArgument;
|
19 | 19 | import static com.google.common.base.Preconditions.checkNotNull;
|
| 20 | +import static com.google.common.truth.Fact.fact; |
20 | 21 | import static com.google.common.truth.Fact.simpleFact;
|
21 | 22 |
|
22 | 23 | import com.google.common.annotations.GwtIncompatible;
|
@@ -172,4 +173,50 @@ public void doesNotContainMatch(String regex) {
|
172 | 173 | failWithActual("expected not to contain a match for", regex);
|
173 | 174 | }
|
174 | 175 | }
|
| 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 | + } |
175 | 222 | }
|
0 commit comments