Skip to content

Commit 44f13e2

Browse files
author
Colin Wilmans
committed
*feature* repeat type arguments of generics
1 parent 4de922b commit 44f13e2

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/SourceDepend.Tests/SourceDependTests.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,98 @@ public ExampleService(ConsoleApp.IAnotherService anotherService)
262262
//Assert
263263
Assert.Equal(expec, actual.Item2);
264264
}
265+
266+
[Fact]
267+
public void SourceDepend_WithGenerics_ShouldRepeatTheseGenerics()
268+
{
269+
//Arrange
270+
var source = """
271+
public partial class ExampleService<TKey,TValue>
272+
{
273+
274+
[Dependency]
275+
private readonly IAnotherService anotherService;
276+
}
277+
278+
public interface IAnotherService {}
279+
""";
280+
281+
var expec = """
282+
// <auto-generated/>
283+
#pragma warning disable
284+
#nullable enable
285+
/// <inheritdoc/>
286+
public partial class ExampleService<TKey,TValue>
287+
{
288+
public ExampleService(IAnotherService anotherService)
289+
{
290+
291+
#if NET6_0_OR_GREATER
292+
ArgumentNullException.ThrowIfNull(anotherService);
293+
#endif
294+
PreConstruct();
295+
296+
this.anotherService = anotherService;
297+
298+
PostConstruct();
299+
}
300+
301+
partial void PreConstruct();
302+
partial void PostConstruct();
303+
}
304+
305+
""";
306+
//Act
307+
var actual = SourceCompiler.GetGeneratedOutput(source, _output);
308+
309+
//Assert
310+
Assert.Equal(expec, actual.Item2);
311+
}
312+
313+
[Fact]
314+
public void SourceDepend_WithGeneric_ShouldRepeatTheseGenerics()
315+
{
316+
//Arrange
317+
var source = """
318+
public partial class ExampleService<T>
319+
{
320+
321+
[Dependency]
322+
private readonly IAnotherService anotherService;
323+
}
324+
325+
public interface IAnotherService {}
326+
""";
327+
328+
var expec = """
329+
// <auto-generated/>
330+
#pragma warning disable
331+
#nullable enable
332+
/// <inheritdoc/>
333+
public partial class ExampleService<T>
334+
{
335+
public ExampleService(IAnotherService anotherService)
336+
{
337+
338+
#if NET6_0_OR_GREATER
339+
ArgumentNullException.ThrowIfNull(anotherService);
340+
#endif
341+
PreConstruct();
342+
343+
this.anotherService = anotherService;
344+
345+
PostConstruct();
346+
}
347+
348+
partial void PreConstruct();
349+
partial void PostConstruct();
350+
}
351+
352+
""";
353+
//Act
354+
var actual = SourceCompiler.GetGeneratedOutput(source, _output);
355+
356+
//Assert
357+
Assert.Equal(expec, actual.Item2);
358+
}
265359
}

src/SourceDepend/CodeGenerators/DependencyClassCodeGenerator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ internal static string Generate(INamedTypeSymbol classSymbol, List<ISymbol> symb
2020
var abstractString = classSymbol.IsAbstract ? "abstract " : "";
2121
var accessibilityKeyword = SyntaxFacts.GetText(classSymbol.DeclaredAccessibility);
2222

23+
string typeArgumentsString = classSymbol.IsGenericType ?
24+
$"<{string.Join(",", classSymbol.TypeArguments.Select(t => t.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)))}>" :
25+
"";
26+
2327
var spaces = hasNameSpace ? " " : "";
2428

2529
// begin building the generated source
@@ -35,7 +39,7 @@ internal static string Generate(INamedTypeSymbol classSymbol, List<ISymbol> symb
3539
}
3640

3741
source.AppendLine($"{spaces}/// <inheritdoc/>");
38-
source.AppendLine($"{spaces}{accessibilityKeyword} {abstractString}{sealedString}partial class {classSymbol.Name}");
42+
source.AppendLine($"{spaces}{accessibilityKeyword} {abstractString}{sealedString}partial class {classSymbol.Name}{typeArgumentsString}");
3943
source.AppendLine($"{spaces}{{");
4044

4145
_ = source.Append($"{spaces} public {classSymbol.Name}(");

0 commit comments

Comments
 (0)