2020-12-19 08:17:35 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-04-24 22:12:17 +02:00
|
|
|
using System.Collections.Generic;
|
2017-09-19 18:47:07 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using NUnit.Framework;
|
2021-10-18 21:25:21 +02:00
|
|
|
using NUnit.Framework.Internal;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Exceptions;
|
2017-09-19 18:47:07 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Common.Testing;
|
|
|
|
|
|
|
|
|
|
public abstract class TestOptionAttributeBase : Attribute
|
2017-09-19 18:47:07 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
public static TOptions GetTestOptions<TOptions>(MethodInfo method)
|
|
|
|
|
where TOptions : TestOptionAttributeBase, new()
|
2017-09-19 18:47:07 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var attr = ((TOptions[])method.GetCustomAttributes(typeof(TOptions), true)).FirstOrDefault();
|
|
|
|
|
var type = method.DeclaringType;
|
|
|
|
|
return Get(type, attr);
|
|
|
|
|
}
|
2017-09-19 18:47:07 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public static TOptions GetFixtureOptions<TOptions>(Type type)
|
|
|
|
|
where TOptions : TestOptionAttributeBase, new() => Get<TOptions>(type, null);
|
2017-09-19 18:47:07 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public static TOptions GetTestOptions<TOptions>()
|
|
|
|
|
where TOptions : TestOptionAttributeBase, new()
|
|
|
|
|
{
|
|
|
|
|
var test = TestContext.CurrentContext.Test;
|
|
|
|
|
var methodName = test.MethodName;
|
|
|
|
|
var type = TestExecutionContext.CurrentContext.TestObject.GetType();
|
|
|
|
|
var methodInfo = type.GetMethod(methodName); // what about overloads?
|
|
|
|
|
var options = GetTestOptions<TOptions>(methodInfo);
|
|
|
|
|
return options;
|
|
|
|
|
}
|
2017-09-19 18:47:07 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private static TOptions Get<TOptions>(Type type, TOptions attr)
|
|
|
|
|
where TOptions : TestOptionAttributeBase, new()
|
|
|
|
|
{
|
|
|
|
|
while (type != null && type != typeof(object))
|
2017-09-19 18:47:07 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var attr2 = ((TOptions[])type.GetCustomAttributes(typeof(TOptions), true)).FirstOrDefault();
|
|
|
|
|
if (attr2 != null)
|
2017-09-19 18:47:07 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
attr = attr == null ? attr2 : attr2.Merge(attr);
|
2017-09-19 18:47:07 +02:00
|
|
|
}
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
type = type.BaseType;
|
2017-09-19 18:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return attr ?? new TOptions();
|
|
|
|
|
}
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private TOptions Merge<TOptions>(TOptions other)
|
|
|
|
|
where TOptions : TestOptionAttributeBase
|
|
|
|
|
{
|
|
|
|
|
if (other == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(other));
|
|
|
|
|
}
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
if (!(Merge((TestOptionAttributeBase)other) is TOptions merged))
|
|
|
|
|
{
|
|
|
|
|
throw new PanicException("Could not merge test options");
|
2017-09-19 18:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return merged;
|
2017-09-19 18:47:07 +02:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
|
|
|
|
|
protected virtual TestOptionAttributeBase Merge(TestOptionAttributeBase other) => this;
|
2017-09-19 18:47:07 +02:00
|
|
|
}
|