Skip to content

Commit

Permalink
Rebasing on latest
Browse files Browse the repository at this point in the history
  • Loading branch information
Keboo committed Feb 2, 2024
1 parent d95d06b commit b7c24bb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 149 deletions.
9 changes: 8 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ csharp_space_between_square_brackets = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true
csharp_style_namespace_declarations=file_scoped:warning
csharp_style_namespace_declarations= file_scoped:warning
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion

#### Naming styles ####
[*.{cs,vb}]
Expand Down Expand Up @@ -362,4 +366,7 @@ dotnet_naming_style.s_camelcase.required_prefix = s_
dotnet_naming_style.s_camelcase.required_suffix =
dotnet_naming_style.s_camelcase.word_separator =
dotnet_naming_style.s_camelcase.capitalization = camel_case
tab_width = 4
indent_size = 4
end_of_line = crlf

Original file line number Diff line number Diff line change
Expand Up @@ -89,42 +89,44 @@ public class Controller { }
[Description("Issue 142")]
public async Task Generation_WithGenericParameter_RemovesInvalidCharactersFromTestsName()
{
var code = @"
using Moq.AutoMock;
var code = """
using Moq.AutoMock;
namespace TestNamespace;
namespace TestNamespace;
[ConstructorTests(typeof(Controller))]
public partial class ControllerTests
{
[ConstructorTests(typeof(Controller))]
public partial class ControllerTests
{
}
}
public class Controller
{
public Controller(ILogger<Controller> logger) { }
}
public class Controller
{
public Controller(ILogger<Controller> logger) { }
}
public interface ILogger<Controller> { }
";
string expected = @"namespace TestNamespace
{
partial class ControllerTests
{
partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName);
public interface ILogger<Controller> { }
""";
string expected = """
namespace TestNamespace
{
partial class ControllerTests
{
partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName);
partial void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(Moq.AutoMock.AutoMocker mocker);
partial void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(Moq.AutoMock.AutoMocker mocker);
public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException()
{
Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker();
AutoMockerTestSetup(mocker, ""ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException"");
ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(mocker);
}
public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException()
{
Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker();
AutoMockerTestSetup(mocker, "ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException");
ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(mocker);
}
}
}
";
}
}
""";

await new VerifyCS.Test
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VerifyCS = Moq.AutoMocker.Generators.Tests.CSharpSourceGeneratorVerifier<Moq.AutoMocker.Generators.UnitTestSourceGenerator>;

using VerifyCS = Moq.AutoMocker.TestGenerator.Tests.CSharpSourceGeneratorVerifier<Moq.AutoMocker.TestGenerator.UnitTestSourceGenerator>;

namespace Moq.AutoMocker.TestGenerator.Tests;
namespace Moq.AutoMocker.Generators.Tests;

[TestClass]
public class TestGeneratorTests
Expand Down Expand Up @@ -136,7 +135,7 @@ public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullEx
GetSourceFile(expected, "ControllerTests.g.cs")
}
}

}.RunAsync();
}

Expand Down
46 changes: 27 additions & 19 deletions Moq.AutoMocker.Generators/SyntaxReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using static Moq.AutoMocker.Generators.AutoMock;

namespace Moq.AutoMocker.Generators;

public class SyntaxReceiver : ISyntaxContextReceiver
Expand All @@ -21,29 +23,35 @@ public class SyntaxReceiver : ISyntaxContextReceiver
: null;
}

private bool GetSkipNullableParameters(AttributeSyntax attributeSyntax)
{
return attributeSyntax.ArgumentList?.Arguments.Count > 0 &&
attributeSyntax.ArgumentList!.Arguments
.Select(x => x.Expression)
.OfType<MemberAccessExpressionSyntax>()
.FirstOrDefault() is { Name.Identifier.ValueText: IgnoreNullableParametersEnumValue };
}


public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (context.Node is ClassDeclarationSyntax classDeclaration &&
context.SemanticModel.GetDeclaredSymbol(classDeclaration) is INamedTypeSymbol symbol &&
classDeclaration.AttributeLists.SelectMany(x => x.Attributes)
.Select(a =>
{
if (context.SemanticModel.GetTypeInfo(a).Type?.Name == AutoMock.ConstructorTestsAttribute)
{
if (GetTargetType(a) is { } targetType &&
context.SemanticModel.GetTypeInfo(targetType).Type is INamedTypeSymbol sutType)
{
return sutType;
}
Diagnostic diagnostic = Diagnostics.MustSpecifyTargetType.Create(a.GetLocation(),
symbol.OriginalDefinition.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat));
DiagnosticMessages.Add(diagnostic);
}
return null;
})
.FirstOrDefault(a => a is not null) is { } sutType
)
.Select(a => context.SemanticModel.GetTypeInfo(a).Type?.Name == ConstructorTestsAttribute ? a : null)
.FirstOrDefault(a => a is not null) is { } attribute)
{
if (!(GetTargetType(attribute) is { } targetType) ||
context.SemanticModel.GetTypeInfo(targetType).Type is not INamedTypeSymbol sutType)
{
Diagnostic diagnostic = Diagnostics.MustSpecifyTargetType.Create(attribute.GetLocation(),
symbol.OriginalDefinition.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat));
DiagnosticMessages.Add(diagnostic);
return;
}

bool skipNullableParameters = GetSkipNullableParameters(attribute);

if (!classDeclaration.Modifiers.Any(x => x.IsKind(SyntaxKind.PartialKeyword)))
{
Diagnostic diagnostic = Diagnostics.TestClassesMustBePartial.Create(classDeclaration.GetLocation(),
Expand All @@ -66,7 +74,6 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
int nullIndex = 0;
foreach (IParameterSymbol parameter in ctor.Parameters)
{
if (parameter.Type.IsValueType) continue;
sut.NullConstructorParameterTests.Add(new NullConstructorParameterTest()
{
Parameters = parameters,
Expand All @@ -81,7 +88,8 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
Namespace = namespaceDeclaration,
TestClassName = testClassName,
Sut = sut
Sut = sut,
SkipNullableParameters = skipNullableParameters
};

TestClasses.Add(targetClass);
Expand Down
97 changes: 0 additions & 97 deletions Moq.AutoMocker.TestGenerator/SyntaxReceiver.cs

This file was deleted.

0 comments on commit b7c24bb

Please sign in to comment.