From 3fddd9cb373bf3d31822683453c91820d9c9f8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20Sj=C3=B8rup?= Date: Sat, 2 Nov 2024 23:44:14 +0100 Subject: [PATCH] Cleanup unit tests and fixing minor issues (#26) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Cleanup tests * Minor fixes * Removed warnings and improved unit test --------- Co-authored-by: Lasse Sjørup --- src/MiniMock/AnalyzerReleases.Shipped.md | 2 +- src/MiniMock/AnalyzerReleases.Unshipped.md | 6 +- src/MiniMock/Builders/EventBuilder.cs | 12 ++-- src/MiniMock/Builders/Helpers.cs | 23 +++----- src/MiniMock/Builders/PropertyBuilder.cs | 5 -- src/MiniMock/DiagnosticsBuilder.cs | 10 ---- src/MiniMock/MiniMockGenerator.cs | 5 -- tests/MiniMock.Tests/Demo.cs | 3 +- .../TriggerEventFromConfiguration.cs | 8 +-- .../GeneralTests/AbstractClassesTest.cs | 56 ++++++++++++++---- .../IndexerTests/GenericIndexerTests.cs | 4 +- .../IndexerTests/StaticInterfaceTests.cs | 9 +-- .../MethodTests/GenericInterfaceTests.cs | 57 ++++++++++++------- .../MethodTests/InheritanceTests.cs | 31 ++-------- tests/MiniMock.Tests/Playground.cs | 3 - .../PropertyTests/InheritanceTests.cs | 37 +++--------- tests/MiniMock.UnitTests/DiagnosticsTests.cs | 2 +- tests/MiniMock.UnitTests/GenericsTests.cs | 2 +- tests/MiniMock.UnitTests/InheritanceTests.cs | 4 +- tests/MiniMock.UnitTests/PropertyTest.cs | 6 +- .../StaticInterfaceMembersTest.cs | 12 ++-- 21 files changed, 143 insertions(+), 154 deletions(-) diff --git a/src/MiniMock/AnalyzerReleases.Shipped.md b/src/MiniMock/AnalyzerReleases.Shipped.md index 3df7b4f..17611f5 100644 --- a/src/MiniMock/AnalyzerReleases.Shipped.md +++ b/src/MiniMock/AnalyzerReleases.Shipped.md @@ -6,7 +6,7 @@ Rule ID | Category | Severity | Notes --------|----------|----------|-------------------- MM0002 | Usage | Error | Ref properties not supported, [Documentation](https://github.com/oswaldsql/MiniMock/blob/main/Documentation/AnalyzerRules/MM0002.md) MM0003 | Usage | Error | Ref return type not supported, [Documentation](https://github.com/oswaldsql/MiniMock/blob/main/Documentation/AnalyzerRules/MM0003.md) -MM0004 | Usage | Error | Generic method not supported, [Documentation](https://github.com/oswaldsql/MiniMock/blob/main/Documentation/AnalyzerRules/MM0004.md) +MM0004 | Usage | Error | Generic method not supported, [Documentation](https://github.com/oswaldsql/MiniMock/blob/main/Documentation/AnalyzerRules/MM0004.md) MM0005 | Usage | Error | Static abstract members not supported, [Documentation](https://github.com/oswaldsql/MiniMock/blob/main/Documentation/AnalyzerRules/MM0005.md) MM0006 | Usage | Error | Can not create mock for a sealed class, [Documentation](https://github.com/oswaldsql/MiniMock/blob/main/Documentation/AnalyzerRules/MM0006.md) diff --git a/src/MiniMock/AnalyzerReleases.Unshipped.md b/src/MiniMock/AnalyzerReleases.Unshipped.md index c5d38d7..88d3fbb 100644 --- a/src/MiniMock/AnalyzerReleases.Unshipped.md +++ b/src/MiniMock/AnalyzerReleases.Unshipped.md @@ -1 +1,5 @@ -### New Rules +### Removed Rules + +Rule ID | Category | Severity | Notes +--------|----------|----------|-------------------- +MM0004 | Usage | Error | Generic method not supported, [Documentation](https://github.com/oswaldsql/MiniMock/blob/main/Documentation/AnalyzerRules/MM0004.md) diff --git a/src/MiniMock/Builders/EventBuilder.cs b/src/MiniMock/Builders/EventBuilder.cs index a8942a4..3ebf1b9 100644 --- a/src/MiniMock/Builders/EventBuilder.cs +++ b/src/MiniMock/Builders/EventBuilder.cs @@ -56,15 +56,15 @@ private static void BuildEvent(CodeBuilder builder, IEventSymbol symbol, List this.{{eventFunction}} += value; - remove => this.{{eventFunction}} -= value; + add => this._{{eventFunction}} += value; + remove => this._{{eventFunction}} -= value; } private void trigger_{{eventFunction}}({{parameterList}}) { - {{eventFunction}}?.Invoke({{nameList}}); + _{{eventFunction}}?.Invoke({{nameList}}); } """); @@ -86,8 +86,8 @@ private static void BuildEvent(CodeBuilder builder, IEventSymbol symbol, List this.{eventName}(args);", $"Returns an action that can be used for triggering {eventName}.", seeCref)); - helpers.Add(new(types + " eventArgs", - $"target.trigger_{eventFunction}(target, eventArgs);", + helpers.Add(new(types + " raise", + $"target.trigger_{eventFunction}(target, raise);", $"Trigger {eventName} directly.", seeCref)); } } diff --git a/src/MiniMock/Builders/Helpers.cs b/src/MiniMock/Builders/Helpers.cs index d27edc3..a596b9b 100644 --- a/src/MiniMock/Builders/Helpers.cs +++ b/src/MiniMock/Builders/Helpers.cs @@ -2,10 +2,7 @@ namespace MiniMock.Builders; using System; using System.Collections.Generic; -using System.Collections.Immutable; -using System.Globalization; using System.Linq; -using System.Threading; using Microsoft.CodeAnalysis; public static class Helpers @@ -31,7 +28,7 @@ public static string AccessibilityString(this Accessibility accessibility) => Accessibility.Protected => "protected", Accessibility.ProtectedOrInternal => "protected internal", Accessibility.Public => "public", - _ => throw new UnsupportedAccessibilityException(accessibility) + _ => throw new UnsupportedAccessibilityException(accessibility), }; internal static void BuildHelpers(this List helpers, CodeBuilder builder, string name) @@ -47,9 +44,7 @@ internal static void BuildHelpers(this List helpers, CodeBuilde foreach (var grouping in signatures) { - builder.Add($""" - /// - """); + builder.Add("""/// """); grouping.Select(t => t.Documentation).Where(t => !string.IsNullOrWhiteSpace(t)).Distinct().ToList().ForEach(t => builder.Add("/// " + t)); if (grouping.Any(t => t.SeeCref != "")) @@ -58,10 +53,10 @@ internal static void BuildHelpers(this List helpers, CodeBuilde builder.Add("/// Configures " + string.Join(", ", crefs)); } - builder.Add($""" - /// - /// The updated configuration. - """); + builder.Add(""" + /// + /// The updated configuration. + """); builder.Add($"public Config {name}({grouping.Key}) {{").Indent(); foreach (var mse in grouping) @@ -77,14 +72,14 @@ internal static void BuildHelpers(this List helpers, CodeBuilde builder.Unindent().Add("}"); } - internal static string OutString(this IParameterSymbol parameterSymbol) => + internal static string OutAsString(this IParameterSymbol parameterSymbol) => parameterSymbol.RefKind switch { RefKind.Out => "out ", RefKind.Ref => "ref ", RefKind.In => "in ", RefKind.RefReadOnlyParameter => "ref readonly ", - _ => "" + _ => "", }; internal static bool HasParameters(this IMethodSymbol method) => method.Parameters.Length > 0; @@ -115,7 +110,7 @@ internal static (string containingSymbol, string accessibilityString, string ove internal static (string methodParameters, string parameterList, string typeList, string nameList) ParameterStrings(this IMethodSymbol method) { - var parameters = method.Parameters.Select(t => new ParameterInfo(t.Type.ToString(), t.Name, t.OutString(), t.Name)).ToList(); + var parameters = method.Parameters.Select(t => new ParameterInfo(t.Type.ToString(), t.Name, t.OutAsString(), t.Name)).ToList(); var methodParameters = parameters.ToString(p => $"{p.OutString}{p.Type} {p.Name}"); diff --git a/src/MiniMock/Builders/PropertyBuilder.cs b/src/MiniMock/Builders/PropertyBuilder.cs index 8ce8409..7c2d8dc 100644 --- a/src/MiniMock/Builders/PropertyBuilder.cs +++ b/src/MiniMock/Builders/PropertyBuilder.cs @@ -19,11 +19,6 @@ public static void BuildProperties(CodeBuilder builder, IEnumerable locations, string message) - { - foreach (var l in locations) - { - context.ReportDiagnostic(Diagnostic.Create(Mm0004, l, message)); - } - } - private static readonly DiagnosticDescriptor Mm0005 = new("MM0005", "Unsupported feature", "{0}", "Usage", DiagnosticSeverity.Error, true); public static void AddStaticAbstractMembersNotSupported(this SourceProductionContext context, IEnumerable locations, string message) { diff --git a/src/MiniMock/MiniMockGenerator.cs b/src/MiniMock/MiniMockGenerator.cs index 5584837..8cbe048 100644 --- a/src/MiniMock/MiniMockGenerator.cs +++ b/src/MiniMock/MiniMockGenerator.cs @@ -51,10 +51,6 @@ private void Build(SourceProductionContext context, ImmutableArray GetAttributes(GeneratorAttributeSyntaxContext internal class UnsupportedAccessibilityException(Accessibility accessibility) : Exception($"Unsupported accessibility type '{accessibility}'"); internal class RefPropertyNotSupportedException(IPropertySymbol propertySymbol, ITypeSymbol typeSymbol) : Exception($"Ref property not supported for '{propertySymbol.Name}' in '{typeSymbol.Name}'" ); internal class RefReturnTypeNotSupportedException(IMethodSymbol methodSymbol, ITypeSymbol typeSymbol) : Exception($"Ref return type not supported for '{methodSymbol.Name}' in '{typeSymbol.Name}'" ); -internal class GenericMethodNotSupportedException(IMethodSymbol methodSymbol, ITypeSymbol typeSymbol) : Exception($"Generic methods in non generic interfaces or classes is not currently supported for '{methodSymbol.Name}' in '{typeSymbol.Name}'" ); internal class StaticAbstractMembersNotSupportedException(string name, ITypeSymbol typeSymbol) : Exception($"Static abstract members in interfaces or classes is not supported for '{name}' in '{typeSymbol.Name}'" ); internal class CanNotMockASealedClassException(ITypeSymbol typeSymbol) : Exception($"Cannot mock the sealed class '{typeSymbol.Name}'" ); diff --git a/tests/MiniMock.Tests/Demo.cs b/tests/MiniMock.Tests/Demo.cs index 7b0dcc6..982b03c 100644 --- a/tests/MiniMock.Tests/Demo.cs +++ b/tests/MiniMock.Tests/Demo.cs @@ -1,5 +1,6 @@ // ReSharper disable UnusedParameter.Local // ReSharper disable UnusedVariable +// ReSharper disable RedundantLambdaParameterType namespace MiniMock.Tests; using Xunit.Abstractions; @@ -46,7 +47,7 @@ public async Task TestingAllTheOptions() .Indexer(get: key => new Version(2, 0, 0, 0), set: (key, value) => { }) // Overwrites the indexer getter and setter .Indexer(values: versions) // Provides a dictionary to retrieve and store versions - .NewVersionAdded(eventArgs: new Version(2, 0, 0, 0)) // Raises the event right away + .NewVersionAdded(raise: new Version(2, 0, 0, 0)) // Raises the event right away .NewVersionAdded(trigger: out triggerNewVersionAdded) // Provides a trigger for when a new version is added ); diff --git a/tests/MiniMock.Tests/EventTests/TriggerEventFromConfiguration.cs b/tests/MiniMock.Tests/EventTests/TriggerEventFromConfiguration.cs index a56f5ee..e62b699 100644 --- a/tests/MiniMock.Tests/EventTests/TriggerEventFromConfiguration.cs +++ b/tests/MiniMock.Tests/EventTests/TriggerEventFromConfiguration.cs @@ -5,12 +5,12 @@ public class TriggerEventFromConfiguration { [Fact] - [Mock] + [Mock] public void ItShouldBePossibleToTriggerEventsFromConfiguration() { // arrange var actual = ""; - var sut = Mock.INotifyDTO(config => config.Value(() => "test", _ => + var sut = Mock.INotifyDto(config => config.Value(() => "test", _ => { config.PropertyChanged(new PropertyChangedEventArgs("Value")); })); @@ -25,7 +25,7 @@ public void ItShouldBePossibleToTriggerEventsFromConfiguration() } [Fact] - [Mock] + [Mock] public void ItShouldBePossibleToTriggerEventsFromAExposedConfiguration() { // Arrange @@ -44,7 +44,7 @@ public void ItShouldBePossibleToTriggerEventsFromAExposedConfiguration() Assert.Equal(new Version(2, 0, 0, 0), actual); } - public interface INotifyDTO : INotifyPropertyChanged + public interface INotifyDto : INotifyPropertyChanged { public string Value { get; set; } } diff --git a/tests/MiniMock.Tests/GeneralTests/AbstractClassesTest.cs b/tests/MiniMock.Tests/GeneralTests/AbstractClassesTest.cs index d6eef36..6c32fef 100644 --- a/tests/MiniMock.Tests/GeneralTests/AbstractClassesTest.cs +++ b/tests/MiniMock.Tests/GeneralTests/AbstractClassesTest.cs @@ -1,5 +1,7 @@ namespace MiniMock.Tests.GeneralTests; +using Xunit.Sdk; + public class AbstractClassesTest { public abstract class AbstractClass @@ -8,13 +10,14 @@ public abstract class AbstractClass public abstract string Abstract { get; set; } public virtual string Virtual { get; set; } = ""; - public string NotAbstractGetOnly { get; } = ""; + public string NotAbstractGetOnly => this.abstractValue ?? ""; public abstract string AbstractGetOnly { get; } - public virtual string VirtualGetOnly { get; } = ""; + public virtual string VirtualGetOnly => throw new TestClassException("this should never be called"); - public string NotAbstractSetOnly { set { } } + private string? abstractValue; + public string NotAbstractSetOnly { set => this.abstractValue = value; } public abstract string AbstractSetOnly { set; } - public virtual string VirtualSetOnly { set{} } + public virtual string VirtualSetOnly { set => throw new TestClassException("this should never be called"); } } [Fact] @@ -22,13 +25,15 @@ public virtual string VirtualSetOnly { set{} } public void AbstractClassPropertiesCanBeSet() { // Arrange + string? actualVirtualSetOnly = null; + var sut = Mock.AbstractClass(config => config .Abstract("Abstract") .AbstractGetOnly("AbstractGetOnly") .AbstractSetOnly("AbstractSetOnly") .Virtual("Virtual") .VirtualGetOnly("VirtualGetOnly") - .VirtualSetOnly("VirtualSetOnly") + .VirtualSetOnly(set: s => actualVirtualSetOnly = s) ); // ACT @@ -41,26 +46,57 @@ public void AbstractClassPropertiesCanBeSet() Assert.Equal("Virtual",sut.Virtual); Assert.Equal("VirtualGetOnly",sut.VirtualGetOnly); sut.VirtualSetOnly = "VirtualSetOnly"; + Assert.Equal("VirtualSetOnly", actualVirtualSetOnly); + } + + [Fact] + [Mock] + public void NoneAbstractClassPropertiesAreParsedThroughToTheBaseClass() + { + // Arrange + var sut = Mock.AbstractClass(); + + // ACT + sut.NotAbstract = "test"; + var sutNotAbstractGetOnly = sut.NotAbstractGetOnly; + sut.NotAbstractSetOnly = "test"; + + // Assert + Assert.Equal("test", sut.NotAbstract); + Assert.Equal("",sutNotAbstractGetOnly); } [Fact] [Mock] public void AbstractClassPropertiesFunctionsCanBeSet() { + string? actualAbstract = null; + string? actualAbstractSetOnly = null; + string? actualVirtual = null; + string? actualVirtualSetOnly = null; // Arrange var sut = Mock.AbstractClass(config => config - .Abstract(get: () => "Abstract", set:s => {}) + .Abstract(get: () => "Abstract", set:s => actualAbstract = s) .AbstractGetOnly(get: () => "AbstractGetOnly") - .AbstractSetOnly(set: s => { }) - .Virtual(get: () => "Virtual", set: s => {}) + .AbstractSetOnly(set: s => actualAbstractSetOnly = s) + .Virtual(get: () => "Virtual", set: s => actualVirtual = s) .VirtualGetOnly(get:() => "VirtualGetOnly") - .VirtualSetOnly(set : s => {}) + .VirtualSetOnly(set : s => actualVirtualSetOnly = s) ); // ACT - + sut.Abstract = "setAbstract"; + sut.AbstractSetOnly = "setAbstractSetOnly"; + sut.Virtual = "setVirtual"; + sut.VirtualSetOnly = "setVirtualSetOnly"; // Assert + Assert.Equal("setAbstract",actualAbstract); + Assert.Equal("setAbstractSetOnly",actualAbstractSetOnly); + Assert.Equal("setVirtual",actualVirtual); + Assert.Equal("setVirtualSetOnly",actualVirtualSetOnly); + + Assert.Equal("Abstract",sut.Abstract); Assert.Equal("AbstractGetOnly",sut.AbstractGetOnly); sut.AbstractSetOnly = "AbstractSetOnly"; diff --git a/tests/MiniMock.Tests/IndexerTests/GenericIndexerTests.cs b/tests/MiniMock.Tests/IndexerTests/GenericIndexerTests.cs index b9397c5..fff1ce9 100644 --- a/tests/MiniMock.Tests/IndexerTests/GenericIndexerTests.cs +++ b/tests/MiniMock.Tests/IndexerTests/GenericIndexerTests.cs @@ -2,9 +2,9 @@ public class GenericIndexerTests { - public interface IGenericIndexRepository where U : new() where T : notnull + public interface IGenericIndexRepository where TU : new() where T : notnull { - U this[T index] { get; set; } + TU this[T index] { get; set; } } [Fact] diff --git a/tests/MiniMock.Tests/IndexerTests/StaticInterfaceTests.cs b/tests/MiniMock.Tests/IndexerTests/StaticInterfaceTests.cs index 5f4b8b0..b179e18 100644 --- a/tests/MiniMock.Tests/IndexerTests/StaticInterfaceTests.cs +++ b/tests/MiniMock.Tests/IndexerTests/StaticInterfaceTests.cs @@ -4,16 +4,13 @@ public class StaticInterfaceTests { public interface ISupportedStaticInterfaceMembers { - static ISupportedStaticInterfaceMembers(){} + static ISupportedStaticInterfaceMembers() => StaticProperty = "Set from ctor"; - static int StaticProperty { get; set; } + static string StaticProperty { get; set; } static string StaticMethod() => "value"; static event EventHandler? StaticEvent; - static void DoStaticEvent() - { - StaticEvent?.Invoke(null, EventArgs.Empty); - } + static void DoStaticEvent() => StaticEvent?.Invoke(null, EventArgs.Empty); static virtual string Bar => "value"; // with implementation } diff --git a/tests/MiniMock.Tests/MethodTests/GenericInterfaceTests.cs b/tests/MiniMock.Tests/MethodTests/GenericInterfaceTests.cs index 50ff865..6a9aae8 100644 --- a/tests/MiniMock.Tests/MethodTests/GenericInterfaceTests.cs +++ b/tests/MiniMock.Tests/MethodTests/GenericInterfaceTests.cs @@ -9,6 +9,8 @@ public class GenericInterfaceTests public void GenericParameter(TKey source); public TKey Name { get; set; } + + public TValue Value { get; set; } } public interface IGeneric where T : notnull @@ -31,21 +33,33 @@ public void GenericStringClass_ShouldReturnGenericType() var sut = Mock.IGeneric(mock => mock .Indexer([]) .ReturnGenericType("Result") - .TryParse((string value, out string result) => { result = value.ToString(); return true; }) ); // Act - sut.EventWithArgs += (sender, e) => { }; + sut.EventWithArgs += (_, _) => { }; var actual = sut.ReturnGenericType(); - var t = sut.TryParse("test", out var result2); - sut["key"] = "value"; - // Assert Assert.Equal("Result", actual); } + [Fact] + public void GenericStringClass_TryParseShouldReturnTrue() + { + // Arrange + var sut = Mock.IGeneric(mock => mock + .TryParse((string value, out string result) => { result = "parsed " + value; return true; }) + ); + + // Act + var actual = sut.TryParse("test", out var actualResult); + + // Assert + Assert.True(actual); + Assert.Equal("parsed test", actualResult); + } + [Fact] public void GenericIntClass_ShouldReturnGenericType() { @@ -96,31 +110,32 @@ public interface IGenericMethod T ReturnGeneric(string value) where T : struct; IEnumerable ReturnDerived(string value) where T : struct; void ReturnVoid(string value) where T : struct; - T ReturnTwoGenerics(string value) where T : struct where U : struct; + T ReturnTwoGenerics(string value) where T : struct where TU : struct; } [Mock] [Fact] - public void METHOD() + public void MockCallFunctionGetTheTypeOfTheGenericAsParameter() { - // Arrange - var sut = Mock.IGenericMethod(config => - { - object Call(string value, Type t) - { - if(t == typeof(int)) - return 10; - return true; - } - - config.ReturnGeneric(10).ReturnGeneric(call: Call); - }); + var sut = Mock.IGenericMethod(config => config.ReturnGeneric(call: ReturnGeneric)); // ACT - sut.ReturnGeneric("test"); - sut.ReturnGeneric("test"); + var actualInt = sut.ReturnGeneric("test"); + var actualBool = sut.ReturnGeneric("test"); // Assert + Assert.Equal(10, actualInt); + Assert.True(actualBool); + return; + // Arrange + object ReturnGeneric(string value, Type t) + { + if (t == typeof(int)) + { + return 10; + } + return true; + } } } diff --git a/tests/MiniMock.Tests/MethodTests/InheritanceTests.cs b/tests/MiniMock.Tests/MethodTests/InheritanceTests.cs index a8197a3..02e4a6c 100644 --- a/tests/MiniMock.Tests/MethodTests/InheritanceTests.cs +++ b/tests/MiniMock.Tests/MethodTests/InheritanceTests.cs @@ -1,13 +1,10 @@ namespace MiniMock.Tests.MethodTests; -using System.ComponentModel; -using System.Runtime.CompilerServices; - public class InheritanceTests { public interface IBase { - bool Method1(); + bool ReturnBool(); void Method2(); bool Method3(string name); bool Method4(out string name); @@ -18,7 +15,7 @@ public interface IBase public interface IDerived : IBase { - new bool Method1(); + new bool ReturnBool(); new void Method2(); new bool Method3(string name); new bool Method4(out string name); @@ -31,10 +28,10 @@ public interface IDerived : IBase [Mock] public void FactMethodName() { - var sut = Mock.IDerived(config => config.Method1(true)); + var sut = Mock.IDerived(config => config.ReturnBool(true)); - Assert.True(sut.Method1()); - Assert.True(((IBase)sut).Method1()); + Assert.True(sut.ReturnBool()); + Assert.True(((IBase)sut).ReturnBool()); Assert.Throws(() => ((IBase)sut).Method6()); } @@ -49,22 +46,4 @@ public void FactMethodName2() Assert.Equal((string?)"Mocked", (string?)sut.Method6()); Assert.Equal("Mocked", ((IBase)sut).Method6()); } - - public class TestNotifyPropertyChanged : INotifyPropertyChanged - { - public event PropertyChangedEventHandler? PropertyChanged; - } - - [Fact] - [Mock] - public void METHOD() - { - // Arrange - - - // ACT - - // Assert - - } } diff --git a/tests/MiniMock.Tests/Playground.cs b/tests/MiniMock.Tests/Playground.cs index 7df0821..58671bd 100644 --- a/tests/MiniMock.Tests/Playground.cs +++ b/tests/MiniMock.Tests/Playground.cs @@ -1,6 +1,3 @@ using MiniMock; -using MiniMock.Tests; [assembly:Mock(MockFactoryName = "Mock")] - -namespace MiniMock.Tests; diff --git a/tests/MiniMock.Tests/PropertyTests/InheritanceTests.cs b/tests/MiniMock.Tests/PropertyTests/InheritanceTests.cs index ef23f3c..3e2d109 100644 --- a/tests/MiniMock.Tests/PropertyTests/InheritanceTests.cs +++ b/tests/MiniMock.Tests/PropertyTests/InheritanceTests.cs @@ -2,20 +2,6 @@ namespace MiniMock.Tests.PropertyTests; public class InheritanceTests { - public interface IBaseWithProperties - { - string Name1 { get; set; } - string Name2 { set; } - string Name3 { get; } - } - - public interface IDerivedWithProperties : IBaseWithProperties - { - new string Name1 { get; set; } - new string Name2 { set; } - new string Name3 { get; } - } - [Fact] [Mock] public void CallsToBaseInterfaceShouldThrowException() @@ -41,25 +27,18 @@ public void CallsToDerivedInterfaceShouldThrowException() Assert.Throws(() => sut.Name2 = "test"); Assert.Throws(() => dummy = sut.Name3); } -} -public class NullableBase -{ - public interface INullIntTest + public interface IBaseWithProperties { - int IntValue { get; set; } + string Name1 { get; set; } + string Name2 { set; } + string Name3 { get; } } - [Fact] - [Mock] - public void METHOD() + public interface IDerivedWithProperties : IBaseWithProperties { - // Arrange - var sut = Mock.INullIntTest(); - - // ACT - - // Assert - + new string Name1 { get; set; } + new string Name2 { set; } + new string Name3 { get; } } } diff --git a/tests/MiniMock.UnitTests/DiagnosticsTests.cs b/tests/MiniMock.UnitTests/DiagnosticsTests.cs index ed81f9f..699d530 100644 --- a/tests/MiniMock.UnitTests/DiagnosticsTests.cs +++ b/tests/MiniMock.UnitTests/DiagnosticsTests.cs @@ -55,7 +55,7 @@ public sealed class SealedClass } [Fact] - public void MockingSealedClasesWillRaiseTheMM0006Error() + public void MockingSealedClasesWillRaiseTheMm0006Error() { var source = Build.TestClass(); diff --git a/tests/MiniMock.UnitTests/GenericsTests.cs b/tests/MiniMock.UnitTests/GenericsTests.cs index f78b0eb..508ed99 100644 --- a/tests/MiniMock.UnitTests/GenericsTests.cs +++ b/tests/MiniMock.UnitTests/GenericsTests.cs @@ -78,7 +78,7 @@ public interface IGenericMethod T ReturnGeneric(string value) where T : struct; IEnumerable ReturnDerived(string value) where T : struct; void ReturnVoid(string value) where T : struct; - T ReturnTwoGenerics(string value) where T : struct where U : struct; + T ReturnTwoGenerics(string value) where T : struct where TU : struct; } [Fact] diff --git a/tests/MiniMock.UnitTests/InheritanceTests.cs b/tests/MiniMock.UnitTests/InheritanceTests.cs index 564b4ee..6e0f278 100644 --- a/tests/MiniMock.UnitTests/InheritanceTests.cs +++ b/tests/MiniMock.UnitTests/InheritanceTests.cs @@ -137,9 +137,9 @@ public void IndexerInheritanceTests() public class DoubleInheritanceTest(ITestOutputHelper testOutputHelper) { - public interface IDoubleInheritance : System.Collections.Generic.ICollection, System.Collections.Generic.IList + // ReSharper disable once RedundantExtendsListEntry (disabled since this is the point of the test) + internal interface IDoubleInheritance : ICollection, IList { - } [Fact] diff --git a/tests/MiniMock.UnitTests/PropertyTest.cs b/tests/MiniMock.UnitTests/PropertyTest.cs index 72dca73..2019457 100644 --- a/tests/MiniMock.UnitTests/PropertyTest.cs +++ b/tests/MiniMock.UnitTests/PropertyTest.cs @@ -1,5 +1,7 @@ namespace MiniMock.UnitTests; +using Xunit.Sdk; + public class PropertyTest(ITestOutputHelper testOutputHelper) { public interface IPropertyInterface @@ -21,9 +23,9 @@ public abstract class AbstractClassWithDifferentProperties public abstract string AbstractGetOnly { get; } public virtual string VirtualGetOnly { get; } = ""; - public string NotAbstractSetOnly { set { } } + public string NotAbstractSetOnly { set => throw new TestClassException("This should not be accessed"); } public abstract string AbstractSetOnly { set; } - public virtual string VirtualSetOnly { set{} } + public virtual string VirtualSetOnly { set => throw new TestClassException("This should not be accessed"); } } [Fact] diff --git a/tests/MiniMock.UnitTests/StaticInterfaceMembersTest.cs b/tests/MiniMock.UnitTests/StaticInterfaceMembersTest.cs index 2af5805..689ff1b 100644 --- a/tests/MiniMock.UnitTests/StaticInterfaceMembersTest.cs +++ b/tests/MiniMock.UnitTests/StaticInterfaceMembersTest.cs @@ -1,10 +1,12 @@ namespace MiniMock.UnitTests; +using Microsoft.CodeAnalysis; + public class StaticInterfaceMembersTest(ITestOutputHelper testOutputHelper) { public interface ISupportedStaticInterfaceMembers { - static ISupportedStaticInterfaceMembers() { } + static ISupportedStaticInterfaceMembers() => StaticProperty = 10; static int StaticProperty { get; set; } static string StaticMethod() => "value"; @@ -34,6 +36,7 @@ public interface IStaticAbstractInterfaceMembers static abstract string AbstractProperty { get; set; } static abstract string AbstractMethod(); static abstract event EventHandler StaticEvent; + static string AbstractMethod1() => "value"; } [Fact] @@ -46,9 +49,10 @@ public void AbstractStaticMembersAreNotSupported() var generate = new MiniMockGenerator().Generate(source); // Assert - testOutputHelper.DumpResult(generate); - - var actualTypeArgumentError = Assert.Single(generate.diagnostics, t => t.Id == "CS8920"); + //testOutputHelper.DumpResult(generate); + Assert.Single(generate.diagnostics, t => t.Id == "CS8920"); var actualAbstractPropertyError = Assert.Single(generate.diagnostics, t => t.Id == "MM0005"); + Assert.Equal(DiagnosticSeverity.Error, actualAbstractPropertyError.Severity); + Assert.Equal("Static abstract members in interfaces or classes is not supported for 'AbstractMethod' in 'IStaticAbstractInterfaceMembers'",actualAbstractPropertyError.GetMessage()); } }