Enhance BaseItemKindTests

pull/5228/head
crobibero 3 years ago
parent 790b284184
commit ac76519081

@ -1,9 +1,12 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using MediaBrowser.Controller.Entities; using System.Reflection;
using Jellyfin.Data.Enums;
using Xunit; using Xunit;
using Xunit.Sdk;
namespace Jellyfin.Server.Implementations.Tests.BaseItem namespace Jellyfin.Server.Implementations.Tests.BaseItem
{ {
@ -11,7 +14,15 @@ namespace Jellyfin.Server.Implementations.Tests.BaseItem
{ {
[Theory] [Theory]
[ClassData(typeof(GetBaseItemDescendant))] [ClassData(typeof(GetBaseItemDescendant))]
public void BaseKindEnumTest(Type baseItemDescendantType) public void BaseItemKindEnumTest(Type baseItemType)
{
var enumValue = Enum.Parse<BaseItemKind>(baseItemType.Name);
Assert.True(Enum.IsDefined(typeof(BaseItemKind), enumValue));
}
[Theory]
[ClassData(typeof(GetBaseItemDescendant))]
public void GetBaseKindEnumTest(Type baseItemDescendantType)
{ {
var defaultConstructor = baseItemDescendantType.GetConstructor(Type.EmptyTypes); var defaultConstructor = baseItemDescendantType.GetConstructor(Type.EmptyTypes);
@ -24,34 +35,58 @@ namespace Jellyfin.Server.Implementations.Tests.BaseItem
} }
} }
private static bool IsProjectAssemblyName(string? name) private class GetBaseItemDescendant : IEnumerable<object?[]>
{ {
if (name == null) private static bool IsProjectAssemblyName(string? name)
{ {
return false; if (name == null)
} {
return false;
}
return name.Contains("Jellyfin", StringComparison.InvariantCulture) return name.StartsWith("Jellyfin", StringComparison.OrdinalIgnoreCase)
|| name.Contains("Emby", StringComparison.InvariantCulture) || name.StartsWith("Emby", StringComparison.OrdinalIgnoreCase)
|| name.Contains("MediaBrowser", StringComparison.InvariantCulture) || name.StartsWith("MediaBrowser", StringComparison.OrdinalIgnoreCase)
|| name.Contains("RSSDP", StringComparison.InvariantCulture); || name.StartsWith("RSSDP", StringComparison.OrdinalIgnoreCase);
} }
private class GetBaseItemDescendant : IEnumerable<object?[]>
{
public IEnumerator<object?[]> GetEnumerator() public IEnumerator<object?[]> GetEnumerator()
{ {
var projectAssemblies = AppDomain.CurrentDomain.GetAssemblies() var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
.Where(x => IsProjectAssemblyName(x.FullName)); foreach (var assembly in loadedAssemblies)
{
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 };
}
}
}
foreach (var projectAssembly in projectAssemblies) var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (path == null)
{ {
var baseItemDescendantTypes = projectAssembly.GetTypes() throw new NullException("Assembly location is null");
.Where(targetType => targetType.IsClass && !targetType.IsAbstract && targetType.IsSubclassOf(typeof(MediaBrowser.Controller.Entities.BaseItem))); }
foreach (var descendantType in baseItemDescendantTypes) foreach (string dll in Directory.GetFiles(path, "*.dll"))
{
var assembly = Assembly.LoadFile(dll);
if (IsProjectAssemblyName(assembly.FullName))
{ {
yield return new object?[] { descendantType }; 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 };
}
} }
} }
} }

Loading…
Cancel
Save