Skip to content

Commit

Permalink
Add custom assert for readonly collection
Browse files Browse the repository at this point in the history
  • Loading branch information
haacked committed Feb 27, 2014
1 parent 9eb14f3 commit 5430718
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Octokit.Tests/Helpers/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,12 @@ public static async Task ThrowsWhenGivenWhitespaceArgument(Func<string, Task> ac
await Throws<ArgumentException>(async () => await action(argument));
}
}

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));
}
}
}
3 changes: 2 additions & 1 deletion Octokit.Tests/Models/PunchCardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Octokit.Response;
using Octokit.Tests.Helpers;
using Xunit;

public class PunchCardTests
Expand Down Expand Up @@ -61,7 +62,7 @@ public void SetsPunchPointsAsReadOnlyDictionary()

var punchCard = new PunchCard(points);

Assert.Null(punchCard.PunchPoints as ICollection<PunchCardPoint>);
AssertEx.IsReadOnlyCollection<PunchCardPoint>(punchCard.PunchPoints);
}
}
}
6 changes: 3 additions & 3 deletions Octokit.Tests/Models/SearchCodeRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using Octokit;
using Octokit;
using Octokit.Tests.Helpers;
using Xunit;

public class SearchCodeRequestTests
Expand All @@ -14,7 +14,7 @@ public void ReturnsAReadOnlyDictionary()
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>);
AssertEx.IsReadOnlyCollection<string>(result);
}
}
}
6 changes: 3 additions & 3 deletions Octokit.Tests/Models/SearchIssuesRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using Octokit;
using Octokit;
using Octokit.Tests.Helpers;
using Xunit;

internal class SearchIssuesRequestTests
Expand All @@ -14,7 +14,7 @@ public void ReturnsAReadOnlyDictionary()
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>);
AssertEx.IsReadOnlyCollection<string>(result);
}
}
}
6 changes: 3 additions & 3 deletions Octokit.Tests/Models/SearchUsersRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using Octokit;
using Octokit;
using Octokit.Tests.Helpers;
using Xunit;

internal class SearchUsersRequestTests
Expand All @@ -14,7 +14,7 @@ public void ReturnsAReadOnlyDictionary()
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>);
AssertEx.IsReadOnlyCollection<string>(result);
}
}
}

0 comments on commit 5430718

Please sign in to comment.