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
8 changes: 4 additions & 4 deletions src/CodeFixes/SyntaxGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public static SyntaxNode InsertArguments(this SyntaxGenerator generator, SyntaxN
return invocation.WithArgumentList(SyntaxFactory.ArgumentList(arguments));
}

if (syntax is ObjectCreationExpressionSyntax creation)
if (syntax is BaseObjectCreationExpressionSyntax creation)
{
SeparatedSyntaxList<ArgumentSyntax> arguments = creation.ArgumentList?.Arguments ?? [];
arguments = arguments.InsertRange(index, items.OfType<ArgumentSyntax>());
return creation.WithArgumentList(SyntaxFactory.ArgumentList(arguments));
}

throw new ArgumentException($"Must be of type {nameof(InvocationExpressionSyntax)} or {nameof(ObjectCreationExpressionSyntax)} but is of type {syntax.GetType().Name}", nameof(syntax));
throw new ArgumentException($"Must be of type {nameof(InvocationExpressionSyntax)} or {nameof(BaseObjectCreationExpressionSyntax)} but is of type {syntax.GetType().Name}", nameof(syntax));
}

public static SyntaxNode ReplaceArgument(this SyntaxGenerator generator, IOperation operation, int index, SyntaxNode item)
Expand All @@ -59,13 +59,13 @@ public static SyntaxNode ReplaceArgument(this SyntaxGenerator generator, SyntaxN
return invocation.WithArgumentList(SyntaxFactory.ArgumentList(arguments));
}

if (syntax is ObjectCreationExpressionSyntax creation)
if (syntax is BaseObjectCreationExpressionSyntax creation)
{
SeparatedSyntaxList<ArgumentSyntax> arguments = creation.ArgumentList?.Arguments ?? [];
arguments = arguments.RemoveAt(index).Insert(index, argument);
return creation.WithArgumentList(SyntaxFactory.ArgumentList(arguments));
}

throw new ArgumentException($"Must be of type {nameof(InvocationExpressionSyntax)} or {nameof(ObjectCreationExpressionSyntax)} but is of type {syntax.GetType().Name}", nameof(syntax));
throw new ArgumentException($"Must be of type {nameof(InvocationExpressionSyntax)} or {nameof(BaseObjectCreationExpressionSyntax)} but is of type {syntax.GetType().Name}", nameof(syntax));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ public static IEnumerable<object[]> TestData()
],
}.WithNamespaces().WithMoqReferenceAssemblyGroups();

IEnumerable<object[]> mockConstructorsWithTargetTypedNew = new object[][]
{
[
"""Mock<ISample> mock = {|Moq1400:new()|};""",
"""Mock<ISample> mock = new(MockBehavior.Loose);""",
],
[
"""Mock<ISample> mock = {|Moq1400:new(MockBehavior.Default)|};""",
"""Mock<ISample> mock = new(MockBehavior.Loose);""",
],
[
"""Mock<ISample> mock = new(MockBehavior.Loose);""",
"""Mock<ISample> mock = new(MockBehavior.Loose);""",
],
[
"""Mock<ISample> mock = new(MockBehavior.Strict);""",
"""Mock<ISample> mock = new(MockBehavior.Strict);""",
],
}.WithNamespaces().WithMoqReferenceAssemblyGroups();

IEnumerable<object[]> mockConstructorsWithExpressions = new object[][]
{
[
Expand Down Expand Up @@ -89,7 +109,7 @@ public static IEnumerable<object[]> TestData()
],
}.WithNamespaces().WithNewMoqReferenceAssemblyGroups();

return mockConstructors.Union(mockConstructorsWithExpressions).Union(fluentBuilders).Union(mockRepositories);
return mockConstructors.Union(mockConstructorsWithTargetTypedNew).Union(mockConstructorsWithExpressions).Union(fluentBuilders).Union(mockRepositories);
}

[Theory]
Expand Down
Loading