8 changed files with 14 additions and 174 deletions
@ -1,61 +0,0 @@
@@ -1,61 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2017 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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 org.springframework.boot.testsupport.assertj; |
||||
|
||||
import org.assertj.core.api.Condition; |
||||
import org.hamcrest.Matcher; |
||||
import org.hamcrest.StringDescription; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Adapter class allowing a Hamcrest {@link Matcher} to be used as an AssertJ |
||||
* {@link Condition}. |
||||
* |
||||
* @param <T> the type of object that the condition accepts |
||||
* @author Phillip Webb |
||||
* @since 2.0.0 |
||||
*/ |
||||
public final class Matched<T> extends Condition<T> { |
||||
|
||||
private final Matcher<? extends T> matcher; |
||||
|
||||
private Matched(Matcher<? extends T> matcher) { |
||||
Assert.notNull(matcher, "Matcher must not be null"); |
||||
this.matcher = matcher; |
||||
} |
||||
|
||||
@Override |
||||
public boolean matches(T value) { |
||||
if (this.matcher.matches(value)) { |
||||
return true; |
||||
} |
||||
StringDescription description = new StringDescription(); |
||||
this.matcher.describeTo(description); |
||||
describedAs(description.toString()); |
||||
return false; |
||||
} |
||||
|
||||
public static <T> Condition<T> when(Matcher<? extends T> matcher) { |
||||
return by(matcher); |
||||
} |
||||
|
||||
public static <T> Condition<T> by(Matcher<? extends T> matcher) { |
||||
return new Matched<>(matcher); |
||||
} |
||||
|
||||
} |
||||
@ -1,20 +0,0 @@
@@ -1,20 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2017 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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. |
||||
*/ |
||||
|
||||
/** |
||||
* Utilities and helpers for AssertJ. |
||||
*/ |
||||
package org.springframework.boot.testsupport.assertj; |
||||
@ -1,56 +0,0 @@
@@ -1,56 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2019 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* https://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 org.springframework.boot.testsupport.assertj; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||
import static org.hamcrest.Matchers.startsWith; |
||||
|
||||
/** |
||||
* Tests for {@link Matched}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
class MatchedTests { |
||||
|
||||
@Test |
||||
void byMatcherMatches() { |
||||
assertThat("1234").is(Matched.by(startsWith("12"))); |
||||
} |
||||
|
||||
@Test |
||||
void byMatcherDoesNotMatch() { |
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertThat("1234").is(Matched.by(startsWith("23")))) |
||||
.withMessageContaining("a string starting with \"23\""); |
||||
} |
||||
|
||||
@Test |
||||
void whenMatcherMatches() { |
||||
assertThat("1234").is(Matched.when(startsWith("12"))); |
||||
} |
||||
|
||||
@Test |
||||
void whenMatcherDoesNotMatch() { |
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertThat("1234").is(Matched.when(startsWith("23")))) |
||||
.withMessageContaining("a string starting with \"23\""); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue