Radarr/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs

207 lines
5.8 KiB
C#
Raw Normal View History

2011-10-07 06:36:04 +00:00
using System;
using System.Collections.Generic;
2011-11-26 02:06:40 +00:00
using System.Diagnostics;
using System.IO;
2011-10-07 06:36:04 +00:00
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
2011-10-07 06:36:04 +00:00
using System.Runtime.CompilerServices;
using Moq;
using Moq.Language.Flow;
using NzbDrone.Common.Composition;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Test.Common.AutoMoq.Unity;
2019-12-22 22:08:53 +00:00
using Unity;
2011-10-07 06:36:04 +00:00
[assembly: InternalsVisibleTo("AutoMoq.Tests")]
namespace NzbDrone.Test.Common.AutoMoq
2011-10-07 06:36:04 +00:00
{
2011-11-26 02:06:40 +00:00
[DebuggerStepThrough]
2011-10-07 06:36:04 +00:00
public class AutoMoqer
{
2013-04-07 22:40:13 +00:00
public readonly MockBehavior DefaultBehavior = MockBehavior.Default;
public Type ResolveType;
private IUnityContainer _container;
private IDictionary<Type, object> _registeredMocks;
2011-10-07 06:36:04 +00:00
public AutoMoqer()
{
SetupAutoMoqer(new UnityContainer());
}
public AutoMoqer(MockBehavior defaultBehavior)
{
DefaultBehavior = defaultBehavior;
SetupAutoMoqer(new UnityContainer());
}
2013-04-07 22:40:13 +00:00
public AutoMoqer(IUnityContainer container)
2011-10-07 06:36:04 +00:00
{
SetupAutoMoqer(container);
}
public virtual T Resolve<T>()
{
ResolveType = typeof(T);
var result = _container.Resolve<T>();
2011-10-07 06:36:04 +00:00
SetConstant(result);
ResolveType = null;
return result;
}
2019-12-22 22:08:53 +00:00
public virtual Mock<T> GetMock<T>()
where T : class
2011-10-07 06:36:04 +00:00
{
return GetMock<T>(DefaultBehavior);
}
2019-12-22 22:08:53 +00:00
public virtual Mock<T> GetMock<T>(MockBehavior behavior)
where T : class
2011-10-07 06:36:04 +00:00
{
ResolveType = null;
var type = GetTheMockType<T>();
if (GetMockHasNotBeenCalledForThisType(type))
{
CreateANewMockAndRegisterIt<T>(type, behavior);
}
var mock = TheRegisteredMockForThisType<T>(type);
if (behavior != MockBehavior.Default && mock.Behavior == MockBehavior.Default)
{
throw new InvalidOperationException("Unable to change be behaviour of a an existing mock.");
}
return mock;
}
2013-04-07 22:40:13 +00:00
public virtual void SetMock(Type type, Mock mock)
2011-10-07 06:36:04 +00:00
{
if (_registeredMocks.ContainsKey(type) == false)
2019-12-22 22:08:53 +00:00
{
_registeredMocks.Add(type, mock);
2019-12-22 22:08:53 +00:00
}
if (mock != null)
2019-12-22 22:08:53 +00:00
{
_container.RegisterInstance(type, mock.Object);
2019-12-22 22:08:53 +00:00
}
2011-10-07 06:36:04 +00:00
}
public virtual void SetConstant<T>(T instance)
{
_container.RegisterInstance(instance);
2011-10-07 06:36:04 +00:00
SetMock(instance.GetType(), null);
}
2019-12-22 22:08:53 +00:00
public ISetup<T> Setup<T>(Expression<Action<T>> expression)
where T : class
2011-10-07 06:36:04 +00:00
{
return GetMock<T>().Setup(expression);
}
2019-12-22 22:08:53 +00:00
public ISetup<T, TResult> Setup<T, TResult>(Expression<Func<T, TResult>> expression)
where T : class
2011-10-07 06:36:04 +00:00
{
return GetMock<T>().Setup(expression);
}
2019-12-22 22:08:53 +00:00
public void Verify<T>(Expression<Action<T>> expression)
where T : class
2011-10-07 06:36:04 +00:00
{
GetMock<T>().Verify(expression);
}
2019-12-22 22:08:53 +00:00
public void Verify<T>(Expression<Action<T>> expression, string failMessage)
where T : class
2011-10-07 06:36:04 +00:00
{
GetMock<T>().Verify(expression, failMessage);
}
2019-12-22 22:08:53 +00:00
public void Verify<T>(Expression<Action<T>> expression, Times times)
where T : class
2011-10-07 06:36:04 +00:00
{
GetMock<T>().Verify(expression, times);
}
2019-12-22 22:08:53 +00:00
public void Verify<T>(Expression<Action<T>> expression, Times times, string failMessage)
where T : class
2011-10-07 06:36:04 +00:00
{
GetMock<T>().Verify(expression, times, failMessage);
}
public void VerifyAllMocks()
{
foreach (var registeredMock in _registeredMocks)
2011-10-07 06:36:04 +00:00
{
var mock = registeredMock.Value as Mock;
if (mock != null)
2019-12-22 22:08:53 +00:00
{
2011-10-07 06:36:04 +00:00
mock.VerifyAll();
2019-12-22 22:08:53 +00:00
}
2011-10-07 06:36:04 +00:00
}
}
private void SetupAutoMoqer(IUnityContainer container)
{
_container = container;
2011-10-07 06:36:04 +00:00
container.RegisterInstance(this);
RegisterPlatformLibrary(container);
_registeredMocks = new Dictionary<Type, object>();
2011-10-07 06:36:04 +00:00
AddTheAutoMockingContainerExtensionToTheContainer(container);
2019-12-22 22:08:53 +00:00
ContainerBuilderBase.RegisterNativeResolver(new[] { "System.Data.SQLite", "Radarr.Core" });
2011-10-07 06:36:04 +00:00
}
private static void AddTheAutoMockingContainerExtensionToTheContainer(IUnityContainer container)
{
container.AddNewExtension<AutoMockingContainerExtension>();
return;
}
2019-12-22 22:08:53 +00:00
private Mock<T> TheRegisteredMockForThisType<T>(Type type)
where T : class
2011-10-07 06:36:04 +00:00
{
2018-04-21 04:59:31 +00:00
return (Mock<T>)_registeredMocks.First(x => x.Key == type).Value;
2011-10-07 06:36:04 +00:00
}
2019-12-22 22:08:53 +00:00
private void CreateANewMockAndRegisterIt<T>(Type type, MockBehavior behavior)
where T : class
2011-10-07 06:36:04 +00:00
{
var mock = new Mock<T>(behavior);
_container.RegisterInstance(mock.Object);
2011-10-07 06:36:04 +00:00
SetMock(type, mock);
}
private bool GetMockHasNotBeenCalledForThisType(Type type)
{
return _registeredMocks.ContainsKey(type) == false;
2011-10-07 06:36:04 +00:00
}
2019-12-22 22:08:53 +00:00
private static Type GetTheMockType<T>()
where T : class
2011-10-07 06:36:04 +00:00
{
return typeof(T);
}
private void RegisterPlatformLibrary(IUnityContainer container)
{
2018-11-23 07:03:32 +00:00
var assemblyName = "Radarr.Windows";
2014-12-07 20:54:07 +00:00
if (OsInfo.IsNotWindows)
{
2018-11-23 07:03:32 +00:00
assemblyName = "Radarr.Mono";
}
if (!File.Exists(assemblyName + ".dll"))
{
return;
}
Assembly.Load(assemblyName);
}
2011-10-07 06:36:04 +00:00
}
2018-04-21 04:59:31 +00:00
}