Skip to content

Commit 922f9ed

Browse files
committed
Add support for collection initializers
1 parent 2ac1033 commit 922f9ed

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

SourceGenerator/SourceGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<PropertyGroup>
1313
<Description>Source Generator for adding missing methods and classes inside tests to allow compilation. Assists on creating code exercises.</Description>
14-
<Version>1.1.2</Version>
14+
<Version>1.1.3</Version>
1515
</PropertyGroup>
1616

1717
<ItemGroup>

SourceGenerator/TestGenerator.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,28 @@ private void ProcessStatement(SourceGeneratorContext context, ref Compilation co
131131

132132
private void ProcessObjectCreation(SourceGeneratorContext context, ref Compilation compilation, NamespaceDeclarationSyntax @namespace, ClassDeclarationSyntax @class, ObjectCreationExpressionSyntax objectCreation)
133133
{
134+
if (!(objectCreation.Initializer is null))
135+
{
136+
foreach (ExpressionSyntax initializerExpression in objectCreation.Initializer.Expressions)
137+
{
138+
if (initializerExpression is ObjectCreationExpressionSyntax initializerObjectCreation)
139+
{
140+
this.GenerateEmptyClass(context, ref compilation, @namespace, initializerObjectCreation.Type.ToString());
141+
this.ProcessObjectCreation(context, ref compilation, @namespace, @class, initializerObjectCreation);
142+
}
143+
}
144+
}
145+
134146
//Ignore constructors without arguments
135-
if (objectCreation.ArgumentList?.Arguments.Count <= 0)
147+
if (objectCreation.ArgumentList is null || objectCreation.ArgumentList.Arguments.Count <= 0)
136148
{
137149
return;
138150
}
139151

140152
SemanticModel methodModel = compilation.GetSemanticModel(objectCreation.SyntaxTree);
141153
SymbolInfo targetSymbol = methodModel.GetSymbolInfo(objectCreation.Type);
142154

143-
this.ProcessArguments(context, ref compilation, @namespace, @class, objectCreation.ArgumentList!.Arguments);
155+
this.ProcessArguments(context, ref compilation, @namespace, @class, objectCreation.ArgumentList.Arguments);
144156

145157
//This is used for generating constructors only (for now, at least), type should exist already
146158
if (targetSymbol.Symbol is null)
@@ -390,6 +402,10 @@ private void ProcessInvocation(SourceGeneratorContext context, ref Compilation c
390402
break;
391403
default:
392404
{
405+
if (targetIdentifier.ToString() == "Test")
406+
{
407+
return default;
408+
}
393409
//Ehh... Lets assume its a missing static class? Ehheheh...
394410
ITypeSymbol symbol = this.GenerateEmptyClass(context, ref compilation, @namespace, targetIdentifier.ToString());
395411

Tests/Instance/InstanceMethodTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,17 @@ public void GenerateMissingInstantiationArguments()
113113
MissingClass3 missingClass = new MissingClass3(MissingClass3Static.MissingArgument);
114114
});
115115
}
116+
117+
[Fact]
118+
public void GenerateCollectionInitializerObjects()
119+
{
120+
Assert.Throws<NotImplementedException>(() =>
121+
{
122+
List<object> list = new List<object>
123+
{
124+
new MissingCollectionInitializerClass("First")
125+
};
126+
});
127+
}
116128
}
117129
}

0 commit comments

Comments
 (0)