Skip to content
Open
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
129 changes: 45 additions & 84 deletions src/Testing.AutoFixtures/AutoFixtureGenerator+StaticGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Rocket.Surgery.Extensions.Testing.AutoFixtures;

[System.Diagnostics.DebuggerDisplay("{DebuggerDisplay,nq}")]
public partial class AutoFixtureGenerator
{
private static NamespaceDeclarationSyntax BuildNamespace(ISymbol namedTypeSymbol)
Expand Down Expand Up @@ -134,12 +135,10 @@ Compilation compilation
{
var isAbstract = parameterSymbol.IsAbstract;
var isInterface = parameterSymbol.Type.TypeKind == TypeKind.Interface;
var isValueType = parameterSymbol.Type.IsValueType;

var symbolName = $"_{parameterSymbol.Name}";
if (!isAbstract && !isInterface)
{
return FieldDeclaration(
return ( !isAbstract && !isInterface )
? FieldDeclaration(
VariableDeclaration(
IdentifierName(
Identifier(
Expand Down Expand Up @@ -171,10 +170,8 @@ Compilation compilation
TokenList(
Token(SyntaxKind.PrivateKeyword)
)
);
}

return FieldDeclaration(
)
: FieldDeclaration(
VariableDeclaration(
IdentifierName(
Identifier(
Expand Down Expand Up @@ -233,7 +230,7 @@ private static MemberDeclarationSyntax BuildBuildMethod(
IEnumerable<IParameterSymbol> parameterSymbols
)
{
List<SyntaxNodeOrToken> list = new();
List<SyntaxNodeOrToken> list = [];
foreach (var parameterSymbol in parameterSymbols)
{
list.Add(Argument(IdentifierName($"_{parameterSymbol.Name}")));
Expand Down Expand Up @@ -379,17 +376,14 @@ private static MemberDeclarationSyntax WithPropertyMethod(IParameterSymbol const

SyntaxToken withTypeOrParameterName(IParameterSymbol parameterSymbol)
{
var primitiveName = $"{char.ToUpper(parameterSymbol.Name[0])}{parameterSymbol.Name.Substring(1, parameterSymbol.Name.Length - 1)}";
var splitLastCamel = useParameterName(parameterSymbol) ? primitiveName : SplitLastCamel(parameterSymbol);
var primitiveName = $"{ char.ToUpper(parameterSymbol.Name[0]).ToString()}{parameterSymbol.Name[1..]}";
var splitLastCamel = ( useParameterName(parameterSymbol) ) ? primitiveName : SplitLastCamel(parameterSymbol);
return Identifier($"With{splitLastCamel}");
}

bool useParameterName(IParameterSymbol parameterSymbol)
{
return parameterSymbol.Type.TypeKind != TypeKind.Interface
bool useParameterName(IParameterSymbol parameterSymbol) => parameterSymbol.Type.TypeKind != TypeKind.Interface
|| parameterSymbol.Type.IsValueType
|| !parameterSymbol.Type.IsAbstract;
}
}

private static MemberDeclarationSyntax BuildOperator(string className, string fixtureName) =>
Expand Down Expand Up @@ -495,9 +489,8 @@ private static InvocationExpressionSyntax GetFieldInvocation(Compilation compila
{
var fakeItEasy = compilation.GetTypeByMetadataName("FakeItEasy.Fake");

if (fakeItEasy is { })
{
return InvocationExpression(
return ( fakeItEasy is { } )
? InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("A"),
Expand All @@ -508,10 +501,8 @@ private static InvocationExpressionSyntax GetFieldInvocation(Compilation compila
typeArgumentListSyntax(symbol)
)
)
);
}

return InvocationExpression(
)
: InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(
Expand All @@ -530,35 +521,13 @@ private static InvocationExpressionSyntax GetFieldInvocation(Compilation compila
)
);

TypeArgumentListSyntax typeArgumentListSyntax(IParameterSymbol parameterSymbol)
{
return TypeArgumentList(
static TypeArgumentListSyntax typeArgumentListSyntax(IParameterSymbol parameterSymbol) => TypeArgumentList(
SingletonSeparatedList<TypeSyntax>(
ParseName(parameterSymbol.Type.GetGenericDisplayName())
)
);
}
}

private static void ReportDiagnostic(
SourceProductionContext productionContext,
DiagnosticDescriptor diagnosticDescriptor,
IEnumerable<Location> locations
)
{
ReportDiagnostic(productionContext, diagnosticDescriptor, locations.ToArray());
}

private static void ReportDiagnostic(SourceProductionContext productionContext, DiagnosticDescriptor diagnosticDescriptor, params Location[] locations)
{
foreach (var location in locations)
{
productionContext.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, location));
}
}

private const string Fixture = nameof(Fixture);

private static INamedTypeSymbol? GetClassForFixture(GeneratorAttributeSyntaxContext syntaxContext)
{
var targetSymbol = syntaxContext.TargetSymbol as INamedTypeSymbol;
Expand All @@ -568,53 +537,45 @@ private static void ReportDiagnostic(SourceProductionContext productionContext,
return targetSymbol;
}

if (syntaxContext.Attributes[0].ConstructorArguments[0].Value is INamedTypeSymbol namedTypeSymbol)
{
return namedTypeSymbol;
}

return null;
return ( syntaxContext.Attributes[0].ConstructorArguments[0].Value is INamedTypeSymbol namedTypeSymbol ) ? namedTypeSymbol : null;
}


private static bool ReportAutoFixture0001(INamedTypeSymbol classForFixture, SourceProductionContext productionContext)
{
if (classForFixture.Constructors.All(x => x.Parameters.IsDefaultOrEmpty))
{
ReportDiagnostic(productionContext, Diagnostics.AutoFixture0001, classForFixture.Locations);
return true;
}

return false;
}
private static bool ReportAutoFixture0001(INamedTypeSymbol classForFixture, SourceProductionContext productionContext) => classForFixture.Constructors.All(methodSymbol => methodSymbol.Parameters.IsDefaultOrEmpty);


private static bool ReportAutoFixture0002(INamedTypeSymbol namedTypeSymbol, SourceProductionContext productionContext)
{
var reported = false;
foreach (var location in namedTypeSymbol
.Constructors
.SelectMany(methodSymbol => methodSymbol.Parameters)
.Distinct(ParameterReductionComparer.Default)
.Select(parameterSymbol => new { parameterSymbol, isArrayType = parameterSymbol.Type is IArrayTypeSymbol })
.Select(
tuple => new
{
tuple.isArrayType,
tuple.parameterSymbol,
hasParamsKeyWord = tuple.parameterSymbol.ToDisplayString().Contains("params"),
}
)
.Where(tuple => tuple.isArrayType && tuple.hasParamsKeyWord)
.SelectMany(tuple => tuple.parameterSymbol.Locations))
{
productionContext.ReportDiagnostic(Diagnostic.Create(Diagnostics.AutoFixture0002, location));
if (!reported)
{
reported = true;
}
}
const bool reported = false;
// foreach (var location in namedTypeSymbol
// .Constructors
// .SelectMany(methodSymbol => methodSymbol.Parameters)
// .Distinct(ParameterReductionComparer.Default)
// .Select(parameterSymbol => new { parameterSymbol, isArrayType = parameterSymbol.Type is IArrayTypeSymbol })
// .Select(
// tuple => new
// {
// tuple.isArrayType,
// tuple.parameterSymbol,
// hasParamsKeyWord = tuple.parameterSymbol.ToDisplayString().Contains("params"),
// }
// )
// .Where(tuple => tuple.isArrayType && tuple.hasParamsKeyWord)
// .SelectMany(tuple => tuple.parameterSymbol.Locations))
// {
// productionContext.ReportDiagnostic(Diagnostic.Create(Rsaf0002.Descriptor, location));
// if (!reported)
// {
// reported = true;
// }
// }

return reported;
}


private const string Fixture = nameof(Fixture);

[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
private string DebuggerDisplay => ToString();
}
Loading
Loading