From fa866bcdfbfae475d20df24c878cb52d0b1badfc Mon Sep 17 00:00:00 2001 From: Chris Winland Date: Tue, 28 May 2024 12:52:01 -0400 Subject: [PATCH] Conditionally include versioning of System.IO.Abstractions / TestableIO.System.IO.Abstractions based on version. If the version is 6, add default type mapping. --- FastMoq.Core/FastMoq.Core.csproj | 5 ++- FastMoq.Core/Mocker.cs | 38 +++++++++++++++----- FastMoq.Core/MockerTestBase_Constructors.cs | 9 ++++- FastMoq.Tests/MocksTests.cs | 39 +++++++++++++++++---- FastMoq.Web/Blazor/MockerBlazorTestBase.cs | 5 ++- 5 files changed, 76 insertions(+), 20 deletions(-) diff --git a/FastMoq.Core/FastMoq.Core.csproj b/FastMoq.Core/FastMoq.Core.csproj index 86ffafa..1e25237 100644 --- a/FastMoq.Core/FastMoq.Core.csproj +++ b/FastMoq.Core/FastMoq.Core.csproj @@ -52,7 +52,10 @@ - + + + + all runtime; build; native; contentfiles; buildtransitive diff --git a/FastMoq.Core/Mocker.cs b/FastMoq.Core/Mocker.cs index 237086a..16b0f8c 100644 --- a/FastMoq.Core/Mocker.cs +++ b/FastMoq.Core/Mocker.cs @@ -183,6 +183,28 @@ public T AddInjections(T obj, Type? referenceType = null) where T : class? return obj; } + /// + /// Adds the file io abstraction mapping. + /// + public void AddFileSystemAbstractionMapping() + { + this + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + .AddType() + ; + } + /// /// Creates a with the given with the option of overwriting an existing /// @@ -252,7 +274,7 @@ public MockModel AddMock(Mock mock, bool overwrite, bool nonPublic = fa /// Replace type if already exists. Default: false. /// arguments needed in model. /// - public void AddType(Type tInterface, Type tClass, Func? createFunc = null, bool replace = false, params object?[]? args) + public Mocker AddType(Type tInterface, Type tClass, Func? createFunc = null, bool replace = false, params object?[]? args) { ArgumentNullException.ThrowIfNull(tClass); ArgumentNullException.ThrowIfNull(tInterface); @@ -267,12 +289,14 @@ public void AddType(Type tInterface, Type tClass, Func? createFu throw new ArgumentException($"{tClass.Name} is not assignable to {tInterface.Name}."); } - if (typeMap.ContainsKey(tInterface) && replace) + if (replace && typeMap.ContainsKey(tInterface)) { typeMap.Remove(tInterface); } - typeMap.Add(tInterface, new InstanceModel(tInterface, tClass, createFunc, args?.ToList() ?? new())); + typeMap.Add(tInterface, new InstanceModel(tInterface, tClass, createFunc, args?.ToList() ?? [])); + + return this; } /// @@ -282,7 +306,7 @@ public void AddType(Type tInterface, Type tClass, Func? createFu /// An optional create function used to create the class. /// Replace type if already exists. Default: false. /// arguments needed in model. - public void AddType(Func? createFunc = null, bool replace = false, params object?[]? args) where T : class => + public Mocker AddType(Func? createFunc = null, bool replace = false, params object?[]? args) where T : class => AddType(createFunc, replace, args); /// @@ -295,7 +319,7 @@ public void AddType(Func? createFunc = null, bool replace = false, /// arguments needed in model. /// $"{typeof(TClass).Name} cannot be an interface." /// $"{typeof(TClass).Name} is not assignable to {typeof(TInterface).Name}." - public void AddType(Func? createFunc = null, bool replace = false, params object?[]? args) + public Mocker AddType(Func? createFunc = null, bool replace = false, params object?[]? args) where TInterface : class where TClass : class => AddType(typeof(TInterface), typeof(TClass), createFunc, replace, args); /// @@ -1339,9 +1363,7 @@ internal ConstructorModel FindConstructor(bool bestGuess, Type type, bool nonPub // if it is not best guess, then we can't know which constructor. if (!bestGuess && constructors.Count(x => x.ParameterList.Length > 0) > 1) { - throw new AmbiguousImplementationException( - "Multiple parameterized constructors exist. Cannot decide which to use." - ); + throw this.GetAmbiguousConstructorImplementationException(type); } // Best Guess // diff --git a/FastMoq.Core/MockerTestBase_Constructors.cs b/FastMoq.Core/MockerTestBase_Constructors.cs index 07c122d..9f96c85 100644 --- a/FastMoq.Core/MockerTestBase_Constructors.cs +++ b/FastMoq.Core/MockerTestBase_Constructors.cs @@ -1,4 +1,7 @@ -namespace FastMoq +using System.IO.Abstractions; +using System.IO.Abstractions.TestingHelpers; + +namespace FastMoq { /// public partial class MockerTestBase where TComponent : class @@ -62,6 +65,10 @@ protected MockerTestBase(Action setupMocksAction, SetupMocksAction = setupMocksAction; CreateComponentAction = createComponentAction ?? DefaultCreateAction; CreatedComponentAction = createdComponentAction; + +#if NET6_0 + Mocks.AddFileSystemAbstractionMapping(); +#endif Component = GetComponent(); } diff --git a/FastMoq.Tests/MocksTests.cs b/FastMoq.Tests/MocksTests.cs index 91422b5..a6d72a9 100644 --- a/FastMoq.Tests/MocksTests.cs +++ b/FastMoq.Tests/MocksTests.cs @@ -25,7 +25,10 @@ namespace FastMoq.Tests { public class MocksTests : MockerTestBase { - public MocksTests() : base(SetupAction, CreateAction, CreatedAction) { } + public MocksTests() : base(SetupAction, CreateAction, CreatedAction) + { + Component.AddFileSystemAbstractionMapping(); + } [Fact] public void AddInjections() @@ -366,10 +369,11 @@ public void CreateMockWithInjectParameters() public void AddTypeT_ShouldBe_AddTypeTT() { Mocks.AddType(_ => Mocks.CreateInstance()); - var t = Mocks.typeMap.First().Value.CreateFunc.Invoke(null); + var t = GetTypeMapOf().Value.CreateFunc.Invoke(null); Mocks.typeMap.Clear(); + Mocks.AddFileSystemAbstractionMapping(); Mocks.AddType(_ => Mocks.CreateInstance()); - var t2 = Mocks.typeMap.First().Value.CreateFunc.Invoke(null); + var t2 = GetTypeMapOf().Value.CreateFunc.Invoke(null); t.Should().BeEquivalentTo(t2); } @@ -377,17 +381,20 @@ public void AddTypeT_ShouldBe_AddTypeTT() public void AddTypeT_ShouldBe_AddType() { Mocks.AddType(_ => Mocks.CreateInstance()); - var t = Mocks.typeMap.First().Value.CreateFunc.Invoke(null); + var t = GetTypeMapOf().Value.CreateFunc.Invoke(null); var o = Mocks.CreateInstance(); o.Should().BeEquivalentTo(t); Mocks.typeMap.Clear(); + Mocks.AddFileSystemAbstractionMapping(); Mocks.AddType(typeof(TestClassOne), typeof(TestClassOne), _ => Mocks.CreateInstance()); - var t2 = Mocks.typeMap.First().Value.CreateFunc.Invoke(null); + var t2 = GetTypeMapOf().Value.CreateFunc.Invoke(null); t.Should().BeEquivalentTo(t2); o = Mocks.CreateInstance(); o.Should().BeEquivalentTo(t2); } + private KeyValuePair GetTypeMapOf() => Mocks.typeMap.First(x => x.Value.Type.FullName == typeof(T).FullName); + [Fact] public void FileSystem_ShouldBeValid() { @@ -746,8 +753,26 @@ public void IsValidConstructor() } [Fact] - public void Mocker_AddMapClass() => new Action(() => Component.AddType()) - .Should().NotThrow(); + public void Mocker_AddMapClass_NotADuplicate() + { + Component.typeMap.Clear(); + new Action(() => Component.AddType()) + .Should().NotThrow(); + } + + [Fact] + public void Mocker_AddMapClass_Duplicate() + { + new Action(() => Component.AddType()) + .Should().Throw(); + } + + [Fact] + public void Mocker_AddMapClass_OverwriteDuplicate() + { + new Action(() => Component.AddType(replace: true)) + .Should().NotThrow(); + } [Fact] public void Mocker_AddMapClassIncompatibleInterface_ShouldThrow() => new Action(() => Component.AddType()) diff --git a/FastMoq.Web/Blazor/MockerBlazorTestBase.cs b/FastMoq.Web/Blazor/MockerBlazorTestBase.cs index eb268cc..46400c6 100644 --- a/FastMoq.Web/Blazor/MockerBlazorTestBase.cs +++ b/FastMoq.Web/Blazor/MockerBlazorTestBase.cs @@ -15,7 +15,6 @@ using System.Collections; using System.Diagnostics.CodeAnalysis; using System.Reflection; -using System.Runtime; using System.Security.Claims; namespace FastMoq.Web.Blazor @@ -532,7 +531,7 @@ public IRenderedComponent GetComponent(Func @@ -542,7 +541,7 @@ public IRenderedComponent GetComponent(Func