/** Fails if the string does not contain the given sequence. */ public void contains(CharSequence string) { checkNotNull(string); if (actual() == null) { failWithActual("expected a string that contains", string); } else if (!actual().contains(string)) { failWithActual("expected to contain", string); } }
/** Fails if the string contains a match on the given regex. */ @GwtIncompatible("java.util.regex.Pattern") public void doesNotContainMatch(Pattern regex) { Matcher matcher = regex.matcher(actual()); if (matcher.find()) { failWithoutActual( fact("expected not to contain a match for", regex), fact("but contained", matcher.group()), fact("full string", actualCustomStringRepresentationForPackageMembersToCall())); } }
/** Fails if the string contains the given sequence. */ public void doesNotContain(CharSequence string) { checkNotNull(string); if (actual() == null) { failWithActual("expected a string that does not contain", string); } else if (actual().contains(string)) { failWithActual("expected not to contain", string); } }
/** Fails if the string does not start with the given string. */ public void startsWith(String string) { checkNotNull(string); if (actual() == null) { failWithActual("expected a string that starts with", string); } else if (!actual().startsWith(string)) { failWithActual("expected to start with", string); } }
/** Fails if the string does not match the given regex. */ public void matches(String regex) { if (!actual().matches(regex)) { failWithActual("expected to match", regex); } }
/** Fails if the string matches the given regex. */ public void doesNotMatch(String regex) { if (actual().matches(regex)) { failWithActual("expected not to match", regex); } }
private boolean containsIgnoreCase(String string) { if (string.isEmpty()) { // TODO(b/79459427): Fix for J2CL discrepancy when string is empty return true; } String subject = actual(); for (int subjectOffset = 0; subjectOffset <= subject.length() - string.length(); subjectOffset++) { if (subject.regionMatches( /* ignoreCase = */ true, /* toffset = */ subjectOffset, /* other = */ string, /* ooffset = */ 0, /* len = */ string.length())) { return true; } } return false; } }
/** Fails if the string does not match the given regex. */ @GwtIncompatible("java.util.regex.Pattern") public void matches(Pattern regex) { if (!regex.matcher(actual()).matches()) { failWithActual("expected to match", regex); } }
/** Fails if the string matches the given regex. */ @GwtIncompatible("java.util.regex.Pattern") public void doesNotMatch(Pattern regex) { if (regex.matcher(actual()).matches()) { failWithActual("expected not to match", regex); } }
/** Fails if the string does not contain a match on the given regex. */ @GwtIncompatible("java.util.regex.Pattern") public void containsMatch(Pattern regex) { if (!regex.matcher(actual()).find()) { failWithActual("expected to contain a match for", regex); } }
/** Fails if the string does not contain a match on the given regex. */ public void containsMatch(String regex) { if (!Platform.containsMatch(actual(), regex)) { failWithActual("expected to contain a match for", regex); } }
/** Fails if the string does not have the given length. */ public void hasLength(int expectedLength) { checkArgument(expectedLength >= 0, "expectedLength(%s) must be >= 0", expectedLength); check("length()").that(actual().length()).isEqualTo(expectedLength); }
/** Fails if the string contains a match on the given regex. */ public void doesNotContainMatch(String regex) { if (Platform.containsMatch(actual(), regex)) { failWithActual("expected not to contain a match for", regex); } }
/** * Fails if the subject is equal to the given string (while ignoring case). The meaning of * equality is the same as for the {@link #isEqualTo} method. */ public void isNotEqualTo(String unexpected) { if (actual() == null) { if (unexpected == null) { failWithoutActual( fact("expected a string that is not equal to", "null (null reference)"), simpleFact("(case is ignored)")); } } else { if (unexpected != null && actual().equalsIgnoreCase(unexpected)) { failWithoutActual( fact("expected not to be", unexpected), butWas(), simpleFact("(case is ignored)")); } } }
/** Fails if the string does not contain the given sequence (while ignoring case). */ public void contains(CharSequence expectedSequence) { checkNotNull(expectedSequence); String expected = expectedSequence.toString(); if (actual() == null) { failWithoutActual( fact("expected a string that contains", expected), butWas(), simpleFact("(case is ignored)")); } else if (!containsIgnoreCase(expected)) { failWithoutActual( fact("expected to contain", expected), butWas(), simpleFact("(case is ignored)")); } }
/** Fails if the string contains the given sequence (while ignoring case). */ public void doesNotContain(CharSequence expectedSequence) { checkNotNull(expectedSequence); String expected = expectedSequence.toString(); if (actual() == null) { failWithoutActual( fact("expected a string that does not contain", expected), butWas(), simpleFact("(case is ignored)")); } else if (containsIgnoreCase(expected)) { failWithoutActual( fact("expected not to contain", expected), butWas(), simpleFact("(case is ignored)")); } }
/** * Fails if the subject is not equal to the given sequence (while ignoring case). For the * purposes of this comparison, two strings are equal if any of the following is true: * * <ul> * <li>they are equal according to {@link String#equalsIgnoreCase} * <li>they are both null * </ul> * * <p>Example: "abc" is equal to "ABC", but not to "abcd". */ public void isEqualTo(String expected) { if (actual() == null) { if (expected != null) { failWithoutActual( fact("expected a string that is equal to", expected), butWas(), simpleFact("(case is ignored)")); } } else { if (expected == null) { failWithoutActual( fact("expected", "null (null reference)"), butWas(), simpleFact("(case is ignored)")); } else if (!actual().equalsIgnoreCase(expected)) { failWithoutActual(fact("expected", expected), butWas(), simpleFact("(case is ignored)")); } } }