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 { #region Properties /// /// Gets or sets a value indicating whether tests are "WithApplication". /// /// /// Default is false. /// This is for tests that inherited from TestWithApplicationBase. /// Implies AutoMapper = true (, ResetPluginManager = false). /// public bool WithApplication { get => _withApplication.ValueOrDefault(false); set => _withApplication.Set(value); } private readonly Settable _withApplication = new Settable(); /// /// Gets or sets a value indicating whether to compose and initialize AutoMapper. /// /// Default is false unless WithApplication is true, in which case default is true. public bool AutoMapper { get => _autoMapper.ValueOrDefault(WithApplication); set => _autoMapper.Set(value); } private readonly Settable _autoMapper = new Settable(); /// /// Gets or sets a value indicating ... FIXME to be completed /// public bool FacadeServiceRepositoryEvents { get => _facadeServiceRepositoryEvents.ValueOrDefault(false); set => _facadeServiceRepositoryEvents.Set(value); } private readonly Settable _facadeServiceRepositoryEvents = new Settable(); /// /// Gets or sets a value indicating the required logging support. /// /// Default is to mock logging. public UmbracoTestOptions.Logger Logger { get => _logger.ValueOrDefault(UmbracoTestOptions.Logger.Mock); set => _logger.Set(value); } private readonly Settable _logger = new Settable(); /// /// Gets or sets a value indicating the required database support. /// /// Default is no database support. public UmbracoTestOptions.Database Database { get => _database.ValueOrDefault(UmbracoTestOptions.Database.None); set => _database.Set(value); } private readonly Settable _database = new Settable(); /// /// Gets or sets a value indicating the required plugin manager support. /// /// Default is to use the global tests plugin manager. 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) { var attr = ((UmbracoTestAttribute[]) method.GetCustomAttributes(typeof (UmbracoTestAttribute), true)).FirstOrDefault(); var type = method.DeclaringType; return Get(type, attr); } public static UmbracoTestAttribute Get(Type type) { return Get(type, null); } 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(); } 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 } }