Skip to content

Commit

Permalink
Add null-checks to StringSubject
Browse files Browse the repository at this point in the history
Also add some missing unit tests while we're here.

RELNOTES=Add null-checks to StringSubject

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=304241733
  • Loading branch information
ethantkoenig authored and nick-someone committed Apr 2, 2020
1 parent 1af73cb commit 3481ab0
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 10 deletions.
42 changes: 33 additions & 9 deletions core/src/main/java/com/google/common/truth/StringSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,56 +114,77 @@ public void endsWith(String string) {
}
}

// TODO(cpovirk): Probably these should check for null first like the earlier methods do.

/** Fails if the string does not match the given regex. */
public void matches(String regex) {
if (!actual.matches(regex)) {
checkNotNull(regex);
if (actual == null) {
failWithActual("expected a string that matches", regex);
} else if (!actual.matches(regex)) {
failWithActual("expected to match", regex);
}
}

/** 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()) {
checkNotNull(regex);
if (actual == null) {
failWithActual("expected a string that matches", regex);
} else if (!regex.matcher(actual).matches()) {
failWithActual("expected to match", regex);
}
}

/** Fails if the string matches the given regex. */
public void doesNotMatch(String regex) {
if (actual.matches(regex)) {
checkNotNull(regex);
if (actual == null) {
failWithActual("expected a string that does not match", regex);
} else if (actual.matches(regex)) {
failWithActual("expected not 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()) {
checkNotNull(regex);
if (actual == null) {
failWithActual("expected a string that does not match", regex);
} else 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()) {
checkNotNull(regex);
if (actual == null) {
failWithActual("expected a string that contains a match for", regex);
} else 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)) {
checkNotNull(regex);
if (actual == null) {
failWithActual("expected a string that contains a match for", regex);
} else if (!Platform.containsMatch(actual, regex)) {
failWithActual("expected to contain a match for", regex);
}
}

/** Fails if the string contains a match on the given regex. */
@GwtIncompatible("java.util.regex.Pattern")
public void doesNotContainMatch(Pattern regex) {
checkNotNull(regex);
if (actual == null) {
failWithActual("expected a string that does not contain a match for", regex);
return;
}
Matcher matcher = regex.matcher(actual);
if (matcher.find()) {
failWithoutActual(
Expand All @@ -175,7 +196,10 @@ public void doesNotContainMatch(Pattern regex) {

/** Fails if the string contains a match on the given regex. */
public void doesNotContainMatch(String regex) {
if (Platform.containsMatch(actual, regex)) {
checkNotNull(regex);
if (actual == null) {
failWithActual("expected a string that does not contain a match for", regex);
} else if (Platform.containsMatch(actual, regex)) {
failWithActual("expected not to contain a match for", regex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,69 @@ public void stringMatchesStringWithFail() {
assertFailureValue("expected to match", ".*aaa.*");
}

@Test
public void stringMatchesStringFailNull() {
expectFailureWhenTestingThat(null).matches(".*aaa.*");
assertFailureValue("expected a string that matches", ".*aaa.*");
}

@Test
@GwtIncompatible("Pattern")
public void stringMatchesPattern() {
assertThat("abcaqadev").doesNotMatch(Pattern.compile(".*aaa.*"));
assertThat("abcaaadev").matches(Pattern.compile(".*aaa.*"));
}

@Test
@GwtIncompatible("Pattern")
public void stringMatchesPatternWithFail() {
expectFailureWhenTestingThat("abcaqadev").matches(Pattern.compile(".*aaa.*"));
assertFailureValue("expected to match", ".*aaa.*");
}

@Test
@GwtIncompatible("Pattern")
public void stringMatchesPatternFailNull() {
expectFailureWhenTestingThat(null).matches(Pattern.compile(".*aaa.*"));
assertFailureValue("expected a string that matches", ".*aaa.*");
}

@Test
public void stringDoesNotMatchString() {
assertThat("abcaqadev").doesNotMatch(".*aaa.*");
}

@Test
public void stringDoesNotMatchStringWithFail() {
expectFailureWhenTestingThat("abcaaadev").doesNotMatch(".*aaa.*");
assertFailureValue("expected not to match", ".*aaa.*");
}

@Test
public void stringDoesNotMatchStringFailNull() {
expectFailureWhenTestingThat(null).doesNotMatch(".*aaa.*");
assertFailureValue("expected a string that does not match", ".*aaa.*");
}

@Test
@GwtIncompatible("Pattern")
public void stringDoesNotMatchPattern() {
assertThat("abcaqadev").doesNotMatch(Pattern.compile(".*aaa.*"));
}

@Test
@GwtIncompatible("Pattern")
public void stringDoesNotMatchPatternWithFail() {
expectFailureWhenTestingThat("abcaaadev").doesNotMatch(Pattern.compile(".*aaa.*"));
assertFailureValue("expected not to match", ".*aaa.*");
}

@Test
@GwtIncompatible("Pattern")
public void stringDoesNotMatchPatternFailNull() {
expectFailureWhenTestingThat(null).doesNotMatch(Pattern.compile(".*aaa.*"));
assertFailureValue("expected a string that does not match", ".*aaa.*");
}

@Test
@GwtIncompatible("Pattern")
public void stringContainsMatchStringUsesFind() {
Expand All @@ -227,6 +277,12 @@ public void stringContainsMatchString() {
assertFailureValue("expected to contain a match for", ".*b.*");
}

@Test
public void stringContainsMatchStringFailNull() {
expectFailureWhenTestingThat(null).containsMatch(".*b.*");
assertFailureValue("expected a string that contains a match for", ".*b.*");
}

@Test
@GwtIncompatible("Pattern")
public void stringContainsMatchPattern() {
Expand All @@ -236,6 +292,13 @@ public void stringContainsMatchPattern() {
assertFailureValue("expected to contain a match for", ".*b.*");
}

@Test
@GwtIncompatible("Pattern")
public void stringContainsMatchPatternFailNull() {
expectFailureWhenTestingThat(null).containsMatch(Pattern.compile(".*b.*"));
assertFailureValue("expected a string that contains a match for", ".*b.*");
}

@Test
public void stringDoesNotContainMatchString() {
assertThat("aaa").doesNotContainMatch(".*b.*");
Expand All @@ -250,6 +313,12 @@ public void stringDoesNotContainMatchStringUsesFind() {
assertFailureValue("expected not to contain a match for", "[b]");
}

@Test
public void stringDoesNotContainMatchStringUsesFindFailNull() {
expectFailureWhenTestingThat(null).doesNotContainMatch("[b]");
assertFailureValue("expected a string that does not contain a match for", "[b]");
}

@Test
@GwtIncompatible("Pattern")
public void stringDoesNotContainMatchPattern() {
Expand All @@ -261,6 +330,13 @@ public void stringDoesNotContainMatchPattern() {
assertFailureValue("full string", "zzabazz");
}

@Test
@GwtIncompatible("Pattern")
public void stringDoesNotContainMatchPatternFailNull() {
expectFailureWhenTestingThat(null).doesNotContainMatch(Pattern.compile(".b."));
assertFailureValue("expected a string that does not contain a match for", ".b.");
}

@Test
public void stringEqualityIgnoringCase() {
assertThat("café").ignoringCase().isEqualTo("CAFÉ");
Expand Down

0 comments on commit 3481ab0

Please sign in to comment.