Skip to content
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

Merged
merged 2 commits into from
Feb 27, 2014

Conversation

haacked
Copy link
Contributor

@haacked haacked commented Feb 27, 2014

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.

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>);
Copy link
Member

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) ?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to IsReadOnlyCollection

Copy link
Contributor Author

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));
}

@shiftkey
Copy link
Member

Looks good.

Perhaps making the test a bit more readable by using IsNotType is the only improvement I can think of...

@shiftkey
Copy link
Member

:shipit:

shiftkey added a commit that referenced this pull request Feb 27, 2014
@shiftkey shiftkey merged commit 764b27e into master Feb 27, 2014
@shiftkey shiftkey deleted the haacked/proper-readonly-collections branch February 27, 2014 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants