-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Make readonly collections truly readonly #394
Conversation
These methods actually returned mutable collections that happen to implement the readonly interfaces. User could cast them to the actual type and add things. I'd rather avoid even that possibility by making these truly return readonly stuff.
|
||
var punchCard = new PunchCard(points); | ||
|
||
Assert.Null(punchCard.PunchPoints as ICollection<PunchCardPoint>); |
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.
Assert.IsNotType<ICollection<PunchCardPoint>>(punchCard.PunchPoints)
?
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.
Nope. That asserts that it's exactly that type. I want to test the is a
relationship. Unfortunately there's not a built in assertion for exactly this.
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.
Of course it does, silly me.
What about an custom assert that does the opposite of Assert.IsAssignableFrom
to make this a bit more descriptive?
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.
Well I think it reads just fine, but I could add AssertEx.IsNotAssignableFrom
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.
Or...
public static void IsReadOnlyCollection<T>(object instance) where T : class
{
var collection = instance as ICollection<T>;
Assert.True(collection == null || collection.IsReadOnly);
}
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.
👍 to IsReadOnlyCollection
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.
Even better.
public static void IsReadOnlyCollection<T>(object instance) where T : class
{
var collection = instance as ICollection<T>;
// The collection == null case is for .NET 4.0
Assert.True(instance is IReadOnlyCollection<T> && (collection == null || collection.IsReadOnly));
}
Looks good. Perhaps making the test a bit more readable by using IsNotType is the only improvement I can think of... |
Make readonly collections truly readonly
These methods actually returned mutable collections that happen
to implement the readonly interfaces. User could cast them to
the actual type and add things. I'd rather avoid even that
possibility by making these truly return readonly stuff.