mirror of
https://github.com/zoriya/octokit.net.git
synced 2025-12-05 23:06:10 +00:00
* Added StringEnum<TEnum> * Added tests * Make sure the serializer can work with StringEnum * Use StringEnum for EventInfo.Event * Add convention test to assert that all Response models use StringEnum<> to wrap enum properties * Add Stringnum<> to all response types failing convention test * Handle StringEnum to Enum conversion when Issue response model populates IssueUpdate request model * Fix unit test * Refactor SimpleJsonSerializer to expose the DeserializeEnum strategy so it can be used in StringEnum class * Need to expose/use SerializeEnum functionality too, so we use the correct string representation of enum values that have custom properties (eg ReactionType Plus1 to "+1") * fix unit tests, since the string is now the "correct" upstream api value * Add a couple of tests for the Enum serialize/deserialize when underscores, hyphens and custom property attributes are present * Compare parsed values for equality * add convention test to ensure enum members all have Parameter property set * update test to cover implicit conversions too * this test should work but fails at the moment due to magic hyphen removal in deserializer causing a one way trip from utf-8 to EncodingType.Utf8 with no way to get back * (unsuccesfully) expand event info test to try to catch more cases of unknown event types * fix broken integration test while im here * Fixed build errors after .NET Core merge * Value -> StringValue, ParsedValue -> Value * Don't allow StringValue to be null * Ignore enums not used in request/response models * Added ParameterAttribute to almost all enum values * Ignore Language enum * Fix failing tests * Fix milestone sort parameter and tests * whitespace * fix milestone unit tests * Fix StringEnum.Equals ... This could've been embarrassing! * Change SimpleJsonSerializer Enum handling to only use `[Parameter()]` attributes (no more magic removal of hyphen/underscores from strings) * Tidy up this integration test while im here * Only test request/response enums in convention test * Keep skipping Language * Remove unused method * Remove excluded enum types * Removed unnecessary ParameterAttributes * Remove unused enum * Add StringEnum test for string-comparison of two invalid values * Bring back IssueCommentSort and use it in IssueCommentRequest This reverts commit 38a4a291d1476ef8c992fe0f76956974b6f32a49. * Use assembly instead of namespace for Octokit check * Add failing test to reproduce the issue where only the first enum paramter/value was added to the cache * Fix deserializer enum cache to include all enum members rather than only the first member encountered * Use a static SimpleJsonSerializer in StringEnum * Remove serializer instance in StringEnum * Add some documentation on StringEnum<TEnum> * Fix parameter value to resolve failing integration test
288 lines
9.3 KiB
C#
288 lines
9.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using Xunit;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using Octokit.Internal;
|
|
|
|
namespace Octokit.Tests.Conventions
|
|
{
|
|
public class ModelTests
|
|
{
|
|
private static readonly Assembly Octokit = typeof(AuthorizationUpdate).GetTypeInfo().Assembly;
|
|
|
|
[Theory]
|
|
[MemberData("ModelTypes")]
|
|
public void AllModelsHaveDebuggerDisplayAttribute(Type modelType)
|
|
{
|
|
var attribute = modelType.GetTypeInfo().GetCustomAttribute<DebuggerDisplayAttribute>(inherit: false);
|
|
if (attribute == null)
|
|
{
|
|
throw new MissingDebuggerDisplayAttributeException(modelType);
|
|
}
|
|
|
|
if (attribute.Value != "{DebuggerDisplay,nq}")
|
|
{
|
|
throw new InvalidDebuggerDisplayAttributeValueException(modelType, attribute.Value);
|
|
}
|
|
|
|
var property = modelType.GetProperty("DebuggerDisplay", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
if (property == null)
|
|
{
|
|
throw new MissingDebuggerDisplayPropertyException(modelType);
|
|
}
|
|
|
|
if (property.PropertyType != typeof(string))
|
|
{
|
|
throw new InvalidDebuggerDisplayReturnType(modelType, property.PropertyType);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("ResponseModelTypes")]
|
|
public void AllResponseModelsHavePublicParameterlessCtors(Type modelType)
|
|
{
|
|
var ctor = modelType.GetConstructor(Type.EmptyTypes);
|
|
|
|
if (ctor == null || !ctor.IsPublic)
|
|
{
|
|
throw new MissingPublicParameterlessCtorException(modelType);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("ResponseModelTypes")]
|
|
public void ResponseModelsHaveGetterOnlyProperties(Type modelType)
|
|
{
|
|
var mutableProperties = new List<PropertyInfo>();
|
|
|
|
foreach (var property in modelType.GetProperties())
|
|
{
|
|
var setter = property.GetSetMethod(nonPublic: true);
|
|
|
|
if (setter == null || !setter.IsPublic)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
mutableProperties.Add(property);
|
|
}
|
|
|
|
if (mutableProperties.Any())
|
|
{
|
|
throw new MutableModelPropertiesException(modelType, mutableProperties);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("ResponseModelTypes")]
|
|
public void ResponseModelsHaveReadOnlyCollections(Type modelType)
|
|
{
|
|
var mutableCollectionProperties = new List<PropertyInfo>();
|
|
|
|
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;
|
|
}
|
|
|
|
if (propertyType.IsReadOnlyCollection())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
mutableCollectionProperties.Add(property);
|
|
}
|
|
}
|
|
|
|
if (mutableCollectionProperties.Any())
|
|
{
|
|
throw new MutableModelPropertiesException(modelType, mutableCollectionProperties);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("ResponseModelTypes")]
|
|
public void ResponseModelsUseStringEnumWrapper(Type modelType)
|
|
{
|
|
var enumProperties = modelType.GetProperties()
|
|
.Where(x => x.PropertyType.GetTypeInfo().IsEnum);
|
|
|
|
if (enumProperties.Any())
|
|
{
|
|
throw new ModelNotUsingStringEnumException(modelType, enumProperties);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("ModelTypesWithUrlProperties")]
|
|
public void ModelsHaveUrlPropertiesOfTypeString(Type modelType)
|
|
{
|
|
var propertiesWithInvalidType = modelType
|
|
.GetProperties()
|
|
.Where(IsUrlProperty)
|
|
.Where(x => x.PropertyType != typeof(string))
|
|
.ToList();
|
|
|
|
if (propertiesWithInvalidType.Count > 0)
|
|
{
|
|
throw new InvalidUrlPropertyTypeException(modelType, propertiesWithInvalidType);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData("EnumTypes")]
|
|
public void EnumMembersHaveParameterAttribute(Type enumType)
|
|
{
|
|
if (enumType == typeof(Language))
|
|
{
|
|
return; // TODO: Annotate all Language entries with a ParameterAttribute.
|
|
}
|
|
|
|
var membersWithoutProperty = enumType.GetRuntimeFields()
|
|
.Where(x => x.Name != "value__")
|
|
.Where(x => x.GetCustomAttribute(typeof(ParameterAttribute), false) == null);
|
|
|
|
if (membersWithoutProperty.Any())
|
|
{
|
|
throw new EnumMissingParameterAttributeException(enumType, membersWithoutProperty);
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<object[]> ModelTypes
|
|
{
|
|
get { return GetModelTypes(includeRequestModels: true).Select(type => new[] { type }); }
|
|
}
|
|
|
|
public static IEnumerable<object[]> ModelTypesWithUrlProperties
|
|
{
|
|
get
|
|
{
|
|
return GetModelTypes(includeRequestModels: true)
|
|
.Where(type => type.GetProperties().Any(IsUrlProperty))
|
|
.Select(type => new[] { type });
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<object[]> ResponseModelTypes
|
|
{
|
|
get { return GetModelTypes(includeRequestModels: false).Select(type => new[] { type }); }
|
|
}
|
|
|
|
public static IEnumerable<object[]> EnumTypes
|
|
{
|
|
get
|
|
{
|
|
return GetModelTypes(includeRequestModels: true)
|
|
.SelectMany(type => type.GetProperties())
|
|
.SelectMany(property => UnwrapGenericArguments(property.PropertyType))
|
|
.Where(type => type.GetTypeInfo().Assembly.Equals(Octokit) && type.GetTypeInfo().IsEnum)
|
|
.Select(type => new[] { type });
|
|
}
|
|
}
|
|
|
|
private static IEnumerable<Type> GetModelTypes(bool includeRequestModels)
|
|
{
|
|
var allModelTypes = new HashSet<Type>();
|
|
|
|
var clientInterfaces = typeof(IGitHubClient).GetTypeInfo().Assembly.ExportedTypes
|
|
.Where(type => type.IsClientInterface());
|
|
|
|
foreach (var exportedType in clientInterfaces)
|
|
{
|
|
var methods = exportedType.GetMethods();
|
|
|
|
var modelTypes = methods.SelectMany(method => UnwrapGenericArguments(method.ReturnType));
|
|
|
|
if (includeRequestModels)
|
|
{
|
|
var requestModels = methods.SelectMany(method => method.GetParameters(),
|
|
(method, parameter) => parameter.ParameterType);
|
|
|
|
modelTypes = modelTypes.Union(requestModels);
|
|
}
|
|
|
|
foreach (var modelType in modelTypes.Where(type => type.IsModel()))
|
|
{
|
|
allModelTypes.Add(modelType);
|
|
}
|
|
}
|
|
|
|
return GetAllModelTypes(allModelTypes);
|
|
}
|
|
|
|
static IEnumerable<Type> GetAllModelTypes(ISet<Type> allModelTypes)
|
|
{
|
|
foreach (var modelType in allModelTypes.ToList())
|
|
{
|
|
GetPropertyModelTypes(modelType, allModelTypes);
|
|
}
|
|
|
|
return allModelTypes;
|
|
}
|
|
|
|
static void GetPropertyModelTypes(Type modelType, ISet<Type> allModelTypes)
|
|
{
|
|
var properties = modelType.GetProperties();
|
|
|
|
foreach (var propertyType in properties.SelectMany(x => UnwrapGenericArguments(x.PropertyType)))
|
|
{
|
|
if (allModelTypes.Contains(propertyType))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!propertyType.IsModel())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
allModelTypes.Add(propertyType);
|
|
|
|
GetPropertyModelTypes(propertyType, allModelTypes);
|
|
}
|
|
}
|
|
|
|
private static IEnumerable<Type> UnwrapGenericArguments(Type returnType)
|
|
{
|
|
if (returnType.GetTypeInfo().IsGenericType)
|
|
{
|
|
var arguments = returnType.GetGenericArguments();
|
|
|
|
foreach (var argument in arguments)
|
|
{
|
|
if (argument.IsModel())
|
|
{
|
|
yield return argument;
|
|
}
|
|
else
|
|
{
|
|
foreach (var unwrappedTypes in UnwrapGenericArguments(argument))
|
|
{
|
|
yield return unwrappedTypes;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
yield return returnType;
|
|
}
|
|
}
|
|
|
|
private static bool IsUrlProperty(PropertyInfo property)
|
|
{
|
|
return property.Name.EndsWith("Url");
|
|
}
|
|
}
|
|
}
|