Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions Source/Mockolate.SourceGenerators/Entities/Class.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,6 +15,7 @@ internal class Class : IEquatable<Class>
private List<Property>? _allProperties;
private string? _classNameWithoutDots;

#pragma warning disable S107 // Methods should not have too many parameters
public Class(ITypeSymbol type,
IAssemblySymbol sourceAssembly,
List<Method>? alreadyDefinedMethods = null,
Expand All @@ -25,6 +24,7 @@ public Class(ITypeSymbol type,
List<Method>? exceptMethods = null,
List<Property>? exceptProperties = null,
List<Event>? exceptEvents = null)
#pragma warning restore S107
{
_sourceAssembly = sourceAssembly;
ClassFullName = type.ToDisplayString(Helpers.TypeDisplayFormat);
Expand All @@ -34,11 +34,21 @@ public Class(ITypeSymbol type,
INamedTypeSymbol? containingType = type.ContainingType;
if (containingType is not null)
{
List<string> 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;
Expand Down Expand Up @@ -151,6 +161,18 @@ bool ShouldIncludeMember(ISymbol member)
}
}

public EquatableArray<Method> Methods { get; }
public EquatableArray<Class> InheritedTypes { get; }
public EquatableArray<Property> Properties { get; }
public EquatableArray<Event> Events { get; }
public EquatableArray<string> ReservedNames { get; }

public bool IsInterface { get; }
public bool HasRequiredMembers { get; }
public string ClassFullName { get; }
public string ClassName { get; }
public string DisplayString { get; }

/// <summary>
/// Folds the member surface (methods, properties, events, recursive base/interface chain,
/// reserved names, kind, required-member flag) into a single content-derived integer.
Expand All @@ -173,18 +195,6 @@ private int ComputeSurfaceHash()
return hash;
}

public EquatableArray<Method> Methods { get; }
public EquatableArray<Class> InheritedTypes { get; }
public EquatableArray<Property> Properties { get; }
public EquatableArray<Event> Events { get; }
public EquatableArray<string> ReservedNames { get; }

public bool IsInterface { get; }
public bool HasRequiredMembers { get; }
public string ClassFullName { get; }
public string ClassName { get; }
public string DisplayString { get; }

/// <summary>
/// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldDeclarationSyntax>()
.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);

Expand All @@ -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,],
Expand All @@ -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<FieldDeclarationSyntax>()
.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);
Expand Down
15 changes: 9 additions & 6 deletions Tests/Mockolate.Tests/MockBehaviorTests.InitializeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public sealed class InitializeTests
[Fact]
public async Task Initialize_DirectSetupsTakePrecedence()
{
MockBehavior behavior = MockBehavior.Default.Initialize<IChocolateDispenser>(setup
=> setup[It.Satisfies<string>(s => s.StartsWith("da"))].InitializeWith(5));
MockBehavior behavior = MockBehavior.Default.Initialize<IChocolateDispenser>(
(Mock.IMockSetupForIChocolateDispenser setup)
=> setup[It.Satisfies<string>((string s) => s.StartsWith("da"))].InitializeWith(5));

IChocolateDispenser sut = IChocolateDispenser.CreateMock(behavior,
setup => setup[It.Satisfies<string>(s => s.EndsWith("rk"))].InitializeWith(16));
Expand All @@ -30,8 +31,9 @@ public async Task Initialize_DirectSetupsTakePrecedence()
public async Task Initialize_OtherType_ShouldIgnoreInitializations()
{
MockBehavior behavior =
MockBehavior.Default.Initialize<IChocolateDispenser>(setup
=> setup[It.Is("Dark")].InitializeWith(15));
MockBehavior.Default.Initialize<IChocolateDispenser>(
(Mock.IMockSetupForIChocolateDispenser setup)
=> setup[It.Is("Dark")].InitializeWith(15));

void Act()
{
Expand All @@ -45,8 +47,9 @@ void Act()
public async Task Initialize_ShouldApplySetupToCreatedMock()
{
MockBehavior behavior =
MockBehavior.Default.Initialize<IChocolateDispenser>(setup
=> setup[It.Is("Dark")].InitializeWith(15));
MockBehavior.Default.Initialize<IChocolateDispenser>(
(Mock.IMockSetupForIChocolateDispenser setup)
=> setup[It.Is("Dark")].InitializeWith(15));

IChocolateDispenser sut = IChocolateDispenser.CreateMock(behavior);

Expand Down