-
-
Notifications
You must be signed in to change notification settings - Fork 683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isUnmodifiable
assertion for Collections
#2102
Comments
That would be cool! How would we assert the list is unmodifiable under the hood? list.getClass().isInstance(Collections.unmodifiableList(Arrays.asList("any value"))) This should work if |
@HaydenMeloche that would cover the JDK types but if I use a Guava ImmutableCollection that won't work so the best implementation IMO is following the possible workaround by calling clear and catching the UnsupportedOperationException, if no exceptin is caught then fail the assertion |
@joel-costigliola Fair point. I'd be willing to tackle this if you think it's worth working on :) |
Sure, go ahead, thanks! |
@HaydenMeloche thank you! Before start working on it please let me put together a more detailed description of the approach I have in mind, so we can blame it also considering Guava. I should be able to do it today or tomorrow max. |
I added some more details and I'll complete them during the weekend. However, this should give already a feeling so please let me know your thoughts. I'd suggest starting the implementation of |
@scordio Thanks for the detailed explanation. I'll get started when I have some time this week. I might create a draft PR when I have something concrete so I can get feedback |
hey @scordio got a chance to working on this over the weekend. I setup my assertion class as such public abstract class AbstractCollectionAssert <SELF extends AbstractCollectionAssert<SELF, ACTUAL, ELEMENT, ELEMENT_ASSERT>,
ACTUAL extends java.util.Collection<? extends ELEMENT>,
ELEMENT,
ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT, ELEMENT>>
extends AbstractIterableAssert<SELF, ACTUAL, ELEMENT, ELEMENT_ASSERT>
implements IndexedObjectEnumerableAssert<SELF, ELEMENT> { I'm running into an issue now with this setup. Existing test cases that test the Ex: This test case: Gives error:
In Assertions.java I setup the collection assert as public static <ELEMENT> CollectionAssert<ELEMENT> assertThat(Collection<? extends ELEMENT> actual) {
return AssertionsForInterfaceTypes.assertThat(actual);
} Since |
Hi @HaydenMeloche, yes please, just push/raise a draft PR and I'll have a look. |
Summary
Considering the
java.util.Collections.unmodifiableXXX()
methods, we could add aisUnmodifiable()
method to the assert classes for collections.Implementation Proposal
Under the hood, the assertion should try to call all the collection APIs which are expected to throw
UnsupportedOperationException
. The assertion succeeds if all of them are throwing the expected exception, otherwise it fails.We could define a new
AbstractCollectionAssert
which would inherit fromAbstractIterableAssert
and would be the parent class for all the other assert classes for collection subtypes.AbstractCollectionAssert
would exposeisUnmodifiable()
. Also,Assertions.assertThat(Collection)
andBDDAssertions.then(Collection)
would be needed.Below is the mapping between the collection type and the APIs to test. Each subtype would cover also the methods of the parent type. E.g., if we have a
List
, the assertion should also verifyCollection
methods.Collection
add(E)
,addAll(Collection)
,clear()
,iterator().remove()
,remove(Object)
,removeAll(Collection)
,removeIf(Predicate)
,retainAll(Collection)
List
add(int, E)
,addAll(int, Collection)
,listIterator().add(E)
,listIterator().remove()
,listIterator().set(E)
,remove(int)
,replaceAll(UnaryOperator)
,set(int, E)
,sort(Comparator)
NavigableSet
descendingIterator().remove()
,pollFirst()
,pollLast()
Set
SortedSet
Example
The text was updated successfully, but these errors were encountered: