forked from hamcrest/JavaHamcrest
-
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.
hamcrest#322 Added MatcherAssume.assumeThat again
- Loading branch information
1 parent
8522353
commit 6e3f583
Showing
3 changed files
with
66 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
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,27 @@ | ||
package org.hamcrest; | ||
|
||
import org.opentest4j.TestAbortedException; | ||
|
||
public final class MatcherAssume { | ||
|
||
private MatcherAssume() { | ||
} | ||
|
||
public static <T> void assumeThat(T assumption, Matcher<? super T> matcher) { | ||
assumeThat("", assumption, matcher); | ||
} | ||
|
||
public static <T> void assumeThat(String message, T assumption, Matcher<? super T> matcher) { | ||
if (!matcher.matches(assumption)) { | ||
throwTestAbortedException(message); | ||
} | ||
} | ||
|
||
private static void throwTestAbortedException(String message) { | ||
throw new TestAbortedException(isNotBlank(message) ? "Assumption failed: " + message : "Assumption failed"); | ||
} | ||
|
||
private static boolean isNotBlank(String string) { | ||
return string != null && !string.trim().isEmpty(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
hamcrest/src/test/java/org/hamcrest/MatcherAssumeTest.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,38 @@ | ||
package org.hamcrest; | ||
|
||
import org.junit.Test; | ||
import org.opentest4j.TestAbortedException; | ||
|
||
import static org.hamcrest.MatcherAssume.assumeThat; | ||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.junit.Assert.*; | ||
|
||
public class MatcherAssumeTest { | ||
|
||
@Test public void | ||
assumptionFailsWithMessage() { | ||
try { | ||
assumeThat("Custom assumption", "a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (TestAbortedException e) { | ||
assertEquals("Assumption failed: Custom assumption", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test public void | ||
assumptionFailsWithDefaultMessage() { | ||
try { | ||
assumeThat("a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (TestAbortedException e) { | ||
assertEquals("Assumption failed", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test public void | ||
assumptionSucceeds() { | ||
assumeThat("xyz", startsWith("xy")); | ||
} | ||
} |