mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-27 22:56:45 -04:00
@@ -6,13 +6,13 @@ using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity;
|
||||
using Moq;
|
||||
using Moq.Language.Flow;
|
||||
using NzbDrone.Common.Composition;
|
||||
using NzbDrone.Common.Disk;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Test.Common.AutoMoq.Unity;
|
||||
using Unity;
|
||||
using Unity.Resolution;
|
||||
|
||||
[assembly: InternalsVisibleTo("AutoMoq.Tests")]
|
||||
@@ -36,7 +36,6 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||
{
|
||||
DefaultBehavior = defaultBehavior;
|
||||
SetupAutoMoqer(new UnityContainer());
|
||||
|
||||
}
|
||||
|
||||
public AutoMoqer(IUnityContainer container)
|
||||
@@ -62,12 +61,14 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||
return result;
|
||||
}
|
||||
|
||||
public virtual Mock<T> GetMock<T>() where T : class
|
||||
public virtual Mock<T> GetMock<T>()
|
||||
where T : class
|
||||
{
|
||||
return GetMock<T>(DefaultBehavior);
|
||||
}
|
||||
|
||||
public virtual Mock<T> GetMock<T>(MockBehavior behavior) where T : class
|
||||
public virtual Mock<T> GetMock<T>(MockBehavior behavior)
|
||||
where T : class
|
||||
{
|
||||
ResolveType = null;
|
||||
var type = GetTheMockType<T>();
|
||||
@@ -89,9 +90,14 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||
public virtual void SetMock(Type type, Mock mock)
|
||||
{
|
||||
if (_registeredMocks.ContainsKey(type) == false)
|
||||
{
|
||||
_registeredMocks.Add(type, mock);
|
||||
}
|
||||
|
||||
if (mock != null)
|
||||
{
|
||||
_container.RegisterInstance(type, mock.Object);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetConstant<T>(T instance)
|
||||
@@ -100,32 +106,38 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||
SetMock(instance.GetType(), null);
|
||||
}
|
||||
|
||||
public ISetup<T> Setup<T>(Expression<Action<T>> expression) where T : class
|
||||
public ISetup<T> Setup<T>(Expression<Action<T>> expression)
|
||||
where T : class
|
||||
{
|
||||
return GetMock<T>().Setup(expression);
|
||||
}
|
||||
|
||||
public ISetup<T, TResult> Setup<T, TResult>(Expression<Func<T, TResult>> expression) where T : class
|
||||
public ISetup<T, TResult> Setup<T, TResult>(Expression<Func<T, TResult>> expression)
|
||||
where T : class
|
||||
{
|
||||
return GetMock<T>().Setup(expression);
|
||||
}
|
||||
|
||||
public void Verify<T>(Expression<Action<T>> expression) where T : class
|
||||
public void Verify<T>(Expression<Action<T>> expression)
|
||||
where T : class
|
||||
{
|
||||
GetMock<T>().Verify(expression);
|
||||
}
|
||||
|
||||
public void Verify<T>(Expression<Action<T>> expression, string failMessage) where T : class
|
||||
public void Verify<T>(Expression<Action<T>> expression, string failMessage)
|
||||
where T : class
|
||||
{
|
||||
GetMock<T>().Verify(expression, failMessage);
|
||||
}
|
||||
|
||||
public void Verify<T>(Expression<Action<T>> expression, Times times) where T : class
|
||||
public void Verify<T>(Expression<Action<T>> expression, Times times)
|
||||
where T : class
|
||||
{
|
||||
GetMock<T>().Verify(expression, times);
|
||||
}
|
||||
|
||||
public void Verify<T>(Expression<Action<T>> expression, Times times, string failMessage) where T : class
|
||||
public void Verify<T>(Expression<Action<T>> expression, Times times, string failMessage)
|
||||
where T : class
|
||||
{
|
||||
GetMock<T>().Verify(expression, times, failMessage);
|
||||
}
|
||||
@@ -136,19 +148,19 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||
{
|
||||
var mock = registeredMock.Value as Mock;
|
||||
if (mock != null)
|
||||
{
|
||||
mock.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region private methods
|
||||
|
||||
private void SetupAutoMoqer(IUnityContainer container)
|
||||
{
|
||||
_container = container;
|
||||
container.RegisterInstance(this);
|
||||
|
||||
_registeredMocks = new Dictionary<Type, object>();
|
||||
|
||||
|
||||
RegisterPlatformLibrary(container);
|
||||
AddTheAutoMockingContainerExtensionToTheContainer(container);
|
||||
|
||||
@@ -163,12 +175,14 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||
return;
|
||||
}
|
||||
|
||||
private Mock<T> TheRegisteredMockForThisType<T>(Type type) where T : class
|
||||
private Mock<T> TheRegisteredMockForThisType<T>(Type type)
|
||||
where T : class
|
||||
{
|
||||
return (Mock<T>)_registeredMocks.First(x => x.Key == type).Value;
|
||||
}
|
||||
|
||||
private void CreateANewMockAndRegisterIt<T>(Type type, MockBehavior behavior) where T : class
|
||||
private void CreateANewMockAndRegisterIt<T>(Type type, MockBehavior behavior)
|
||||
where T : class
|
||||
{
|
||||
var mock = new Mock<T>(behavior);
|
||||
_container.RegisterInstance(mock.Object);
|
||||
@@ -180,7 +194,8 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||
return _registeredMocks.ContainsKey(type) == false;
|
||||
}
|
||||
|
||||
private static Type GetTheMockType<T>() where T : class
|
||||
private static Type GetTheMockType<T>()
|
||||
where T : class
|
||||
{
|
||||
return typeof(T);
|
||||
}
|
||||
@@ -203,14 +218,12 @@ namespace NzbDrone.Test.Common.AutoMoq
|
||||
// This seems to be required now so that Unity can resolve the extra arguments to the
|
||||
// Mono DiskProvider. I don't understand why we need this now but didn't before.
|
||||
// It's auto registering everything in the assembly with Ixxx -> xxx.
|
||||
types.Except(new [] { diskProvider }).Where(t => t.GetInterfaces().Any(i => i.Name == "I" + t.Name)).ToList()
|
||||
types.Except(new[] { diskProvider }).Where(t => t.GetInterfaces().Any(i => i.Name == "I" + t.Name)).ToList()
|
||||
.ForEach(t => container.RegisterType(t.GetInterface("I" + t.Name, false), t));
|
||||
|
||||
// This tells the mocker to resolve IFileSystem using an actual filesystem (and not a mock)
|
||||
// if not specified, giving the old behaviour before we switched to System.IO.Abstractions.
|
||||
SetConstant<IFileSystem>(new FileSystem());
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Unity;
|
||||
using Moq;
|
||||
using Unity.Strategies;
|
||||
using Unity;
|
||||
using Unity.Builder;
|
||||
using Unity.Strategies;
|
||||
|
||||
namespace NzbDrone.Test.Common.AutoMoq.Unity
|
||||
{
|
||||
@@ -36,8 +36,6 @@ namespace NzbDrone.Test.Common.AutoMoq.Unity
|
||||
}
|
||||
}
|
||||
|
||||
#region private methods
|
||||
|
||||
private bool AMockObjectShouldBeCreatedForThisType(Type type)
|
||||
{
|
||||
var mocker = _container.Resolve<AutoMoqer>();
|
||||
@@ -78,7 +76,5 @@ namespace NzbDrone.Test.Common.AutoMoq.Unity
|
||||
{
|
||||
return new[] { typeof(object[]) };
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace NzbDrone.Test.Common.AutoMoq.Unity
|
||||
{
|
||||
public class AutoMockingContainerExtension : UnityContainerExtension
|
||||
{
|
||||
private readonly IList<Type> registeredTypes = new List<Type>();
|
||||
private readonly IList<Type> _registeredTypes = new List<Type>();
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
@@ -15,25 +15,21 @@ namespace NzbDrone.Test.Common.AutoMoq.Unity
|
||||
SetBuildingStrategyForBuildingUnregisteredTypes();
|
||||
}
|
||||
|
||||
#region private methods
|
||||
|
||||
private void SetEventsOnContainerToTrackAllRegisteredTypes()
|
||||
{
|
||||
Context.Registering += ((sender, e) => RegisterType(e.TypeFrom));
|
||||
Context.RegisteringInstance += ((sender, e) => RegisterType(e.RegisteredType));
|
||||
Context.Registering += (sender, e) => RegisterType(e.TypeFrom);
|
||||
Context.RegisteringInstance += (sender, e) => RegisterType(e.RegisteredType);
|
||||
}
|
||||
|
||||
private void RegisterType(Type typeToRegister)
|
||||
{
|
||||
registeredTypes.Add(typeToRegister);
|
||||
_registeredTypes.Add(typeToRegister);
|
||||
}
|
||||
|
||||
private void SetBuildingStrategyForBuildingUnregisteredTypes()
|
||||
{
|
||||
var strategy = new AutoMockingBuilderStrategy(registeredTypes, Container);
|
||||
var strategy = new AutoMockingBuilderStrategy(_registeredTypes, Container);
|
||||
Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user