Files
Umbraco-CMS/tests/Umbraco.Tests.Common/Testing/TestOptionAttributeBase.cs
Nikolaj Geisle b4fbad14c7 V11: cleanup obsoletes (#12481)
* Remove IExternalLoginService.cs

* Remove UmbracoApplicationComponentsInstallingNotification.cs

* Remove UmbracoApplicationMainDomAcquiredNotification.cs

* Merge IContentTypeWithHistoryCleanup with IContentType.cs

* Remove obsolete ctors from notifications

* Remove obsolete methods

* Remove obsolete method from RequestHandlerSettings.cs

* Fix UmbracoBuilder.Repositories.cs

* RemoveRemove obsolete constants

* Remove obsolete method from IRuntimeMinifier

* Remove SetLastLogin from IMemberRepository

* Revert "RemoveRemove obsolete constants"

This reverts commit cddb8ad1cf3d02bd9949d52bed91b45c8d2d66a9.

* Remove obsoleted Constants-Conventions.cs

* Remove obsolete ctors

* Make ContentData properties immutable

* remove obsolete static property from TestOptionAttributeBase

* Merge IMacroWithAliasService into IMacroService

* Remove IUserComposer

* remove obsolete AddOEmbedProvider method

* remove obsolete static EmbedProvidersCollectionBuilder

* remove obsolete HasFlagAll<T> method

* Remove obsolete LocalizedTextService property from BaseHttpHeaderCheck

* Remove obsolete GetDefaultFIleContent method from ViewHelper

* Remove more obsolete ctors and methods

* Remove obsolete ctor from RelationType

* Remove more obsolete methods

* Remove IExternalLoginRepository

* merge IMacroWithAliasRepository with IMacroRepository

* Remove obsolete methods from ExternalLoginRepository

* Remove obsolete method from IUserRepository

* Remove obsolete SetLastLogin, as it was NoOp

* Remove wierd SetLastLogin method from UserService

* Remove GetLogLevel from ILogViewer

* Remove more obsolete methods and ctors

* Remove more obsoletes

* Use other method in BackOfficeServerVariables.cs since GetAllTypes is now removed

* Remove obsolete ctor

* Remove ConfigureIISServerOptions

* Remove more obsolete methods

* Merge ITwoFactorLoginService2 with ITwoFactorLoginService

* Re-introduce GetCustomGenericProperties in MemberTabsAndPropertiesMapper.cs

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
2022-06-07 11:16:30 +02:00

74 lines
2.5 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using NUnit.Framework.Internal;
using Umbraco.Cms.Core.Exceptions;
namespace Umbraco.Cms.Tests.Common.Testing
{
public abstract class TestOptionAttributeBase : Attribute
{
public static TOptions GetTestOptions<TOptions>(MethodInfo method)
where TOptions : TestOptionAttributeBase, new()
{
TOptions attr = ((TOptions[])method.GetCustomAttributes(typeof(TOptions), true)).FirstOrDefault();
Type type = method.DeclaringType;
return Get(type, attr);
}
public static TOptions GetFixtureOptions<TOptions>(Type type)
where TOptions : TestOptionAttributeBase, new() => Get<TOptions>(type, null);
public static TOptions GetTestOptions<TOptions>()
where TOptions : TestOptionAttributeBase, new()
{
TestContext.TestAdapter test = TestContext.CurrentContext.Test;
var methodName = test.MethodName;
var type = TestExecutionContext.CurrentContext.TestObject.GetType();
MethodInfo methodInfo = type.GetMethod(methodName); // what about overloads?
TOptions 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))
{
TOptions 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 PanicException("Could not merge test options");
}
return merged;
}
protected virtual TestOptionAttributeBase Merge(TestOptionAttributeBase other) => this;
}
}