Skip to content

Commit 036ef7b

Browse files
author
Nikolay Pianikov
committed
Add global namespace handling in interface generation and improve test coverage
1 parent e35df65 commit 036ef7b

5 files changed

Lines changed: 47 additions & 2 deletions

File tree

build/Core/Targets/TestExamplesTarget.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public async Task<int> RunAsync(CancellationToken cancellationToken)
3333
File.Copy(Path.Combine(solutionDir, "tests", "Pure.DI.UsageTests", "Test.props"), Path.Combine(appDir, "Directory.Build.props"));
3434
var programFile = Path.Combine(appDir, "Program.cs");
3535
var example = properties["example"];
36+
var failures = 0;
3637
foreach (var (_, groupExamples) in examples)
3738
{
3839
foreach (var vars in groupExamples)
@@ -60,6 +61,7 @@ public async Task<int> RunAsync(CancellationToken cancellationToken)
6061

6162
if (result.ExitCode != 0)
6263
{
64+
failures++;
6365
WriteLine($"Test \"{description}\"", Color.Header);
6466
foreach (var line in code.Split([Environment.NewLine], StringSplitOptions.None))
6567
{
@@ -68,6 +70,11 @@ public async Task<int> RunAsync(CancellationToken cancellationToken)
6870
}
6971
}
7072
}
73+
74+
if (failures > 0)
75+
{
76+
return 1;
77+
}
7178
}
7279
finally
7380
{
@@ -83,4 +90,4 @@ public async Task<int> RunAsync(CancellationToken cancellationToken)
8390
private static bool HasIntegrationTest(Example example) =>
8491
!example.TryGetValue(CreateExamplesTarget.IntegrationTestKey, out var integrationTestStr)
8592
|| bool.TryParse(integrationTestStr, out var integrationTest) && integrationTest;
86-
}
93+
}

src/Pure.DI.Core/InterfaceGeneration/InterfaceBuilder.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public GeneratedInterfacesResult BuildInterfacesFor(
4949
}
5050

5151
const string generateInterfaceAttributeFullName = $"{Names.GlobalNamespacePrefix}{Names.GenerateInterfaceAttributeFullName}";
52-
var defaultNamespaceName = typeSymbol.ContainingNamespace.ToDisplayString();
52+
var defaultNamespaceName = GetNamespaceName(typeSymbol.ContainingNamespace);
5353
var defaultInterfaceName = $"I{classSyntax.Identifier.Text}";
5454

5555
var settingsByInterface = new Dictionary<InterfaceKey, (bool AsInternal, bool HasClassAttribute)>();
@@ -458,4 +458,7 @@ private static string GetInterfaceDisplayName(InterfaceKey key) =>
458458
? key.InterfaceName
459459
: $"{key.NamespaceName}.{key.InterfaceName}";
460460

461+
private static string GetNamespaceName(INamespaceSymbol namespaceSymbol) =>
462+
namespaceSymbol.IsGlobalNamespace ? string.Empty : namespaceSymbol.ToDisplayString();
463+
461464
}

tests/Pure.DI.IntegrationTests/InterfaceGenerationTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,39 @@ public static void Main()
138138
result.StdOut.ShouldContain("ok");
139139
}
140140

141+
[Fact]
142+
public async Task ShouldGenerateInterfaceInGlobalNamespace()
143+
{
144+
var result = await """
145+
using Pure.DI;
146+
147+
public partial interface IService
148+
{
149+
}
150+
151+
[GenerateInterface]
152+
public partial class Service : IService
153+
{
154+
public string Message => "ok";
155+
}
156+
157+
public class Program
158+
{
159+
public static void Main()
160+
{
161+
IService service = new Service();
162+
System.Console.WriteLine(service.Message);
163+
}
164+
}
165+
""".RunAsync(new Options(LanguageVersion.CSharp10, CheckCompilationErrors: false));
166+
167+
result.Errors.Count.ShouldBe(0, result);
168+
result.Success.ShouldBeTrue(result);
169+
result.GeneratedCode.ShouldNotContain("namespace {global_namespace}");
170+
result.GeneratedCode.ShouldContain("public partial interface IService");
171+
result.StdOut.ShouldContain("ok");
172+
}
173+
141174
[Fact]
142175
public async Task ShouldPreserveMethodParameterOrder()
143176
{

tests/Pure.DI.UsageTests/Interfaces/GenerateInterfaceSelectiveMembersScenario.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
$f=- Generate interface members selectively
88
$f=- Keep class-level generation settings
99
$f=- Exclude explicitly ignored members from all generated interfaces
10+
$r=Shouldly
1011
*/
1112

1213
// ReSharper disable ClassNeverInstantiated.Global

tests/Pure.DI.UsageTests/Interfaces/GenerateMultipleInterfacesScenario.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
$f=- Generate multiple interfaces from one class
88
$f=- Select members per interface using member attributes
99
$f=- Reuse one member in several generated interfaces
10+
$r=Shouldly
1011
*/
1112

1213
// ReSharper disable ClassNeverInstantiated.Global

0 commit comments

Comments
 (0)