Switch to TheoryData and clean up tests

This commit is contained in:
Cody Robibero 2021-08-21 17:31:06 -06:00
parent 468283bfb2
commit 50b3d74c95

View File

@ -1,51 +1,16 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Jellyfin.Data.Enums;
using Xunit;
using Xunit.Sdk;
namespace Jellyfin.Server.Implementations.Tests.TypedBaseItem
{
public class BaseItemKindTests
{
[Theory]
[ClassData(typeof(GetBaseItemDescendants))]
public void EnumParse_GivenValidBaseItemType_ReturnsEnumValue(Type baseItemType)
public static TheoryData<Type> BaseItemKind_TestData()
{
var enumValue = Enum.Parse<BaseItemKind>(baseItemType.Name);
Assert.True(Enum.IsDefined(typeof(BaseItemKind), enumValue));
}
var data = new TheoryData<Type>();
[Theory]
[ClassData(typeof(GetBaseItemDescendants))]
public void GetBaseItemKind_WhenCalledAfterDefaultCtor_DoesNotThrow(Type baseItemDescendantType)
{
var defaultConstructor = baseItemDescendantType.GetConstructor(Type.EmptyTypes);
var instance = (MediaBrowser.Controller.Entities.BaseItem)defaultConstructor!.Invoke(null);
var exception = Record.Exception(() => instance.GetBaseItemKind());
Assert.Null(exception);
}
private class GetBaseItemDescendants : IEnumerable<object?[]>
{
private static bool IsProjectAssemblyName(string? name)
{
if (name == null)
{
return false;
}
return name.StartsWith("Jellyfin", StringComparison.OrdinalIgnoreCase)
|| name.StartsWith("Emby", StringComparison.OrdinalIgnoreCase)
|| name.StartsWith("MediaBrowser", StringComparison.OrdinalIgnoreCase);
}
public IEnumerator<object?[]> GetEnumerator()
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in loadedAssemblies)
{
@ -57,35 +22,42 @@ namespace Jellyfin.Server.Implementations.Tests.TypedBaseItem
&& targetType.IsSubclassOf(typeof(MediaBrowser.Controller.Entities.BaseItem)));
foreach (var baseItemType in baseItemTypes)
{
yield return new object?[] { baseItemType };
data.Add(baseItemType);
}
}
}
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (path == null)
{
throw new NullException("Assembly location is null");
return data;
}
foreach (string dll in Directory.GetFiles(path, "*.dll"))
[Theory]
[MemberData(nameof(BaseItemKind_TestData))]
public void EnumParse_GivenValidBaseItemType_ReturnsEnumValue(Type baseItemDescendantType)
{
var assembly = Assembly.LoadFile(dll);
if (IsProjectAssemblyName(assembly.FullName))
{
var baseItemTypes = assembly.GetTypes()
.Where(targetType => targetType.IsClass
&& !targetType.IsAbstract
&& targetType.IsSubclassOf(typeof(MediaBrowser.Controller.Entities.BaseItem)));
foreach (var baseItemType in baseItemTypes)
{
yield return new object?[] { baseItemType };
}
}
}
var enumValue = Enum.Parse<BaseItemKind>(baseItemDescendantType.Name);
Assert.True(Enum.IsDefined(typeof(BaseItemKind), enumValue));
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
[Theory]
[MemberData(nameof(BaseItemKind_TestData))]
public void GetBaseItemKind_WhenCalledAfterDefaultCtor_DoesNotThrow(Type baseItemDescendantType)
{
var defaultConstructor = baseItemDescendantType.GetConstructor(Type.EmptyTypes);
var instance = (MediaBrowser.Controller.Entities.BaseItem)defaultConstructor!.Invoke(null);
var exception = Record.Exception(() => instance.GetBaseItemKind());
Assert.Null(exception);
}
private static bool IsProjectAssemblyName(string? name)
{
if (name == null)
{
return false;
}
return name.StartsWith("Jellyfin", StringComparison.OrdinalIgnoreCase)
|| name.StartsWith("Emby", StringComparison.OrdinalIgnoreCase)
|| name.StartsWith("MediaBrowser", StringComparison.OrdinalIgnoreCase);
}
}
}