diff --git a/src/Umbraco.Tests/Testing/TestOptionAttributeBase.cs b/src/Umbraco.Tests/Testing/TestOptionAttributeBase.cs new file mode 100644 index 0000000000..db7dd25152 --- /dev/null +++ b/src/Umbraco.Tests/Testing/TestOptionAttributeBase.cs @@ -0,0 +1,65 @@ +using System; +using System.Linq; +using System.Reflection; +using NUnit.Framework; + +namespace Umbraco.Tests.Testing +{ + public abstract class TestOptionAttributeBase : Attribute + { + public static TOptions GetTestOptions(MethodInfo method) + where TOptions : TestOptionAttributeBase, new() + { + var attr = ((TOptions[]) method.GetCustomAttributes(typeof (TOptions), true)).FirstOrDefault(); + var type = method.DeclaringType; + return Get(type, attr); + } + + public static TOptions GetFixtureOptions(Type type) + where TOptions : TestOptionAttributeBase, new() + { + return Get(type, null); + } + + public static TOptions GetTestOptions() + where TOptions : TestOptionAttributeBase, new() + { + var test = TestContext.CurrentContext.Test; + var typeName = test.ClassName; + var methodName = test.MethodName; + var type = Type.GetType(typeName, true); + if (type == null) + throw new Exception("panic"); // makes no sense + var methodInfo = type.GetMethod(methodName); // what about overloads? + var options = GetTestOptions(methodInfo); + return options; + } + + private static TOptions Get(Type type, TOptions attr) + where TOptions : TestOptionAttributeBase, new() + { + while (type != null && type != typeof(object)) + { + var attr2 = ((TOptions[]) type.GetCustomAttributes(typeof (TOptions), true)).FirstOrDefault(); + if (attr2 != null) + attr = attr == null ? attr2 : attr2.Merge(attr); + type = type.BaseType; + } + return attr ?? new TOptions(); + } + + private TOptions Merge(TOptions other) + where TOptions : TestOptionAttributeBase + { + if (other == null) throw new ArgumentNullException(nameof(other)); + if (!(Merge((TestOptionAttributeBase) other) is TOptions merged)) + throw new Exception("panic"); + return merged; + } + + protected virtual TestOptionAttributeBase Merge(TestOptionAttributeBase other) + { + return this; + } + } +} diff --git a/src/Umbraco.Tests/Testing/UmbracoTestAttribute.cs b/src/Umbraco.Tests/Testing/UmbracoTestAttribute.cs index 58ce26ee17..ced4528cb6 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestAttribute.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestAttribute.cs @@ -1,15 +1,11 @@ using System; -using System.Linq; -using System.Reflection; using Umbraco.Core; namespace Umbraco.Tests.Testing { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, /*AllowMultiple = false,*/ Inherited = false)] - public class UmbracoTestAttribute : Attribute + public class UmbracoTestAttribute : TestOptionAttributeBase { - #region Properties - /// /// Gets or sets a value indicating whether tests are "WithApplication". /// @@ -55,44 +51,20 @@ namespace Umbraco.Tests.Testing public UmbracoTestOptions.PluginManager PluginManager { get => _pluginManager.ValueOrDefault(UmbracoTestOptions.PluginManager.Default); set => _pluginManager.Set(value); } private readonly Settable _pluginManager = new Settable(); - #endregion - - #region Get - - public static UmbracoTestAttribute Get(MethodInfo method) + protected override TestOptionAttributeBase Merge(TestOptionAttributeBase other) { - var attr = ((UmbracoTestAttribute[]) method.GetCustomAttributes(typeof (UmbracoTestAttribute), true)).FirstOrDefault(); - var type = method.DeclaringType; - return Get(type, attr); - } + if (!(other is UmbracoTestAttribute attr)) + throw new ArgumentException(nameof(other)); - public static UmbracoTestAttribute Get(Type type) - { - return Get(type, null); - } + base.Merge(other); - private static UmbracoTestAttribute Get(Type type, UmbracoTestAttribute attr) - { - while (type != null && type != typeof(object)) - { - var attr2 = ((UmbracoTestAttribute[]) type.GetCustomAttributes(typeof (UmbracoTestAttribute), true)).FirstOrDefault(); - if (attr2 != null) - attr = attr == null ? attr2 : attr2.Merge(attr); - type = type.BaseType; - } - return attr ?? new UmbracoTestAttribute(); - } + _autoMapper.Set(attr._autoMapper); + _facadeServiceRepositoryEvents.Set(attr._facadeServiceRepositoryEvents); + _logger.Set(attr._logger); + _database.Set(attr._database); + _pluginManager.Set(attr._pluginManager); - private UmbracoTestAttribute Merge(UmbracoTestAttribute other) - { - _autoMapper.Set(other._autoMapper); - _facadeServiceRepositoryEvents.Set(other._facadeServiceRepositoryEvents); - _logger.Set(other._logger); - _database.Set(other._database); - _pluginManager.Set(other._pluginManager); return this; } - - #endregion } } diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index ba0cbeaff3..eee2043879 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -113,10 +113,7 @@ namespace Umbraco.Tests.Testing TestObjects = new TestObjects(Container); // get/merge the attributes marking the method and/or the classes - var testName = TestContext.CurrentContext.Test.Name; - var pos = testName.IndexOf('('); - if (pos > 0) testName = testName.Substring(0, pos); - Options = UmbracoTestAttribute.Get(GetType().GetMethod(testName)); + Options = TestOptionAttributeBase.GetTestOptions(); Compose(); Initialize(); diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 842c3fd3c7..de1c00c907 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -262,6 +262,7 @@ +