BaseItemKindTests.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using Jellyfin.Data.Enums;
  8. using Xunit;
  9. using Xunit.Sdk;
  10. namespace Jellyfin.Server.Implementations.Tests.BaseItem
  11. {
  12. public class BaseItemKindTests
  13. {
  14. [Theory]
  15. [ClassData(typeof(GetBaseItemDescendants))]
  16. public void BaseItemKindEnumTest(Type baseItemType)
  17. {
  18. var enumValue = Enum.Parse<BaseItemKind>(baseItemType.Name);
  19. Assert.True(Enum.IsDefined(typeof(BaseItemKind), enumValue));
  20. }
  21. [Theory]
  22. [ClassData(typeof(GetBaseItemDescendants))]
  23. public void GetBaseKindEnumTest(Type baseItemDescendantType)
  24. {
  25. var defaultConstructor = baseItemDescendantType.GetConstructor(Type.EmptyTypes);
  26. var instance = (MediaBrowser.Controller.Entities.BaseItem)defaultConstructor!.Invoke(null);
  27. var exception = Record.Exception(() => instance.GetBaseItemKind());
  28. Assert.Null(exception);
  29. }
  30. private class GetBaseItemDescendants : IEnumerable<object?[]>
  31. {
  32. private static bool IsProjectAssemblyName(string? name)
  33. {
  34. if (name == null)
  35. {
  36. return false;
  37. }
  38. return name.StartsWith("Jellyfin", StringComparison.OrdinalIgnoreCase)
  39. || name.StartsWith("Emby", StringComparison.OrdinalIgnoreCase)
  40. || name.StartsWith("MediaBrowser", StringComparison.OrdinalIgnoreCase);
  41. }
  42. public IEnumerator<object?[]> GetEnumerator()
  43. {
  44. var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
  45. foreach (var assembly in loadedAssemblies)
  46. {
  47. if (IsProjectAssemblyName(assembly.FullName))
  48. {
  49. var baseItemTypes = assembly.GetTypes()
  50. .Where(targetType => targetType.IsClass
  51. && !targetType.IsAbstract
  52. && targetType.IsSubclassOf(typeof(MediaBrowser.Controller.Entities.BaseItem)));
  53. foreach (var baseItemType in baseItemTypes)
  54. {
  55. yield return new object?[] { baseItemType };
  56. }
  57. }
  58. }
  59. var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  60. if (path == null)
  61. {
  62. throw new NullException("Assembly location is null");
  63. }
  64. foreach (string dll in Directory.GetFiles(path, "*.dll"))
  65. {
  66. var assembly = Assembly.LoadFile(dll);
  67. if (IsProjectAssemblyName(assembly.FullName))
  68. {
  69. var baseItemTypes = assembly.GetTypes()
  70. .Where(targetType => targetType.IsClass
  71. && !targetType.IsAbstract
  72. && targetType.IsSubclassOf(typeof(MediaBrowser.Controller.Entities.BaseItem)));
  73. foreach (var baseItemType in baseItemTypes)
  74. {
  75. yield return new object?[] { baseItemType };
  76. }
  77. }
  78. }
  79. }
  80. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  81. }
  82. }
  83. }