forked from google/truth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in an initial superstructure for truth extensions, and migrate th…
…e first of these to be added, re2j ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=106555650
- Loading branch information
Showing
5 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth-parent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<groupId>com.google.truth.extensions</groupId> | ||
<artifactId>truth-extensions-parent</artifactId> | ||
<packaging>pom</packaging> | ||
<name>Truth Extensions (Parent)</name> | ||
<description> | ||
Parent metdata for a collection of Truth extensions, Subjects, utilities for | ||
the Truth assertion framework. | ||
</description> | ||
<modules> | ||
<module>re2j</module> | ||
</modules> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.google.truth.extensions</groupId> | ||
<artifactId>truth-extensions-parent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>truth-re2j-extension</artifactId> | ||
<name>Truth Extension for RE2J</name> | ||
<description> | ||
An extension for the Truth test assertion framework supporting RE2J patterns | ||
</description> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.re2j</groupId> | ||
<artifactId>re2j</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> | ||
|
||
|
164 changes: 164 additions & 0 deletions
164
extensions/re2j/src/main/java/com/google/common/truth/extensions/re2j/Re2jSubjects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright (c) 2015 Google, Inc. | ||
* | ||
* 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. | ||
*/ | ||
package com.google.common.truth.extensions.re2j; | ||
|
||
import com.google.common.annotations.GwtIncompatible; | ||
import com.google.common.truth.FailureStrategy; | ||
import com.google.common.truth.Subject; | ||
import com.google.common.truth.SubjectFactory; | ||
import com.google.re2j.Pattern; | ||
|
||
/** | ||
* Truth subjects for re2j regular expressions. | ||
* | ||
* <p>Truth natively provides subjects for dealing with {@code java.util.regex} based regular | ||
* expressions. This class is intended to provide {@code com.google.re2j} analogues to those | ||
* methods. | ||
*/ | ||
public final class Re2jSubjects { | ||
/** | ||
* Returns a subject factory for {@link String} subjects which you can use to assert things about | ||
* {@link com.google.re2j.Pattern} regexes. | ||
* | ||
* <p>This subject does not replace Truth's built-in {@link com.google.common.truth.StringSubject} | ||
* but instead provides only the methods needed to deal with regular expressions. | ||
* | ||
* @see com.google.common.truth.StringSubject | ||
*/ | ||
public static SubjectFactory<Re2jStringSubject, String> re2jString() { | ||
return Re2jStringSubject.FACTORY; | ||
} | ||
|
||
/** | ||
* Subject for {@link String} subjects which you can use to assert things about | ||
* {@link com.google.re2j.Pattern} regexes. | ||
* | ||
* @see #re2jString | ||
*/ | ||
public static final class Re2jStringSubject extends Subject<Re2jStringSubject, String> { | ||
private static final SubjectFactory<Re2jStringSubject, String> FACTORY = | ||
new SubjectFactory<Re2jStringSubject, String>() { | ||
@Override | ||
public Re2jStringSubject getSubject(FailureStrategy fs, String target) { | ||
return new Re2jStringSubject(fs, target); | ||
} | ||
}; | ||
|
||
private Re2jStringSubject(FailureStrategy failureStrategy, String subject) { | ||
super(failureStrategy, subject); | ||
} | ||
|
||
@Override | ||
protected String getDisplaySubject() { | ||
if (internalCustomName() != null) { | ||
return internalCustomName() + " (<" + quote(getSubject()) + ">)"; | ||
} else { | ||
return "<" + quote(getSubject()) + ">"; | ||
} | ||
} | ||
|
||
/** | ||
* Fails if the string does not match the given regex. | ||
*/ | ||
public void matches(String regex) { | ||
if (!Pattern.matches(regex, getSubject())) { | ||
fail("matches", regex); | ||
} | ||
} | ||
|
||
/** | ||
* Fails if the string does not match the given regex. | ||
*/ | ||
@GwtIncompatible("com.google.re2j.Pattern") | ||
public void matches(Pattern regex) { | ||
if (!regex.matcher(getSubject()).matches()) { | ||
fail("matches", regex); | ||
} | ||
} | ||
|
||
/** | ||
* Fails if the string matches the given regex. | ||
*/ | ||
public void doesNotMatch(String regex) { | ||
if (Pattern.matches(regex, getSubject())) { | ||
fail("fails to match", regex); | ||
} | ||
} | ||
|
||
/** | ||
* Fails if the string matches the given regex. | ||
*/ | ||
@GwtIncompatible("com.google.re2j.Pattern") | ||
public void doesNotMatch(Pattern regex) { | ||
if (regex.matcher(getSubject()).matches()) { | ||
fail("fails to match", regex); | ||
} | ||
} | ||
|
||
/** | ||
* Fails if the string does not contain a match on the given regex. | ||
*/ | ||
@GwtIncompatible("com.google.re2j.Pattern") | ||
public void containsMatch(Pattern pattern) { | ||
if (!pattern.matcher(getSubject()).find()) { | ||
failWithRawMessage( | ||
"%s should have contained a match for <%s>", | ||
getDisplaySubject(), | ||
pattern); | ||
} | ||
} | ||
|
||
/** | ||
* Fails if the string does not contain a match on the given regex. | ||
*/ | ||
public void containsMatch(String regex) { | ||
if (!containsMatch(getSubject(), regex)) { | ||
failWithRawMessage("%s should have contained a match for <%s>", getDisplaySubject(), regex); | ||
} | ||
} | ||
|
||
/** | ||
* Fails if the string contains a match on the given regex. | ||
*/ | ||
@GwtIncompatible("com.google.re2j.Pattern") | ||
public void doesNotContainMatch(Pattern pattern) { | ||
if (pattern.matcher(getSubject()).find()) { | ||
failWithRawMessage("%s should not have contained a match for <%s>", | ||
getDisplaySubject(), pattern); | ||
} | ||
} | ||
|
||
/** | ||
* Fails if the string contains a match on the given regex. | ||
*/ | ||
public void doesNotContainMatch(String regex) { | ||
if (containsMatch(getSubject(), regex)) { | ||
failWithRawMessage("%s should not have contained a match for <%s>", | ||
getDisplaySubject(), regex); | ||
} | ||
} | ||
|
||
private static String quote(CharSequence toBeWrapped) { | ||
return "\"" + toBeWrapped + "\""; | ||
} | ||
|
||
private static boolean containsMatch(String subject, String regex) { | ||
return Pattern.compile(regex).matcher(subject).find(); | ||
} | ||
} | ||
|
||
private Re2jSubjects() {} | ||
} |
72 changes: 72 additions & 0 deletions
72
extensions/re2j/src/test/java/com/google/common/truth/extensions/re2j/Re2jSubjectsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2015 Google, Inc. | ||
* | ||
* 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. | ||
*/ | ||
package com.google.common.truth.extensions.re2j; | ||
|
||
import static com.google.common.truth.extensions.re2j.Re2jSubjects.re2jString; | ||
import static com.google.common.truth.Truth.assertAbout; | ||
|
||
import com.google.re2j.Pattern; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Unit tests for {@link Re2jSubjects}. */ | ||
@RunWith(JUnit4.class) | ||
public class Re2jSubjectsTest { | ||
private static final String PATTERN_STR = "(?:hello )+world"; | ||
private static final Pattern PATTERN = Pattern.compile(PATTERN_STR); | ||
|
||
@Test | ||
public void matches_string_succeeds() { | ||
assertAbout(re2jString()).that("hello world").matches(PATTERN_STR); | ||
} | ||
|
||
@Test | ||
public void matches_pattern_succeeds() { | ||
assertAbout(re2jString()).that("hello world").matches(PATTERN); | ||
} | ||
|
||
@Test | ||
public void doesNotMatch_string_succeeds() { | ||
assertAbout(re2jString()).that("world").doesNotMatch(PATTERN_STR); | ||
} | ||
|
||
@Test | ||
public void doesNotMatch_pattern_succeeds() { | ||
assertAbout(re2jString()).that("world").doesNotMatch(PATTERN); | ||
} | ||
|
||
@Test | ||
public void containsMatch_string_succeeds() { | ||
assertAbout(re2jString()).that("this is a hello world").containsMatch(PATTERN_STR); | ||
} | ||
|
||
@Test | ||
public void containsMatch_pattern_succeeds() { | ||
assertAbout(re2jString()).that("this is a hello world").containsMatch(PATTERN); | ||
} | ||
|
||
@Test | ||
public void doesNotContainMatch_string_succeeds() { | ||
assertAbout(re2jString()).that("hello cruel world").doesNotContainMatch(PATTERN_STR); | ||
} | ||
|
||
@Test | ||
public void doesNotContainMatch_pattern_succeeds() { | ||
assertAbout(re2jString()).that("hello cruel world").doesNotContainMatch(PATTERN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters