Added test for readonly collections

This commit is contained in:
Kristian Hellang
2015-01-06 09:19:45 +01:00
parent 6731a59263
commit 273ad8acdc
2 changed files with 32 additions and 0 deletions
+23
View File
@@ -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 }); }
+9
View File
@@ -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);
}
}
}