Skip to content

Commit

Permalink
Ensure that WithAssertions is in sync with Assertions
Browse files Browse the repository at this point in the history
 - Add missing assertThat methods
 - Align returns types (abstract -> subtype should be backward compatible)
 - Add a test case to AssertionsTest to ensure that WithAssertions is in sync
   with Assertions
  • Loading branch information
cykl authored and joel-costigliola committed Oct 16, 2016
1 parent 511337a commit cc5ddcd
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 8 deletions.
103 changes: 96 additions & 7 deletions src/main/java/org/assertj/core/api/WithAssertions.java
Expand Up @@ -15,6 +15,8 @@
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.text.DateFormat;
Expand All @@ -33,6 +35,10 @@
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;
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;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
Expand Down Expand Up @@ -134,7 +140,7 @@ default <T extends AssertDelegateTarget> T assertThat(final T assertion) {
/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(Map)}
*/
default <K, V> AbstractMapAssert<?, ? extends Map<K, V>, K, V> assertThat(final Map<K, V> actual) {
default <K, V> MapAssert<K, V> assertThat(final Map<K, V> actual) {
return Assertions.assertThat(actual);
}

Expand Down Expand Up @@ -260,19 +266,35 @@ default <T extends Comparable<? super T>> AbstractComparableAssert<?, T> assertT
/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(Iterable)}
*/
default <T> AbstractIterableAssert<?, Iterable<? extends T>, T, ObjectAssert<T>> assertThat(
final Iterable<? extends T> actual) {
default <T> IterableAssert<T> assertThat(final Iterable<? extends T> actual) {
return Assertions.assertThat(actual);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(Iterable, AssertFactory)}
*/
default <ACTUAL extends Iterable<? extends ELEMENT>, ELEMENT, ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>>
FactoryBasedNavigableIterableAssert<?, ACTUAL, ELEMENT, ELEMENT_ASSERT> assertThat(Iterable<? extends ELEMENT> actual,
AssertFactory<ELEMENT, ELEMENT_ASSERT> assertFactory) {
return Assertions.assertThat(actual, assertFactory);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(Iterator)}
*/
default <T> AbstractIterableAssert<?, Iterable<? extends T>, T, ObjectAssert<T>> assertThat(
final Iterator<? extends T> actual) {
default <T> IterableAssert<T> assertThat(final Iterator<? extends T> actual) {
return Assertions.assertThat(actual);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(Iterable, Class)}
*/
default <ACTUAL extends Iterable<? extends ELEMENT>, ELEMENT, ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>>
ClassBasedNavigableIterableAssert<?, ACTUAL, ELEMENT, ELEMENT_ASSERT> assertThat(ACTUAL actual,
Class<ELEMENT_ASSERT> assertClass) {
return Assertions.assertThat(actual, assertClass);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(Boolean)}
*/
Expand Down Expand Up @@ -395,9 +417,27 @@ default AbstractDoubleAssert<?> assertThat(final Double actual) {
/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(List)}
*/
default <T> AbstractListAssert<?, List<? extends T>, T, ObjectAssert<T>> assertThat(final List<? extends T> actual) {
default <T> ListAssert<? extends T> assertThat(final List<? extends T> actual) {
return Assertions.assertThat(actual);
}
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(List, Class)} )}
*/
default <ELEMENT, ACTUAL extends List<? extends ELEMENT>, ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>>
ClassBasedNavigableListAssert<?, ACTUAL, ELEMENT, ELEMENT_ASSERT> assertThat(List<? extends ELEMENT> actual,
Class<ELEMENT_ASSERT> assertClass) {
return Assertions.assertThat(actual, assertClass);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(List, AssertFactory)} )}
*/
default <ACTUAL extends List<? extends ELEMENT>, ELEMENT, ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>>
FactoryBasedNavigableListAssert<?, ACTUAL, ELEMENT, ELEMENT_ASSERT> assertThat(List<? extends ELEMENT> actual,
AssertFactory<ELEMENT, ELEMENT_ASSERT> assertFactory) {
return Assertions.assertThat(actual, assertFactory);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(List)}
Expand Down Expand Up @@ -676,6 +716,55 @@ default <T extends Throwable> ThrowableTypeAssert<T> assertThatExceptionOfType(f
return Assertions.assertThatExceptionOfType(exceptionType);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(Predicate)}
*/
default <T> PredicateAssert<T> assertThat(final Predicate<T> actual) {
return Assertions.assertThat(actual);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(IntPredicate)}
*/
default IntPredicateAssert assertThat(final IntPredicate actual) {
return Assertions.assertThat(actual);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(LongPredicate)}
*/
default LongPredicateAssert assertThat(final LongPredicate actual) {
return Assertions.assertThat(actual);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(DoublePredicate)}
*/
default DoublePredicateAssert assertThat(final DoublePredicate actual) {
return Assertions.assertThat(actual);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(URL)}
*/
default AbstractUrlAssert<?> assertThat(final URL actual) {
return Assertions.assertThat(actual);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(URI)}
*/
default AbstractUriAssert<?> assertThat(final URI actual) {
return Assertions.assertThat(actual);
}

