Skip to content

Commit

Permalink
enable fluent assertions on size of list assertj#301, assertj#589
Browse files Browse the repository at this point in the history
tried change to "enable fluent assertions on size of iterable"
  • Loading branch information
PascalSchumacher committed Apr 17, 2016
1 parent 5fcd421 commit a2df960
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
Expand Up @@ -44,6 +44,8 @@
import org.assertj.core.internal.ObjectArrays;
import org.assertj.core.internal.Objects;
import org.assertj.core.internal.OnFieldsComparator;
import org.assertj.core.util.IterableUtil;
import org.assertj.core.util.Preconditions;
import org.assertj.core.util.VisibleForTesting;
import org.assertj.core.util.introspection.IntrospectionError;

Expand Down Expand Up @@ -1515,4 +1517,10 @@ public S withFailMessage(String newErrorMessage, Object... args) {
public S withThreadDumpOnError() {
return super.withThreadDumpOnError();
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public IterableSizeAssert<T, Integer> size() {
Preconditions.checkNotNull(actual, "Can not assert on size of iterable which is null.");
return new IterableSizeAssert(this, IterableUtil.sizeOf(actual));
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractListAssert.java
Expand Up @@ -18,6 +18,7 @@
import org.assertj.core.data.Index;
import org.assertj.core.description.Description;
import org.assertj.core.internal.*;
import org.assertj.core.util.Preconditions;
import org.assertj.core.util.VisibleForTesting;

/**
Expand Down Expand Up @@ -327,4 +328,9 @@ public S withThreadDumpOnError() {
return super.withThreadDumpOnError();
}

// @SuppressWarnings({ "rawtypes", "unchecked" })
// public IterableSizeAssert<T, Integer> size() {
// Preconditions.checkNotNull(actual, "Can not assert on size of list which is null.");
// return new IterableSizeAssert(this, actual.size());
// }
}
24 changes: 24 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractListSizeAssert.java
@@ -0,0 +1,24 @@
/**
* 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.List;

public abstract class AbstractListSizeAssert<S extends AbstractListAssert<S, A, T>, A extends List<? extends T>, T> extends AbstractIntegerAssert<ListSizeAssert<T, Integer>>{

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

public abstract S returnToList();
}
30 changes: 30 additions & 0 deletions src/main/java/org/assertj/core/api/ListSizeAssert.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.List;

public class ListSizeAssert<T, U> extends AbstractListSizeAssert<ListAssert<T>, List<? extends T>, T>{

private AbstractListAssert<ListAssert<T>, List<? extends T>, T> source;

public ListSizeAssert(AbstractListAssert<ListAssert<T>, List<? extends T>, T> source, Integer i) {
super(i, ListSizeAssert.class);
this.source = source;
}

@Override
public ListAssert<T> returnToList() {
return (ListAssert<T>) source;
}
}
46 changes: 46 additions & 0 deletions src/test/java/org/assertj/core/api/list/ListAssert_size_Test.java
@@ -0,0 +1,46 @@
/**
* 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.list;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.test.ExpectedException.none;

import java.util.Arrays;
import java.util.List;

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


public class ListAssert_size_Test {

@Rule
public ExpectedException thrown = none();

@Test
public void should_be_able_to_use_integer_assertions_on_size_of_list() {
List<String> strings = Arrays.asList("a", "b", "c");
assertThat(strings).size().isGreaterThan(0).isLessThanOrEqualTo(3).returnToIterable().contains("a").doesNotContain("d");

List<Integer> integers = Arrays.asList(1, 2, 3);
assertThat(integers).size().isGreaterThan(0).isLessThanOrEqualTo(3).returnToIterable().contains(1).doesNotContain(4);
}

@Test
public void should_have_nice_error_message_when_size_is_used_on_list_which_is_null() {
List<Integer> nullList = null;
thrown.expectNullPointerException("Can not assert on size of iterable which is null.");
assertThat(nullList).size().isGreaterThan(1);
}
}

0 comments on commit a2df960

Please sign in to comment.