-
-
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 Iterable
instances
#2974
Comments
I can take a look at this one, if nobody else is yet. |
Sure, go for it, @etellman! |
Extract finding mutating methods into a separate class, MutatingMethodFinder which just looks for mutating methods in collections. Add unit tests for MutatingMethodFinder which test all of the mutating methods in the supported collection types. Move the assertion up from AbstractCollectionAssert to AbstractIterableAssert. see: assertj#2974
I think I got a little carried away with tests and moving things around, but here's a PR: #3160 Since I added tests that make all of the methods but one unsupported and verify that the class is still identified as mutable. All the common mutable collections implement The only slight change to the behavior is that I treated throwing a documented exceptions other than Since the concrete classes know what kind of collection they have, I pushed this decision down to the concrete classes. If an assertion class knows that it has a list, it doesn't need to check whether actual is an instance of a list. This is avoids a little unnecessary runtime type checking. This also forces the author of a concrete assertion class to think about what type of collection the assertion assertions are for and perhaps write a test. I added a test for checking whether unmodifiable Guava Multisets are really unmodifiabele and found, I think, a bug. I didn't get the expected A disadvantage of this approach is that the concrete classes have to think about what kind of collection they have. An alternative would be to get rid of the visitor idea and have It looks like there's a binary compatibility issue. I ran |
Here's issue in google/guava for changing the behavior of It looks like it's technically OK to either throw |
Hey @etellman, thanks a lot for your effort! I haven't managed to look at your changes yet but I'll do it as soon as I can. |
No problem. google/guava#6702 has generated a surprisingly high amount of discussion with the Guava people. I had assumed it would be uncontroversial, but they're a bit reluctant to change anything, for fear of somebody counting on |
The Guava people approved google/guava#6702. It turns out that this class is rarely used and they're confident either way will be fine. I thought about this a bit more, however, and the unmodifiability checks so could be changed so it wouldn't matter.... The checks now are trying to verify that every potentially mutating method can be made to throw an For example, it makes sense for a class that can only represent an empty list to implement The distinction is whether "is unmodifiable" means "this particular instance will never change" or "any instance of the class that this is an instance of will never change". The second thing isn't possible to automatically determine, which is why there is a need for lists of pre-approved collection types now. I did a little POC of this approach, and it seems to work. I can open a new issue/PR to discuss it, however, as this one is already pretty large. |
Since nobody has looked at this change yet anyway, I just added the above idea to the existing PR (#3160). This means that the special cases for known unmodifiable classes aren't needed. Sorry, this change is pretty big and more than the originally-requested "move a method to a different class". Much of it is new tests, however, and not needing lists of pre-approved known unmodifiable classes seems easier to maintain and more general. I'm going to stop messing with this now---feel free to do what you want with it. |
isUnmodifiable
assertion for IterablesisUnmodifiable
assertion for Iterable
instances
Feature summary
Similarly to #2102, we could have an
isUnmodifiable()
assertion forIterable
types.We might consider moving
AbstractCollectionAssert::isUnmodifiable
toAbstractIterableAssert
, as long as it's binary compatible. Otherwise, we might move the inner implementation and keep the existing public method, to be removed in version 4.Example
The text was updated successfully, but these errors were encountered: