11namespace Pure . DI . IntegrationTests ;
22
3- using System ;
4- using System . Linq ;
5- using InterfaceGeneration ;
6- using Microsoft . CodeAnalysis ;
7- using Microsoft . CodeAnalysis . CSharp ;
8-
93public class InterfaceGenerationTests
104{
115 [ Fact ]
12- public void ShouldGenerateAnInterfaceFromAnnotatedClass ( )
6+ public async Task ShouldGenerateAnInterfaceFromAnnotatedClass ( )
137 {
14- var generated = GenerateInterfaceSource ( """
8+ var result = await """
9+ using System;
1510 using Pure.DI;
1611
1712 namespace Demo;
1813
19- public partial interface IService;
14+ public partial interface IService
15+ {
16+ }
2017
2118 [GenerateInterface]
2219 public partial class Service
@@ -32,26 +29,35 @@ public string GetText<T>(string? value)
3229 where T : class
3330 => value ?? string.Empty;
3431 }
35- """ ,
36- "IService" ) ;
37-
38- generated . ShouldContain ( "public partial interface IService" ) ;
39- generated . ShouldContain ( "string Name { get; set; }" ) ;
40- generated . ShouldContain ( "Changed" ) ;
41- generated . ShouldContain ( "GetText<T>" ) ;
42- generated . ShouldNotContain ( "Hidden" ) ;
32+
33+ public class Program
34+ {
35+ public static void Main() { }
36+ }
37+ """ . RunAsync ( new Options ( LanguageVersion . CSharp10 , CheckCompilationErrors : false ) ) ;
38+
39+ result . Errors . Count . ShouldBe ( 0 , result ) ;
40+ result . Warnings . Count . ShouldBe ( 0 , result ) ;
41+
42+ result . GeneratedCode . ShouldContain ( "public partial interface IService" ) ;
43+ result . GeneratedCode . ShouldContain ( "string Name { get; set; }" ) ;
44+ result . GeneratedCode . ShouldContain ( "Changed" ) ;
45+ result . GeneratedCode . ShouldContain ( "GetText<T>" ) ;
46+ result . GeneratedCode . ShouldNotContain ( "Hidden" ) ;
4347 }
4448
4549 [ Fact ]
46- public void ShouldGenerateInterfaceForGenericType ( )
50+ public async Task ShouldGenerateInterfaceForGenericType ( )
4751 {
48- var generated = GenerateInterfaceSource ( """
52+ var result = await """
4953 using System;
5054 using Pure.DI;
5155
5256 namespace Demo;
5357
54- public partial interface IRepository<TItem>;
58+ public partial interface IRepository<TItem>
59+ {
60+ }
5561
5662 [GenerateInterface]
5763 public partial class Repository<TItem>
@@ -63,13 +69,20 @@ public partial class Repository<TItem>
6369
6470 public TItem Create() => new();
6571 }
66- """ ,
67- "IRepository" ) ;
6872
69- generated . ShouldContain ( "public partial interface IRepository<TItem>" ) ;
70- generated . ShouldContain ( "where TItem : class, new()" ) ;
71- generated . ShouldContain ( "TItem? Current { get; set; }" ) ;
72- generated . ShouldContain ( "EventHandler<TItem>" ) ;
73+ public class Program
74+ {
75+ public static void Main() { }
76+ }
77+ """ . RunAsync ( new Options ( LanguageVersion . CSharp10 , CheckCompilationErrors : false ) ) ;
78+
79+ result . Errors . Count . ShouldBe ( 0 , result ) ;
80+ result . Warnings . Count . ShouldBe ( 0 , result ) ;
81+
82+ result . GeneratedCode . ShouldContain ( "public partial interface IRepository<TItem>" ) ;
83+ result . GeneratedCode . ShouldContain ( "where TItem : class, new()" ) ;
84+ result . GeneratedCode . ShouldContain ( "TItem? Current { get; set; }" ) ;
85+ result . GeneratedCode . ShouldContain ( "EventHandler<TItem>" ) ;
7386 }
7487
7588 [ Fact ]
@@ -80,17 +93,24 @@ public async Task ShouldUseGeneratedInterfaceWithPureDi()
8093
8194 namespace Demo;
8295
83- public partial interface IService;
96+ public partial interface IService
97+ {
98+ }
8499
85100 [GenerateInterface]
86101 public partial class Service : IService
87102 {
88103 public string Message => "ok";
89104 }
90105
91- public partial class Consumer(IService service)
106+ public partial class Consumer
92107 {
93- public string Message { get; } = service.Message;
108+ public Consumer(IService service)
109+ {
110+ Message = service.Message;
111+ }
112+
113+ public string Message { get; }
94114 }
95115
96116 partial class Setup
@@ -112,49 +132,11 @@ public static void Main()
112132 }
113133 """ ;
114134
115- var generator = new Generator ( ) ;
116- var interfaceGenerator = generator . InterfaceGenerator ;
117- var generatedInterface = GenerateInterfaceSource ( code , "IService" ) ;
118- var result = await interfaceGenerator . Api
119- . Select ( source => source . SourceText . ToString ( ) )
120- . Append ( code )
121- . Append ( generatedInterface )
122- . RunAsync ( new Options ( LanguageVersion . CSharp12 , CheckCompilationErrors : false ) ) ;
135+ var result = await code . RunAsync ( new Options ( LanguageVersion . CSharp10 , CheckCompilationErrors : false ) ) ;
123136
124137 result . Errors . Count . ShouldBe ( 0 , result ) ;
125138 result . Warnings . Count . ShouldBe ( 0 , result ) ;
126139 result . Success . ShouldBeTrue ( result ) ;
127140 result . StdOut . ShouldContain ( "ok" ) ;
128141 }
129-
130- private static string GenerateInterfaceSource ( string code , string interfaceName )
131- {
132- var syntaxTree = CSharpSyntaxTree . ParseText ( code ) ;
133- var references = AppDomain
134- . CurrentDomain
135- . GetAssemblies ( )
136- . Where ( assembly => ! assembly . IsDynamic && ! string . IsNullOrWhiteSpace ( assembly . Location ) )
137- . Select ( assembly => MetadataReference . CreateFromFile ( assembly . Location ) )
138- . Cast < MetadataReference > ( ) ;
139-
140- var compilation = CSharpCompilation . Create (
141- "InterfaceGenerationTests" ,
142- [ syntaxTree ] ,
143- references ,
144- new ( OutputKind . DynamicallyLinkedLibrary ) ) ;
145-
146- var sourceGenerator = new SourceGenerator ( ) ;
147- CSharpGeneratorDriver . Create ( sourceGenerator ) . RunGeneratorsAndUpdateCompilation (
148- compilation ,
149- out var outputCompilation ,
150- out var diagnostics ) ;
151-
152- diagnostics . Where ( diagnostic => diagnostic . Severity == DiagnosticSeverity . Error ) . ShouldBeEmpty ( ) ;
153-
154- return outputCompilation . SyntaxTrees
155- . Select ( tree => tree . ToString ( ) )
156- . FirstOrDefault ( text => text . Contains ( "[global::System.CodeDom.Compiler.GeneratedCode(\" Pure.DI\" " ) && text . Contains ( $ "partial interface { interfaceName } ") )
157- ?? throw new InvalidOperationException ( string . Join ( Environment . NewLine + "---" + Environment . NewLine ,
158- outputCompilation . SyntaxTrees . Select ( tree => tree . ToString ( ) ) ) ) ;
159- }
160142}
0 commit comments