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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions Octokit.Tests/Models/PunchCardTests.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Octokit.Response;
using Xunit;

namespace Octokit.Tests.Models
public class PunchCardTests
{
public class PunchCardTests
public class TheConstructor
{
[Fact]
public void ThrowsExceptionWithNullPunchCardPoints()
{
Assert.Throws<ArgumentNullException>(()=>new PunchCard(null));
Assert.Throws<ArgumentNullException>(() => new PunchCard(null));
}

[Fact]
public void ThrowsExceptionWhenPunchCardPointsHaveIncorrectFormat()
{
IList<int> point1 = new []{1,2,3,4,5,6};
IEnumerable<IList<int>> points = new List<IList<int>>{point1};
IList<int> point1 = new[] { 1, 2, 3, 4, 5, 6 };
IEnumerable<IList<int>> points = new List<IList<int>> { point1 };
Assert.Throws<ArgumentException>(() => new PunchCard(points));
}

[Fact]
public void DoesNotThrowExceptionWhenPunchPointsHaveCorrectFormat()
{
IList<int> point1 = new[] { 1, 2, 3};
IList<int> point1 = new[] { 1, 2, 3 };
IEnumerable<IList<int>> points = new List<IList<int>> { point1 };
Assert.DoesNotThrow(() => new PunchCard(points));
}
Expand All @@ -35,7 +36,7 @@ public void CanQueryCommitsForDayAndHour()
IList<int> point1 = new[] { 1, 0, 3 };
IList<int> point2 = new[] { 1, 1, 4 };
IList<int> point3 = new[] { 1, 2, 0 };
IEnumerable<IList<int>> points = new List<IList<int>> { point1,point2,point3 };
IEnumerable<IList<int>> points = new List<IList<int>> { point1, point2, point3 };

var punchCard = new PunchCard(points);

Expand All @@ -44,10 +45,23 @@ public void CanQueryCommitsForDayAndHour()
var commitsAtMondayAt2Am = punchCard.GetCommitCountFor(DayOfWeek.Monday, 2);
var commitsAtTuesdayAt2Am = punchCard.GetCommitCountFor(DayOfWeek.Tuesday, 2);

Assert.Equal(3,commitsAtMondayAt12Am);
Assert.Equal(3, commitsAtMondayAt12Am);
Assert.Equal(4, commitsAtMondayAt1Am);
Assert.Equal(0, commitsAtMondayAt2Am);
Assert.Equal(0, commitsAtTuesdayAt2Am);
}

[Fact]
public void SetsPunchPointsAsReadOnlyDictionary()
{
IList<int> point1 = new[] { 1, 0, 3 };
IList<int> point2 = new[] { 1, 1, 4 };
IList<int> point3 = new[] { 1, 2, 0 };
IEnumerable<IList<int>> points = new List<IList<int>> { point1, point2, point3 };

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

}
}
}
}
20 changes: 20 additions & 0 deletions Octokit.Tests/Models/SearchCodeRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using Octokit;
using Xunit;

public class SearchCodeRequestTests
{
public class TheMergedQualifiersMethod
{
[Fact]
public void ReturnsAReadOnlyDictionary()
{
var request = new SearchCodeRequest("test");

var result = request.MergedQualifiers();

// If I can cast this to a writeable collection, then that defeats the purpose of a read only.
Assert.Null(result as ICollection<string>);
Copy link
Member

Choose a reason for hiding this comment

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

⬆️

}
}
}
20 changes: 20 additions & 0 deletions Octokit.Tests/Models/SearchIssuesRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using Octokit;
using Xunit;

internal class SearchIssuesRequestTests
{
public class TheMergedQualifiersMethod
{
[Fact]
public void ReturnsAReadOnlyDictionary()
{
var request = new SearchIssuesRequest("test");

var result = request.MergedQualifiers();

// If I can cast this to a writeable collection, then that defeats the purpose of a read only.
Assert.Null(result as ICollection<string>);
Copy link
Member

Choose a reason for hiding this comment

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

⬆️

}
}
}
20 changes: 20 additions & 0 deletions Octokit.Tests/Models/SearchUsersRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using Octokit;
using Xunit;

internal class SearchUsersRequestTests
{
public class TheMergedQualifiersMethod
{
[Fact]
public void ReturnsAReadOnlyDictionary()
{
var request = new SearchUsersRequest("test");

var result = request.MergedQualifiers();

// If I can cast this to a writeable collection, then that defeats the purpose of a read only.
Assert.Null(result as ICollection<string>);
Copy link
Member

Choose a reason for hiding this comment

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

⬆️

}
}
}
3 changes: 3 additions & 0 deletions Octokit.Tests/Octokit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
<Compile Include="Models\ReadOnlyPagedCollectionTests.cs" />
<Compile Include="Models\RepositoryUpdateTests.cs" />
<Compile Include="Models\RequestParametersTests.cs" />
<Compile Include="Models\SearchCodeRequestTests.cs" />
<Compile Include="Models\SearchUsersRequestTests.cs" />
<Compile Include="Models\SearchIssuesRequestTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helpers\StringExtensionsTests.cs" />
<Compile Include="Clients\RepositoriesClientTests.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Octokit/Models/Request/SearchCodeRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -157,7 +158,7 @@ public override IReadOnlyCollection<string> MergedQualifiers()
parameters.Add(String.Format(CultureInfo.InvariantCulture, "repo:{0}", Repo));
}

return parameters;
return new ReadOnlyCollection<string>(parameters);
}

internal string DebuggerDisplay
Expand Down
5 changes: 3 additions & 2 deletions Octokit/Models/Request/SearchIssuesRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Octokit.Internal;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -259,7 +260,7 @@ public override IReadOnlyCollection<string> MergedQualifiers()
parameters.Add(String.Format(CultureInfo.InvariantCulture, "repo:{0}", Repo));
}

return parameters;
return new ReadOnlyCollection<string>(parameters);
}

internal string DebuggerDisplay
Expand Down
4 changes: 3 additions & 1 deletion Octokit/Models/Response/PunchCard.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
Expand All @@ -12,7 +13,8 @@ public class PunchCard
public PunchCard(IEnumerable<IList<int>> punchCardData)
{
Ensure.ArgumentNotNull(punchCardData, "punchCardData");
PunchPoints = punchCardData.Select(point => new PunchCardPoint(point)).ToList();
PunchPoints = new ReadOnlyCollection<PunchCardPoint>(
punchCardData.Select(point => new PunchCardPoint(point)).ToList());
}

/// <summary>
Expand Down