Skip to content

Commit

Permalink
hamcrest#322 Added MatcherAssume.assumeThat again
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdemaeyer committed Feb 23, 2021
1 parent 8522353 commit 6e3f583
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions hamcrest/hamcrest.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'osgi'
version = rootProject.version

dependencies {
api 'org.opentest4j:opentest4j:1.2.0'
testImplementation(group: 'junit', name: 'junit', version: '4.13') {
transitive = false
}
Expand Down
27 changes: 27 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/MatcherAssume.java
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 hamcrest/src/test/java/org/hamcrest/MatcherAssumeTest.java
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"));
}
}

0 comments on commit 6e3f583

Please sign in to comment.