-
Notifications
You must be signed in to change notification settings - Fork 731
Add support for IReadOnlyDictionary assertions #963
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
Conversation
ad5aa05 to
28adaf0
Compare
You mean that the compiler doesn't accept a |
|
@dennisdoomen the compiler does support having both overloads. |
|
Frankly, I don't like a |
|
I think I understand your point. It is in a sense a scratch on the glass. However, I dislike the fact that I do not have all dictionary assertions on I think that there is a decision to make. (A) We can go practical on this one and agree to some form of impurity (whether we will choose @jnyrup @dennisdoomen I would appreciate a clear decision on that matter, thanks. |
|
What about replacing the overload for |
Clever idea, I will try it out in the evening. |
28adaf0 to
3a59f77
Compare
|
Well, it worked suprisingly well. Please review the changes (ignoring Just few tests had to be adjusted, other than that it worked like a charm. |
|
The only test failing is Moreover, I have just cleaned up classes from initial |
|
From a unit test perspective I like the change, as the assertions shouldn't use any methods not available from But this would be both an ABI and API breaking change.
While the potential problems mentioned above are most likely only the case for a minority, my gut feeling is still that such a breaking change should be postponed to a major release. |
|
I have to agree with @jnyrup. Unless we can think of a way to prevent the breaking change, we'll have to postpone this to a future major release. |
|
Planning the feature for the next major release is not a problem at all. I will have time to polish the implementation and think about downsides. Thanks for mentioning the fact that |
|
I know there's been lots of talk how to support this, however, I think one way to solve it would be to have the dictionary methods on an public static class DictionaryExtension
{
public static void Example<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> value)
{
if (value is IDictionary<TKey, TValue> dictionary)
{
UseDictionary(dictionary);
}
else if (value is IReadOnlyDictionary<TKey, TValue> readOnlyDictionary)
{
UseReadOnlyDictionary(readOnlyDictionary);
}
else
{
throw new NotSupportedException("value must be a dictionary type");
}
}
private static void UseDictionary<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
{
}
private static void UseReadOnlyDictionary<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> readOnlyDictionary)
{
}
}The following works fine with the above: new Dictionary<string, int>().Example();The above works as both interfaces ( However, the above would match a Happy to have a stab at another PR if you prefer to avoid confusion with this one. |
|
I'm late for this but here's my two cents. The conflict happens not only for Still, these assertions are incomplete. They test only one form of enumerating the collection. For example, a collection that implements The collection may not even implement any interface or may have a public This is fine if you assume the iterators are well implemented but not if you're implementing an iterator. That's my case so I implemented a It would be great is this feature was added to the API. |
|
Now that we're accepting breaking changes again, I think it's time to reconsider this PR. |
# Conflicts: # Src/FluentAssertions/AssemblyInfo.cs # Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs # Tests/Shared.Specs/DictionaryEquivalencySpecs.cs # Tests/Shared.Specs/Shared.Specs.projitems
|
Hi again, I managed to merge with I have yet to learn what they are and how to fix them :-). Could you point me to some resources? Locally build.ps1 runs successfully. |
|
Ok, I merged with develop and fixed the approval tests. I changed What is important is that I had to disable one test which did not compile with the new API. It was the test that used a dictionary that did not implement
Do any more discussion/decisions/doubts come to your mind? |
|
What we could do is to provide an |
jnyrup
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to see you again @krajek!
I'm looking forward to getting this merged for 6.0 - I'm hitting this issue at work myself.
nit: We don't use AssemblyInfo.cs anymore, its content is now included in FluentAssertions.csproj.
| @@ -0,0 +1,3031 @@ | |||
| using System; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file seems to duplicate nearly all tests from GenericDictionaryAssertions.
I'm wondering what the purpose is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also not sure whether to "duplicate" all the tests. In general the older file tests dictionaries while the newer one tests read only dictionaries.
I wanted full coverage of both scenarios, so I basically copied all the tests. They were extremely useful during the development of the PR, now a little bit less I guess. I would err on the side on leaving them, but will not protest if your consensus would go the other way.
|
I think I adjusted the PR according to all your comments. Anything else to complete the task? BTW: I wrote simple Powershell script approving new API, maybe you will find it useful: Remove-Item *.approved.txt*
Get-ChildItem -Filter "*received.txt" | ForEach-Object {
$NewName = $_.FullName -replace 'received.txt', 'approved.txt'
Copy-Item $_.FullName $NewName
} |
Yeah, good thinking. We wanted to start updating https://github.com/fluentassertions/fluentassertions/blob/develop/docs/_pages/releases.md for anything on
Yep. Please add it to the root of the repo. Something like |
|
I have just added an entry in the |
# Conflicts: # Tests/Shared.Specs/Shared.Specs.projitems # docs/_pages/releases.md
|
I merged with the |
|
@jnyrup is this one still on your radar? |
Closes #568.
The main idea is that due to limitations of method overload resolution we cannot have
[...]Should(IReadOnlyDictionary<TKey,TValue>[...]. It will always conflict with overload acceptingIDictionary.Therefore I propose using
ShouldReadOnlyDictionaryname. It seems that it would be beneficial to cut this Gordian knot.Please let me know what you think. Upon acceptance, I will complete documentation etc.