Skip to content

Commit

Permalink
enable fluent assertions on size of Maps (as suggested in assertj/ass…
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSchumacher authored and joel-costigliola committed May 28, 2016
1 parent 89cfb4f commit 608ede7
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 6 deletions.
Expand Up @@ -1730,10 +1730,11 @@ public SELF withThreadDumpOnError() {
* assertThat(elvesRings).size().isGreaterThan(3);</code></pre>
*
* @return AbstractIterableSizeAssert built with the {@code Iterable}'s size.
* @throws NullPointerException if the given {@code Iterable} is {@code null}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public AbstractIterableSizeAssert<SELF, ACTUAL, ELEMENT, ELEMENT_ASSERT> size() {
Preconditions.checkNotNull(actual, "Can not assert on size of a null iterable.");
Preconditions.checkNotNull(actual, "Can not perform assertions on the size of a null iterable.");
return new IterableSizeAssert(this, IterableUtil.sizeOf(actual));
}
}
34 changes: 32 additions & 2 deletions src/main/java/org/assertj/core/api/AbstractMapAssert.java
Expand Up @@ -20,6 +20,7 @@

import org.assertj.core.description.Description;
import org.assertj.core.internal.Maps;
import org.assertj.core.util.Preconditions;
import org.assertj.core.util.VisibleForTesting;

/**
Expand Down Expand Up @@ -624,8 +625,8 @@ public S containsOnly(@SuppressWarnings("unchecked") Map.Entry<? extends K, ? ex
* <p>
* Example :
* <pre><code class='java'> Map&lt;Ring, TolkienCharacter&gt; ringBearers = newLinkedHashMap(entry(oneRing, frodo),
* entry(nenya, galadriel),
* entry(narya, gandalf));
* entry(nenya, galadriel),
* entry(narya, gandalf));
*
* // assertion will pass
* assertThat(ringBearers).containsExactly(entry(oneRing, frodo),
Expand Down Expand Up @@ -841,4 +842,33 @@ public S withFailMessage(String newErrorMessage, Object... args) {
public S withThreadDumpOnError() {
return super.withThreadDumpOnError();
}

/**
* Return an {@code Assert} object that allows to perform assertions on the size of the {@link Map} under test.
* <p>
* Once this method is called, the object under test is no more the initial {@link Map} but its size,
* to perform assertions on the initial {@link Map}, call {@link AbstractMapSizeAssert#returnToMap()}.
* <p>
* Example :
* <pre><code class='java'> Map&lt;Ring, TolkienCharacter&gt; ringBearers = newHashMap(entry(oneRing, frodo),
* entry(nenya, galadriel),
* entry(narya, gandalf));
*
* // assertion will pass:
* assertThat(ringBearers).size().isGreaterThan(1)
* .isLessThanOrEqualTo(3)
* returnToMap().contains(entry(oneRing, frodo),
* entry(nenya, galadriel),
* entry(narya, gandalf));
*
* // assertion will fail:
* assertThat(ringBearers).size().isGreaterThan(5);</code></pre>
*
* @throws NullPointerException if the given map is {@code null}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public AbstractMapSizeAssert<S, A, K, V> size() {
Preconditions.checkNotNull(actual, "Can not perform assertions on the size of a null map.");
return new MapSizeAssert(this, actual.size());
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractMapSizeAssert.java
@@ -0,0 +1,25 @@
/**
* 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.Map;

public abstract class AbstractMapSizeAssert<S extends AbstractMapAssert<S, A, K, V>, A extends Map<K, V>, K, V>
extends AbstractIntegerAssert<AbstractMapSizeAssert<S, A, K, V>> {

protected AbstractMapSizeAssert(Integer actual, Class<?> selfType) {
super(actual, selfType);
}

public abstract AbstractMapAssert<S, A, K, V> returnToMap();
}
30 changes: 30 additions & 0 deletions src/main/java/org/assertj/core/api/MapSizeAssert.java
@@ -0,0 +1,30 @@
/**
* 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.Map;

public class MapSizeAssert<K, V> extends AbstractMapSizeAssert<MapAssert<K, V>, Map<K, V>, K, V> {

private AbstractMapAssert<MapAssert<K, V>, Map<K, V>, K, V> source;

public MapSizeAssert(AbstractMapAssert<MapAssert<K, V>, Map<K, V>, K, V> source, Integer i) {
super(i, MapSizeAssert.class);
this.source = source;
}

@Override
public AbstractMapAssert<MapAssert<K, V>, Map<K, V>, K, V> returnToMap() {
return source;
}
}
Expand Up @@ -28,7 +28,7 @@ public class IterableAssert_size_Test {
public ExpectedException thrown = none();

@Test
public void should_be_able_to_use_integer_assertions_on_size_of_iterable() {
public void should_be_able_to_use_integer_assertions_on_iterable_size() {
Iterable<String> strings = new HashSet<String>(Arrays.asList("a", "b", "c"));
// @format:off
assertThat(strings).size().isGreaterThan(0)
Expand All @@ -44,9 +44,9 @@ public void should_be_able_to_use_integer_assertions_on_size_of_iterable() {
}

@Test
public void should_have_nice_error_message_when_size_is_used_on_iterable_which_is_null() {
public void should_have_an_helpful_error_message_when_size_is_used_on_a_null_iterable() {
Iterable<Integer> nullList = null;
thrown.expectNullPointerException("Can not assert on size of a null iterable.");
thrown.expectNullPointerException("Can not perform assertions on the size of a null iterable.");
assertThat(nullList).size().isGreaterThan(1);
}
}
48 changes: 48 additions & 0 deletions src/test/java/org/assertj/core/api/map/MapAssert_size_Test.java
@@ -0,0 +1,48 @@
/**
* 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.map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
import static org.assertj.core.test.ExpectedException.none;
import static org.assertj.core.test.Maps.mapOf;

import java.util.Map;

import org.assertj.core.test.ExpectedException;
import org.junit.Rule;
import org.junit.Test;

public class MapAssert_size_Test {

@Rule
public ExpectedException thrown = none();

@Test
@SuppressWarnings("unchecked")
public void should_be_able_to_use_integer_assertions_on_size_the_map_size() {
Map<String, String> stringToString = mapOf(entry("a", "1"), entry("b", "2"));
// @format:off
assertThat(stringToString).size().isGreaterThan(0)
.isLessThanOrEqualTo(3)
.returnToMap().contains(entry("a", "1"));
// @format:on
}

@Test
public void should_have_an_helpful_error_message_when_size_is_used_on_a_null_map() {
Map<String, String> nullMap = null;
thrown.expectNullPointerException("Can not perform assertions on the size of a null map.");
assertThat(nullMap).size().isGreaterThan(1);
}
}

0 comments on commit 608ede7

Please sign in to comment.