diff --git a/MockMe.sln b/MockMe.sln index 368b0ae..d7a646d 100644 --- a/MockMe.sln +++ b/MockMe.sln @@ -36,7 +36,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "wiki", "wiki", "{4723EF4B-98A5-4F3D-BEB9-0632A3939D4D}" ProjectSection(SolutionItems) = preProject wiki\AvoidingCommonPitfalls.md = wiki\AvoidingCommonPitfalls.md - HowDoesItWork.md = HowDoesItWork.md + wiki\HowDoesItWork.md = wiki\HowDoesItWork.md wiki\QuickStart.md = wiki\QuickStart.md EndProjectSection EndProject @@ -48,6 +48,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MockMe.Tests.Overloads.Seal EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MockMe.Tests.Overloads.Interface", "tests\MockMe.Tests.Overloads.Interface\MockMe.Tests.Overloads.Interface.csproj", "{E08ADBB1-7CB8-4BFF-9BAC-485DC7822719}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MockMe.Tests.Overloads.Virtual", "tests\MockMe.Tests.Overloads.Virtual\MockMe.Tests.Overloads.Virtual.csproj", "{2F9424F6-0BB5-4BD8-92A7-3625A666111F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -86,6 +88,10 @@ Global {E08ADBB1-7CB8-4BFF-9BAC-485DC7822719}.Debug|Any CPU.Build.0 = Debug|Any CPU {E08ADBB1-7CB8-4BFF-9BAC-485DC7822719}.Release|Any CPU.ActiveCfg = Release|Any CPU {E08ADBB1-7CB8-4BFF-9BAC-485DC7822719}.Release|Any CPU.Build.0 = Release|Any CPU + {2F9424F6-0BB5-4BD8-92A7-3625A666111F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2F9424F6-0BB5-4BD8-92A7-3625A666111F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2F9424F6-0BB5-4BD8-92A7-3625A666111F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2F9424F6-0BB5-4BD8-92A7-3625A666111F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -100,12 +106,14 @@ Global {705ED000-6380-4C9E-BF2E-71BA8C390E28} = {134D18E2-ECAC-4571-B7BE-0C10796D5555} {1CDE1684-1F3F-AF84-5126-85482E225FD8} = {134D18E2-ECAC-4571-B7BE-0C10796D5555} {E08ADBB1-7CB8-4BFF-9BAC-485DC7822719} = {134D18E2-ECAC-4571-B7BE-0C10796D5555} + {2F9424F6-0BB5-4BD8-92A7-3625A666111F} = {134D18E2-ECAC-4571-B7BE-0C10796D5555} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {CF07A0CA-1140-44DC-9E7E-F99E65D9EF97} EndGlobalSection GlobalSection(SharedMSBuildProjectFiles) = preSolution tests\MockMe.Tests.Overloads\MockMe.Tests.Overloads.projitems*{1cde1684-1f3f-af84-5126-85482e225fd8}*SharedItemsImports = 5 + tests\MockMe.Tests.Overloads\MockMe.Tests.Overloads.projitems*{2f9424f6-0bb5-4bd8-92a7-3625a666111f}*SharedItemsImports = 5 tests\MockMe.Tests.Overloads\MockMe.Tests.Overloads.projitems*{705ed000-6380-4c9e-bf2e-71ba8c390e28}*SharedItemsImports = 13 tests\MockMe.Tests.Overloads\MockMe.Tests.Overloads.projitems*{e08adbb1-7cb8-4bff-9bac-485dc7822719}*SharedItemsImports = 5 EndGlobalSection diff --git a/src/MockMe.Generator/MockGenerators/PatchMethodGenerators/GenericClassMethodPatchMethodGenerator.cs b/src/MockMe.Generator/MockGenerators/PatchMethodGenerators/GenericClassMethodPatchMethodGenerator.cs index 1c6a38a..4a09aad 100644 --- a/src/MockMe.Generator/MockGenerators/PatchMethodGenerators/GenericClassMethodPatchMethodGenerator.cs +++ b/src/MockMe.Generator/MockGenerators/PatchMethodGenerators/GenericClassMethodPatchMethodGenerator.cs @@ -73,7 +73,7 @@ string typeSymbolName ) .GetValue(mock); - return ({returnType}) + {(isVoidReturnType ? "" : $"return ({returnType})")} callTracker .GetType() .GetMethod( @@ -84,7 +84,7 @@ string typeSymbolName {string.Join(", ", this.MethodSymbol.TypeParameters.Select(p => p.Name.AddOnIfNotEmpty("typeof(", ")"))) .AddOnIfNotEmpty(".MakeGenericMethod(", ")")} .Invoke(callTracker, new object[] {{ {paramString} }}); }} - return default; + return{(isVoidReturnType ? "" : " default")}; }} " ); diff --git a/src/MockMe/Images/MockMeM.png b/src/MockMe/Images/MockMeM.png index 6ace99b..e684b91 100644 Binary files a/src/MockMe/Images/MockMeM.png and b/src/MockMe/Images/MockMeM.png differ diff --git a/tests/MockMe.Tests.ExampleClasses/ClassWithGenericMethods.cs b/tests/MockMe.Tests.ExampleClasses/ClassWithGenericMethods.cs new file mode 100644 index 0000000..c42fe78 --- /dev/null +++ b/tests/MockMe.Tests.ExampleClasses/ClassWithGenericMethods.cs @@ -0,0 +1,13 @@ +using System; + +namespace MockMe.Tests.ExampleClasses; + +public class ClassWithGenericMethods +{ + public T OneGenericType(T t) => throw new NotImplementedException(); + + public T1 TwoGenericTypes(T1 t, T2 t2) => throw new NotImplementedException(); + + public T1 ThreeGenericTypes(T1 t, T2 t2, T3 t3) => + throw new NotImplementedException(); +} diff --git a/tests/MockMe.Tests.Overloads.Virtual/AllOverloads.cs b/tests/MockMe.Tests.Overloads.Virtual/AllOverloads.cs new file mode 100644 index 0000000..433f0a1 --- /dev/null +++ b/tests/MockMe.Tests.Overloads.Virtual/AllOverloads.cs @@ -0,0 +1,611 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; + +#pragma warning disable CA1716 // Using reserved word in namespace +namespace MockMe.Tests.Overloads +#pragma warning restore CA1716 // Using reserved word in namespace +{ + [ExcludeFromCodeCoverage] + internal class AllOverloads + { + protected virtual int ProtectedProp { get; set; } + + protected virtual int ProtectedMethod() => throw new NotImplementedException(); + + internal virtual int InternalProp { get; set; } + + internal virtual int InternalMethod() => throw new NotImplementedException(); + + public virtual int Prop_GetSet { get; set; } + public virtual int Prop_GetInit { get; init; } + public virtual int Prop_GetOnly { get; } + public virtual int Prop_SetOnly + { + set => throw new NotImplementedException(); + } + + public string this[int index] + { + get => throw new NotImplementedException(); + set => throw new NotImplementedException(); + } + + public virtual int this[string index] + { + get => throw new NotImplementedException(); + set => throw new NotImplementedException(); + } + + public double this[double index] + { + set => throw new NotImplementedException(); + } + internal virtual int this[float index] + { + get => throw new NotImplementedException(); + set => throw new NotImplementedException(); + } + protected virtual int this[decimal index] + { + get => throw new NotImplementedException(); + set => throw new NotImplementedException(); + } + + public virtual void VoidReturn() => throw new NotImplementedException(); + + public virtual void VoidReturn(int p1) => throw new NotImplementedException(); + + public virtual void VoidReturn(int p1, int p2) => throw new NotImplementedException(); + + public virtual void VoidReturn(int p1, int p2, int p3) => + throw new NotImplementedException(); + + public virtual void VoidReturn(int p1, int p2, int p3, int p4) => + throw new NotImplementedException(); + + public virtual void VoidReturn(int p1, int p2, int p3, int p4, int p5) => + throw new NotImplementedException(); + + public virtual void VoidReturn(int p1, int p2, int p3, int p4, int p5, int p6) => + throw new NotImplementedException(); + + public virtual void VoidReturn(int p1, int p2, int p3, int p4, int p5, int p6, int p7) => + throw new NotImplementedException(); + + public virtual void VoidReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8 + ) => throw new NotImplementedException(); + + public virtual void VoidReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9 + ) => throw new NotImplementedException(); + + public virtual void VoidReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10 + ) => throw new NotImplementedException(); + + public virtual void VoidReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11 + ) => throw new NotImplementedException(); + + public virtual void VoidReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12 + ) => throw new NotImplementedException(); + + public virtual void VoidReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13 + ) => throw new NotImplementedException(); + + public virtual void VoidReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13, + int p14 + ) => throw new NotImplementedException(); + + public virtual void VoidReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13, + int p14, + int p15 + ) => throw new NotImplementedException(); + + public virtual int SyncReturn() => throw new NotImplementedException(); + + public virtual int SyncReturn(int p1) => throw new NotImplementedException(); + + public virtual int SyncReturn(int p1, int p2) => throw new NotImplementedException(); + + public virtual int SyncReturn(int p1, int p2, int p3) => + throw new NotImplementedException(); + + public virtual int SyncReturn(int p1, int p2, int p3, int p4) => + throw new NotImplementedException(); + + public virtual int SyncReturn(int p1, int p2, int p3, int p4, int p5) => + throw new NotImplementedException(); + + public virtual int SyncReturn(int p1, int p2, int p3, int p4, int p5, int p6) => + throw new NotImplementedException(); + + public virtual int SyncReturn(int p1, int p2, int p3, int p4, int p5, int p6, int p7) => + throw new NotImplementedException(); + + public virtual int SyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8 + ) => throw new NotImplementedException(); + + public virtual int SyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9 + ) => throw new NotImplementedException(); + + public virtual int SyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10 + ) => throw new NotImplementedException(); + + public virtual int SyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11 + ) => throw new NotImplementedException(); + + public virtual int SyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12 + ) => throw new NotImplementedException(); + + public virtual int SyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13 + ) => throw new NotImplementedException(); + + public virtual int SyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13, + int p14 + ) => throw new NotImplementedException(); + + public virtual int SyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13, + int p14, + int p15 + ) => throw new NotImplementedException(); + + public virtual Task AsyncReturn() => throw new NotImplementedException(); + + public virtual Task AsyncReturn(int p1) => throw new NotImplementedException(); + + public virtual Task AsyncReturn(int p1, int p2) => throw new NotImplementedException(); + + public virtual Task AsyncReturn(int p1, int p2, int p3) => + throw new NotImplementedException(); + + public virtual Task AsyncReturn(int p1, int p2, int p3, int p4) => + throw new NotImplementedException(); + + public virtual Task AsyncReturn(int p1, int p2, int p3, int p4, int p5) => + throw new NotImplementedException(); + + public virtual Task AsyncReturn(int p1, int p2, int p3, int p4, int p5, int p6) => + throw new NotImplementedException(); + + public virtual Task AsyncReturn(int p1, int p2, int p3, int p4, int p5, int p6, int p7) => + throw new NotImplementedException(); + + public virtual Task AsyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8 + ) => throw new NotImplementedException(); + + public virtual Task AsyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9 + ) => throw new NotImplementedException(); + + public virtual Task AsyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10 + ) => throw new NotImplementedException(); + + public virtual Task AsyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11 + ) => throw new NotImplementedException(); + + public virtual Task AsyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12 + ) => throw new NotImplementedException(); + + public virtual Task AsyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13 + ) => throw new NotImplementedException(); + + public virtual Task AsyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13, + int p14 + ) => throw new NotImplementedException(); + + public virtual Task AsyncReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13, + int p14, + int p15 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn() => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn(int p1) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn(int p1, int p2) => + throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn(int p1, int p2, int p3) => + throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn(int p1, int p2, int p3, int p4) => + throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn(int p1, int p2, int p3, int p4, int p5) => + throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn(int p1, int p2, int p3, int p4, int p5, int p6) => + throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13, + int p14 + ) => throw new NotImplementedException(); + + public virtual Task AsyncOfTReturn( + int p1, + int p2, + int p3, + int p4, + int p5, + int p6, + int p7, + int p8, + int p9, + int p10, + int p11, + int p12, + int p13, + int p14, + int p15 + ) => throw new NotImplementedException(); + } +} diff --git a/tests/MockMe.Tests.Overloads.Virtual/MockMe.Tests.Overloads.Virtual.csproj b/tests/MockMe.Tests.Overloads.Virtual/MockMe.Tests.Overloads.Virtual.csproj new file mode 100644 index 0000000..0e5239c --- /dev/null +++ b/tests/MockMe.Tests.Overloads.Virtual/MockMe.Tests.Overloads.Virtual.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + diff --git a/tests/MockMe.Tests.slnf b/tests/MockMe.Tests.slnf index 1c1f0f3..4997fbe 100644 --- a/tests/MockMe.Tests.slnf +++ b/tests/MockMe.Tests.slnf @@ -5,6 +5,7 @@ "tests\\MockMe.Tests.ExampleClasses\\MockMe.Tests.ExampleClasses.csproj", "tests\\MockMe.Tests.Overloads.Interface\\MockMe.Tests.Overloads.Interface.csproj", "tests\\MockMe.Tests.Overloads.Sealed\\MockMe.Tests.Overloads.Sealed.csproj", + "tests\\MockMe.Tests.Overloads.Virtual\\MockMe.Tests.Overloads.Virtual.csproj", "tests\\MockMe.Tests.Overloads\\MockMe.Tests.Overloads.shproj", "tests\\MockMe.Tests\\MockMe.Tests.csproj" ] diff --git a/tests/MockMe.Tests/ArgTests.cs b/tests/MockMe.Tests/ArgTests.cs index 3c01713..43cff1c 100644 --- a/tests/MockMe.Tests/ArgTests.cs +++ b/tests/MockMe.Tests/ArgTests.cs @@ -1,4 +1,3 @@ -using System; using MockMe.Asserters; using MockMe.Exceptions; using MockMe.Tests.ExampleClasses; diff --git a/tests/MockMe.Tests/AssertionTests.cs b/tests/MockMe.Tests/AssertionTests.cs index 341acf1..2d01f21 100644 --- a/tests/MockMe.Tests/AssertionTests.cs +++ b/tests/MockMe.Tests/AssertionTests.cs @@ -11,7 +11,7 @@ public class AssertionTests [Fact] public void TestWasCalled_ForMethodWithNoArgsAndNoReturnVal() { - var calculatorMock = Mock.Me(default(Calculator)); + var calculatorMock = Mock.Me(); Assert.ThrowsAny(() => calculatorMock.Assert.TurnOff().WasCalled()); diff --git a/tests/MockMe.Tests/GenericClassTests.cs b/tests/MockMe.Tests/GenericClassTests.cs index 10a3cba..ecd1e72 100644 --- a/tests/MockMe.Tests/GenericClassTests.cs +++ b/tests/MockMe.Tests/GenericClassTests.cs @@ -7,7 +7,7 @@ public class ConnorsCoolGenericType { public T GetRandomVal() => throw new NotImplementedException(); - public T MyCoolProp => throw new NotImplementedException(); + public T? MyCoolProp { get; set; } public string TakeAT(T input) => throw new NotImplementedException(); } @@ -107,5 +107,23 @@ public void MultipleReferenceTypeOverloads_ShouldReturnConfiguredValues() Assert.Equal(myObj, returnVal); Assert.Equal("returnVal", returnString); } + + [Fact] + public void PropertyOfGenericClassType_ShouldReturnConfiguredValues() + { + var mockString = Mock.Me>(); + + mockString.Setup.MyCoolProp.Get().Returns("returnVal"); + string? setVal = null; + mockString.Setup.MyCoolProp.Set(Arg.Any()).Callback(x => setVal = x); + + ConnorsCoolGenericType coolStringType = mockString; + + Assert.Equal("returnVal", coolStringType.MyCoolProp); + coolStringType.MyCoolProp = "set value"; + Assert.Equal("set value", setVal); + mockString.Assert.MyCoolProp.Get().WasCalled(); + mockString.Assert.MyCoolProp.Set("set value").WasCalled(); + } } } diff --git a/tests/MockMe.Tests/GenericMethodTests.cs b/tests/MockMe.Tests/GenericMethodTests.cs index 5cc0945..5e74a4f 100644 --- a/tests/MockMe.Tests/GenericMethodTests.cs +++ b/tests/MockMe.Tests/GenericMethodTests.cs @@ -1,5 +1,4 @@ //using MockMe.Tests.SampleClasses; -using System.Collections.Generic; using MockMe.Tests.ExampleClasses; using Xunit; @@ -17,26 +16,21 @@ public void GenericMethod_ShouldReturnConfiguredValue() ComplexCalculator calc = (ComplexCalculator)mock; var result = calc.ComputeHashForObjects(new int[] { 1, 2, 3, 4, 5 }); + Assert.Equal(99, result); } - //[Fact] - //public void GenericClass_ShouldReturnConfiguredValue() - //{ - // //var mock = Mock.Me>(); - - // var stringMock = Mock.Me>(); - - // //mock.Setup.Contains(5).Returns(true); - - // stringMock.Setup.Contains("hello").Returns(true); - - // //List list = mock; + [Fact] + public void GenericMethodWithMultipleGenericArgs_ShouldReturnConfiguredValue() + { + var mock = Mock.Me(); - // //list.set_Item(5) + mock.Setup.ThreeGenericTypes(Arg.Any(), Arg.Any(), Arg.Any()) + .Returns("asdf"); - // //var result = list.Contains(5); + ClassWithGenericMethods obj = mock; - // var x = stringMock.MockedObject.Contains("hello"); - //} + var result = obj.ThreeGenericTypes("Hello", 1, 9.9); + Assert.Equal("asdf", result); + } } } diff --git a/HowDoesItWork.md b/wiki/HowDoesItWork.md similarity index 100% rename from HowDoesItWork.md rename to wiki/HowDoesItWork.md