mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-02 10:55:53 +00:00
Added test for readonly collections
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Octokit.Tests.Helpers;
|
||||
@@ -36,6 +37,28 @@ namespace Octokit.Tests.Conventions
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData("ResponseModelTypes")]
|
||||
public void ResponseModelsHaveReadOnlyCollections(Type modelType)
|
||||
{
|
||||
foreach (var property in modelType.GetProperties())
|
||||
{
|
||||
var propertyType = property.PropertyType;
|
||||
|
||||
if (typeof(IEnumerable).IsAssignableFrom(propertyType))
|
||||
{
|
||||
// Let's skip arrays as well for now.
|
||||
// There seems to be some special array handling in the Gist model.
|
||||
if (propertyType == typeof(string) || propertyType.IsArray)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
AssertEx.IsReadOnlyCollection(propertyType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> ModelTypes
|
||||
{
|
||||
get { return GetModelTypes(includeRequestModels: true).Select(type => new[] { type }); }
|
||||
|
||||
@@ -55,5 +55,14 @@ namespace Octokit.Tests.Helpers
|
||||
// The collection == null case is for .NET 4.0
|
||||
Assert.True(instance is IReadOnlyList<T> && (collection == null || collection.IsReadOnly));
|
||||
}
|
||||
|
||||
public static void IsReadOnlyCollection(Type type)
|
||||
{
|
||||
var isReadOnlyList = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IReadOnlyList<>);
|
||||
|
||||
var isReadOnlyDictionary = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>);
|
||||
|
||||
Assert.True(isReadOnlyList || isReadOnlyDictionary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user