Skip to content

Commit

Permalink
fixes assertj#658 add support for java 8 Predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr authored and unknown committed Mar 6, 2017
1 parent 18196b2 commit 1d9c782
Show file tree
Hide file tree
Showing 44 changed files with 2,604 additions and 1 deletion.
127 changes: 127 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractPredicateAssert.java
@@ -0,0 +1,127 @@
/**
* 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.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.core.api;

import java.util.function.Predicate;

import org.assertj.core.error.ShouldNotAccept;
import org.assertj.core.internal.Iterables;
import org.assertj.core.presentation.PredicateDescription;
import org.assertj.core.util.VisibleForTesting;

import static org.assertj.core.error.ShouldAccept.shouldAccept;
import static org.assertj.core.error.ShouldNotAccept.shouldNotAccept;
import static org.assertj.core.error.ShouldNotMatch.shouldNotMatch;
import static org.assertj.core.presentation.PredicateDescription.fromDescription;

/**
* Assertions for {@link Predicate}.
*
* @param <T> type of the value contained in the {@link Predicate}.
*
* @author Filip Hrisafov
*/
public abstract class AbstractPredicateAssert<S extends AbstractPredicateAssert<S, T>, T> extends
AbstractAssert<S, Predicate<T>> {

@VisibleForTesting
Iterables iterables = Iterables.instance();

protected AbstractPredicateAssert(Predicate<T> actual, Class<?> selfType) {
super(actual, selfType);
}

/**
* <p>
* Verifies that the {@link Predicate} evaluates {@code true} for the given value.
* </p>
* Assertion will pass :
* <pre><code class='java'> assertThat(predicate -> predicate.equals("something"))
* .accepts("something");</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(predicate -> predicate.equals("something"))
* .accepts("something else");</code></pre>
*
* @return this assertion object.
*/
public S accepts(T value) {
isNotNull();
if (!actual.test(value)) { throwAssertionError(shouldAccept(actual, value, fromDescription(info.description()))); }
return myself;
}

/**
* <p>
* Verifies that the {@link Predicate} evaluates {@code false} for the given value.
* </p>
* Assertion will pass :
* <pre><code class='java'> assertThat(predicate -> predicate.equals("something"))
* .rejects("something else");</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(predicate -> predicate.equals("something"))
* .rejects("something");</code></pre>
*
* @return this assertion object.
*/
public S rejects(T value) {
isNotNull();
if (actual.test(value)) { throwAssertionError(shouldNotAccept(actual, value, fromDescription(info.description()))); }
return myself;
}

/**
* <p>
* Verifies that {@link Predicate} evaluates to {@code true} for all the elements from the {@code values}.
* </p>
* Assertion will pass:
* <pre><code class='java'>
* List<String> elements = Arrays.asList("first", "second");
* assertThat(value -> elements.contains(value)).acceptsAll(elements);</code></pre>
*
* Assertion will fail:
* <pre><code class='java'>
* List<String> elements = Arrays.asList("first", "second");
* assertThat(value -> elements.contains(value)).acceptsAll(Arrays.asList("first", "third"));</code></pre>
*
* @return this assertion object
*/
public S acceptsAll(Iterable<? extends T> values) {
isNotNull();
iterables.assertAllMatch(info, values, actual);
return myself;
}

/**
* <p>
* Verifies that {@link Predicate} evaluates to {@code false} for all the elements from the {@code values}.
* </p>
* Assertion will pass:
* <pre><code class='java'>
* List<String> elements = Arrays.asList("first", "second");
* assertThat(value -> elements.contains(value)).rejectsAll(Arrays.asList("third", "fourth"));</code></pre>
*
* Assertion will fail:
* <pre><code class='java'>
* List<String> elements = Arrays.asList("first", "second");
* assertThat(value -> elements.contains(value)).rejectsAll(Arrays.asList("first", "third"));</code></pre>
*
* @return this assertion object
*/
public S rejectsAll(Iterable<? extends T> values) {
isNotNull();
iterables.assertNoneMatch(info, values, actual);
return myself;
}
}
128 changes: 128 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractPredicateLikeAssert.java
@@ -0,0 +1,128 @@
/**
* 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.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.core.api;

import java.util.function.Predicate;

import org.assertj.core.internal.Iterables;
import org.assertj.core.presentation.PredicateDescription;
import org.assertj.core.util.VisibleForTesting;

import static org.assertj.core.error.ShouldAccept.shouldAccept;
import static org.assertj.core.error.ShouldNotAccept.shouldNotAccept;
import static org.assertj.core.presentation.PredicateDescription.fromDescription;

/**
* Assertions for {@link Predicate}.
*
* @param <T> the actual type
* @param <W> the type of the wrapped variable in one of the special predicates
*
* @author Filip Hrisafov
*/
public abstract class AbstractPredicateLikeAssert<S extends AbstractPredicateLikeAssert<S, T, W>, T, W> extends
AbstractAssert<S, T> {

@VisibleForTesting
Iterables iterables = Iterables.instance();

@VisibleForTesting
Predicate<W> wrappedPredicate;

protected AbstractPredicateLikeAssert(T actual, Predicate<W> wrappedPredicate, Class<?> selfType) {
super(actual, selfType);
this.wrappedPredicate = wrappedPredicate;
}

/**
* Verifies that the {@link Predicate} evaluates {@code true} for the given value.
* </p>
* Assertion will pass :
* <pre><code class='java'> assertThat(predicate -> predicate.equals("something"))
* .accepts("something");</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(predicate -> predicate.equals("something"))
* .accepts("something else");</code></pre>
*
* @return this assertion object.
*/
protected S acceptsInternal(W value) {
isNotNull();
if (!wrappedPredicate.test(value)) { throwAssertionError(shouldAccept(wrappedPredicate, value, fromDescription(info.description()))); }
return myself;
}

/**
* Verifies that the {@link Predicate} evaluates {@code false} for the given value.
* </p>
* Assertion will pass :
* <pre><code class='java'> assertThat(predicate -> predicate.equals("something"))
* .rejects("something else");</code></pre>
*
* Assertion will fail :
* <pre><code class='java'> assertThat(predicate -> predicate.equals("something"))
* .rejects("something");</code></pre>
*
* @return this assertion object.
*/
protected S rejectsInternal(W value) {
isNotNull();
if (wrappedPredicate.test(value)) { throwAssertionError(shouldNotAccept(wrappedPredicate, value, fromDescription(info.description()))); }
return myself;
}

/**
* <p>
* Verifies that {@link Predicate} evaluates to {@code true} for all the elements from the {@code values}.
* </p>
* Assertion will pass:
* <pre><code class='java'>
* List<String> elements = Arrays.asList("first", "second");
* assertThat(value -> elements.contains(value)).acceptsAll(elements);</code></pre>
*
* Assertion will fail:
* <pre><code class='java'>
* List<String> elements = Arrays.asList("first", "second");
* assertThat(value -> elements.contains(value)).acceptsAll(Arrays.asList("first", "third"));</code></pre>
*
* @return this assertion object
*/
protected S acceptsAllInternal(Iterable<? extends W> values) {
isNotNull();
iterables.assertAllMatch(info, values, wrappedPredicate);
return myself;
}

/**
* <p>
* Verifies that {@link Predicate} evaluates to {@code false} for all the elements from the {@code values}.
* </p>
* Assertion will pass:
* <pre><code class='java'>
* List<String> elements = Arrays.asList("first", "second");
* assertThat(value -> elements.contains(value)).rejectsAll(Arrays.asList("third", "fourth"));</code></pre>
*
* Assertion will fail:
* <pre><code class='java'>
* List<String> elements = Arrays.asList("first", "second");
* assertThat(value -> elements.contains(value)).rejectsAll(Arrays.asList("first", "third"));</code></pre>
*
* @return this assertion object
*/
protected S rejectsAllInternal(Iterable<? extends W> values) {
isNotNull();
iterables.assertNoneMatch(info, values, wrappedPredicate);
return myself;
}
}
Expand Up @@ -35,6 +35,8 @@
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;
import java.util.function.IntPredicate;
import java.util.function.Predicate;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.api.exception.RuntimeIOException;
Expand Down
Expand Up @@ -16,6 +16,10 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.DoublePredicate;
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -218,4 +222,51 @@ public static <T extends Comparable<? super T>> AbstractComparableAssert<?, T> a
public static <T extends AssertDelegateTarget> T assertThat(T assertion) {
return assertion;
}

/**
* Create assertion for {@link Predicate}.
*
* @param actual the actual value.
* @param <T> the type of the value contained in the {@link Predicate}.
* @return the created assertion object.
* @since 3.5.0
*/
public static <T> PredicateAssert<T> assertThat(Predicate<T> actual) {
return new PredicateAssert<>(actual);
}

/**
* Create assertion for {@link IntPredicate}.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.5.0
*/
public static IntPredicateAssert assertThat(IntPredicate actual) {
return new IntPredicateAssert(actual);
}

/**
* Create assertion for {@link LongPredicate}.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.5.0
*/
public static LongPredicateAssert assertThat(LongPredicate actual) {
return new LongPredicateAssert(actual);
}

/**
* Create assertion for {@link DoublePredicate}.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.5.0
*/
public static DoublePredicateAssert assertThat(DoublePredicate actual) {
return new DoublePredicateAssert(actual);
}


}

0 comments on commit 1d9c782

Please sign in to comment.