/**
* Delegate call to {@link org.assertj.core.api.Assertions#assertThat(AssertProvider)}
*/
default <T> T assertThat(final AssertProvider<T> component) {
return Assertions.assertThat(component);
}

// --------------------------------------------------------------------------------------------------
// Filter methods : not assertions but here to have a complete entry point to all AssertJ features.
// --------------------------------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/assertj/core/api/AssertionsTest.java
Expand Up @@ -44,4 +44,13 @@ public void standard_soft_assertions_should_have_the_same_methods_as_in_assertio
.containsExactlyInAnyOrder(assertThatMethods);

}

@Test
public void with_assertions_should_have_the_same_methods_as_in_assertions() {
Method[] withAssertionsMethods = findMethodsWithName(WithAssertions.class, "assertThat");
Method[] assertionsMethods = findMethodsWithName(Assertions.class, "assertThat");

assertThat(withAssertionsMethods).usingElementComparator(IGNORING_DECLARING_CLASS_AND_METHOD_NAME)
.containsExactlyInAnyOrder(assertionsMethods);
}
}
Expand Up @@ -16,7 +16,10 @@
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.time.LocalDate;
Expand All @@ -33,6 +36,10 @@
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
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;

import org.assertj.core.api.exception.RuntimeIOException;
Expand Down Expand Up @@ -208,6 +215,23 @@ public void withAssertions_assertThat_list_Test() {
assertThat(new ArrayList<String>()).isEmpty();
}

/**
* Test that the delegate method is called.
*/
@SuppressWarnings("unchecked")
@Test
public void withAssertions_assertThat_list_assert_class_Test() {
assertThat(Arrays.asList(ITEMS), ObjectAssert.class).first().isEqualTo(ITEMS[0]);
}

/**
* Test that the delegate method is called.
*/
@Test
public void withAssertions_assertThat_list_assert_factory_Test() {
assertThat(Arrays.asList(ITEMS), t -> new ObjectAssert<>(t)).first().isEqualTo(ITEMS[0]);
}

/**
* Test that the delegate method is called.
*/
Expand Down Expand Up @@ -346,6 +370,23 @@ public void withAssertions_assertThat_iterable_Test() {
assertThat((Iterable<TestItem>) Arrays.asList(ITEMS)).contains(ITEMS[0]);
}

/**
* Test that the delegate method is called.
*/
@SuppressWarnings("unchecked")
@Test
public void withAssertions_assertThat_iterable_assert_class_Test() {
assertThat((Iterable<TestItem>) Arrays.asList(ITEMS), ObjectAssert.class).first().isEqualTo(ITEMS[0]);
}

/**
* Test that the delegate method is called.
*/
@Test
public void withAssertions_assertThat_iterable_assert_factory_Test() {
assertThat((Iterable<TestItem>) Arrays.asList(ITEMS), t -> new ObjectAssert<>(t)).first().isEqualTo(ITEMS[0]);
}

/**
* Test that the delegate method is called.
*/
Expand Down Expand Up @@ -718,4 +759,39 @@ public void withAssertions_catchThrowable_Test() {
});
assertThat(t).hasMessage("message");
}

@Test
public void withAssertions_assertThat_predicate_Test() {
Predicate<Boolean> predicate = b -> b;
assertThat(predicate).accepts(true);
}

@Test
public void withAssertions_assertThat_intPredicate_Test() {
IntPredicate predicate = i -> i == 0;
assertThat(predicate).accepts(0);
}

@Test
public void withAssertions_assertThat_longPredicate_Test() {
LongPredicate predicate = l -> l == 0;
assertThat(predicate).accepts(0l);
}

@Test
public void withAssertions_assertThat_doublePredicate_Test() {
DoublePredicate predicate = d -> d > 0;
assertThat(predicate).accepts(1.0);
}

@Test
public void withAssertions_assertThat_url_Test() throws MalformedURLException {
assertThat(new URL("https://github.com/joel-costigliola/assertj-core")).hasHost("github.com");
}

@Test
public void withAssertions_assertThat_uri_Test() throws URISyntaxException {
assertThat(new URI("https://github.com/joel-costigliola/assertj-core")).hasHost("github.com");
}
}

0 comments on commit cc5ddcd

Please sign in to comment.