diff --git a/Source/Mockolate.SourceGenerators/Entities/Class.cs b/Source/Mockolate.SourceGenerators/Entities/Class.cs index 13b1df09..ef0110fb 100644 --- a/Source/Mockolate.SourceGenerators/Entities/Class.cs +++ b/Source/Mockolate.SourceGenerators/Entities/Class.cs @@ -1,9 +1,7 @@ using System.Collections.Immutable; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Text; using Microsoft.CodeAnalysis; -using Mockolate.SourceGenerators.Internals; namespace Mockolate.SourceGenerators.Entities; @@ -17,6 +15,7 @@ internal class Class : IEquatable private List? _allProperties; private string? _classNameWithoutDots; +#pragma warning disable S107 // Methods should not have too many parameters public Class(ITypeSymbol type, IAssemblySymbol sourceAssembly, List? alreadyDefinedMethods = null, @@ -25,6 +24,7 @@ public Class(ITypeSymbol type, List? exceptMethods = null, List? exceptProperties = null, List? exceptEvents = null) +#pragma warning restore S107 { _sourceAssembly = sourceAssembly; ClassFullName = type.ToDisplayString(Helpers.TypeDisplayFormat); @@ -34,11 +34,21 @@ public Class(ITypeSymbol type, INamedTypeSymbol? containingType = type.ContainingType; if (containingType is not null) { + List ancestors = new(); while (containingType is not null) { - ClassName = containingType.Name + "." + ClassName; + ancestors.Add(containingType.Name); containingType = containingType.ContainingType; } + + StringBuilder nameBuilder = new(); + for (int i = ancestors.Count - 1; i >= 0; i--) + { + nameBuilder.Append(ancestors[i]).Append('.'); + } + + nameBuilder.Append(ClassName); + ClassName = nameBuilder.ToString(); } IsInterface = type.TypeKind == TypeKind.Interface; @@ -151,6 +161,18 @@ bool ShouldIncludeMember(ISymbol member) } } + public EquatableArray Methods { get; } + public EquatableArray InheritedTypes { get; } + public EquatableArray Properties { get; } + public EquatableArray Events { get; } + public EquatableArray ReservedNames { get; } + + public bool IsInterface { get; } + public bool HasRequiredMembers { get; } + public string ClassFullName { get; } + public string ClassName { get; } + public string DisplayString { get; } + /// /// Folds the member surface (methods, properties, events, recursive base/interface chain, /// reserved names, kind, required-member flag) into a single content-derived integer. @@ -173,18 +195,6 @@ private int ComputeSurfaceHash() return hash; } - public EquatableArray Methods { get; } - public EquatableArray InheritedTypes { get; } - public EquatableArray Properties { get; } - public EquatableArray Events { get; } - public EquatableArray ReservedNames { get; } - - public bool IsInterface { get; } - public bool HasRequiredMembers { get; } - public string ClassFullName { get; } - public string ClassName { get; } - public string DisplayString { get; } - /// /// Identifiers that the mock class shares its scope with but that aren't surfaced through /// Methods/Properties/Events: generic type parameters of the type itself, nested types, and diff --git a/Tests/Mockolate.Internal.Tests/Interactions/FastMockInteractionsTests.cs b/Tests/Mockolate.Internal.Tests/Interactions/FastMockInteractionsTests.cs index fc2ae853..e4448297 100644 --- a/Tests/Mockolate.Internal.Tests/Interactions/FastMockInteractionsTests.cs +++ b/Tests/Mockolate.Internal.Tests/Interactions/FastMockInteractionsTests.cs @@ -284,11 +284,17 @@ public async Task GetOrCreateFallbackBuffer_BarrierSynchronizedRace_AllCallersRe }, TaskCreationOptions.LongRunning); } +#if NET48 + CancellationToken cancellationToken = TestContext.Current.CancellationToken; +#else + CancellationToken cancellationToken = CancellationToken.None; +#endif + for (int round = 0; round < rounds; round++) { fallbackField.SetValue(sut, null); - barrier.SignalAndWait(); - barrier.SignalAndWait(); + barrier.SignalAndWait(cancellationToken); + barrier.SignalAndWait(cancellationToken); object? installed = fallbackField.GetValue(sut); await That(installed).IsNotNull(); diff --git a/Tests/Mockolate.SourceGenerators.Tests/Entities/TypeIsFormattableTests.cs b/Tests/Mockolate.SourceGenerators.Tests/Entities/TypeIsFormattableTests.cs index c2693d5f..f7b1af16 100644 --- a/Tests/Mockolate.SourceGenerators.Tests/Entities/TypeIsFormattableTests.cs +++ b/Tests/Mockolate.SourceGenerators.Tests/Entities/TypeIsFormattableTests.cs @@ -19,11 +19,11 @@ public async Task WhenSymbolIsObject_ShouldNotReportFormattable() [MetadataReference.CreateFromFile(typeof(object).Assembly.Location),], new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); SemanticModel model = compilation.GetSemanticModel(tree); - FieldDeclarationSyntax declaration = tree.GetRoot().DescendantNodes() + FieldDeclarationSyntax declaration = tree.GetRoot(TestContext.Current.CancellationToken).DescendantNodes() .OfType() .First(); VariableDeclaratorSyntax variable = declaration.Declaration.Variables.Single(); - IFieldSymbol fieldSymbol = (IFieldSymbol)model.GetDeclaredSymbol(variable)!; + IFieldSymbol fieldSymbol = (IFieldSymbol)model.GetDeclaredSymbol(variable, TestContext.Current.CancellationToken)!; Type type = Type.From(fieldSymbol.Type); @@ -43,7 +43,7 @@ public class Holder public IFormattable Value; } """; - SyntaxTree tree = CSharpSyntaxTree.ParseText(source); + SyntaxTree tree = CSharpSyntaxTree.ParseText(source, cancellationToken: TestContext.Current.CancellationToken); CSharpCompilation compilation = CSharpCompilation.Create( "TestAssembly", [tree,], @@ -53,11 +53,11 @@ public class Holder ], new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); SemanticModel model = compilation.GetSemanticModel(tree); - FieldDeclarationSyntax declaration = tree.GetRoot().DescendantNodes() + FieldDeclarationSyntax declaration = tree.GetRoot(TestContext.Current.CancellationToken).DescendantNodes() .OfType() .First(); VariableDeclaratorSyntax variable = declaration.Declaration.Variables.Single(); - IFieldSymbol fieldSymbol = (IFieldSymbol)model.GetDeclaredSymbol(variable)!; + IFieldSymbol fieldSymbol = (IFieldSymbol)model.GetDeclaredSymbol(variable, TestContext.Current.CancellationToken)!; ITypeSymbol typeSymbol = fieldSymbol.Type; Type type = Type.From(typeSymbol); diff --git a/Tests/Mockolate.Tests/MockBehaviorTests.InitializeTests.cs b/Tests/Mockolate.Tests/MockBehaviorTests.InitializeTests.cs index 0a3f8b9b..2a11a1de 100644 --- a/Tests/Mockolate.Tests/MockBehaviorTests.InitializeTests.cs +++ b/Tests/Mockolate.Tests/MockBehaviorTests.InitializeTests.cs @@ -9,8 +9,9 @@ public sealed class InitializeTests [Fact] public async Task Initialize_DirectSetupsTakePrecedence() { - MockBehavior behavior = MockBehavior.Default.Initialize(setup - => setup[It.Satisfies(s => s.StartsWith("da"))].InitializeWith(5)); + MockBehavior behavior = MockBehavior.Default.Initialize( + (Mock.IMockSetupForIChocolateDispenser setup) + => setup[It.Satisfies((string s) => s.StartsWith("da"))].InitializeWith(5)); IChocolateDispenser sut = IChocolateDispenser.CreateMock(behavior, setup => setup[It.Satisfies(s => s.EndsWith("rk"))].InitializeWith(16)); @@ -30,8 +31,9 @@ public async Task Initialize_DirectSetupsTakePrecedence() public async Task Initialize_OtherType_ShouldIgnoreInitializations() { MockBehavior behavior = - MockBehavior.Default.Initialize(setup - => setup[It.Is("Dark")].InitializeWith(15)); + MockBehavior.Default.Initialize( + (Mock.IMockSetupForIChocolateDispenser setup) + => setup[It.Is("Dark")].InitializeWith(15)); void Act() { @@ -45,8 +47,9 @@ void Act() public async Task Initialize_ShouldApplySetupToCreatedMock() { MockBehavior behavior = - MockBehavior.Default.Initialize(setup - => setup[It.Is("Dark")].InitializeWith(15)); + MockBehavior.Default.Initialize( + (Mock.IMockSetupForIChocolateDispenser setup) + => setup[It.Is("Dark")].InitializeWith(15)); IChocolateDispenser sut = IChocolateDispenser.CreateMock(behavior);