Refactor test options attribute
This commit is contained in:
65
src/Umbraco.Tests/Testing/TestOptionAttributeBase.cs
Normal file
65
src/Umbraco.Tests/Testing/TestOptionAttributeBase.cs
Normal file
@@ -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<TOptions>(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<TOptions>(Type type)
|
||||
where TOptions : TestOptionAttributeBase, new()
|
||||
{
|
||||
return Get<TOptions>(type, null);
|
||||
}
|
||||
|
||||
public static TOptions GetTestOptions<TOptions>()
|
||||
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<TOptions>(methodInfo);
|
||||
return options;
|
||||
}
|
||||
|
||||
private static TOptions Get<TOptions>(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>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether tests are "WithApplication".
|
||||
/// </summary>
|
||||
@@ -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<UmbracoTestOptions.PluginManager> _pluginManager = new Settable<UmbracoTestOptions.PluginManager>();
|
||||
|
||||
#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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<UmbracoTestAttribute>();
|
||||
|
||||
Compose();
|
||||
Initialize();
|
||||
|
||||
@@ -262,6 +262,7 @@
|
||||
<Compile Include="Persistence\NPocoExpressionsTests.cs" />
|
||||
<Compile Include="Persistence\UnitOfWorkTests.cs" />
|
||||
<Compile Include="Persistence\Repositories\RedirectUrlRepositoryTests.cs" />
|
||||
<Compile Include="Testing\TestOptionAttributeBase.cs" />
|
||||
<Compile Include="Testing\UmbracoTestAttribute.cs" />
|
||||
<Compile Include="Testing\UmbracoTestBase.cs" />
|
||||
<Compile Include="TestHelpers\Entities\MockedPropertyTypes.cs" />
|
||||
|
||||
Reference in New Issue
Block a user