Skip to content

Commit 7cf0a74

Browse files
authored
Create & Test Namespace Emission (#914)
1 parent 3a7d89b commit 7cf0a74

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/generators/Silk.NET.SilkTouch.Emitter/CSharpEmitter.cs

+21
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ protected override FieldSymbol VisitField(FieldSymbol fieldSymbol)
176176
return fieldSymbol;
177177
}
178178

179+
protected override NamespaceSymbol VisitNamespace(NamespaceSymbol namespaceSymbol)
180+
{
181+
AssertClearState();
182+
183+
VisitIdentifier(namespaceSymbol.Identifier);
184+
if (_syntax is not IdentifierNameSyntax namespaceIdentifierSyntax)
185+
throw new InvalidOperationException("Namespace Identifier was not visited correctly");
186+
ClearState();
187+
188+
// TODO visit members
189+
190+
_syntax = NamespaceDeclaration
191+
(
192+
List<AttributeListSyntax>(), TokenList(), namespaceIdentifierSyntax.WithTrailingTrivia(LineFeed),
193+
List<ExternAliasDirectiveSyntax>(), List<UsingDirectiveSyntax>(), List<MemberDeclarationSyntax>()
194+
)
195+
.WithNamespaceKeyword(Token(SyntaxTriviaList.Empty, SyntaxKind.NamespaceKeyword, TriviaList(Space)))
196+
.WithOpenBraceToken(Token(SyntaxTriviaList.Empty, SyntaxKind.OpenBraceToken, TriviaList(LineFeed)));
197+
return namespaceSymbol;
198+
}
199+
179200
protected override IdentifierSymbol VisitIdentifier(IdentifierSymbol identifierSymbol)
180201
{
181202
AssertClearState();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Immutable;
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CSharp;
7+
using Microsoft.CodeAnalysis.CSharp.Syntax;
8+
using Silk.NET.SilkTouch.Symbols;
9+
using Xunit;
10+
11+
namespace Silk.NET.SilkTouch.Emitter.Tests;
12+
13+
public class EmitterNamespaceTests : EmitterTest
14+
{
15+
[Fact]
16+
public void NamespaceIntegration()
17+
{
18+
var syntax = Transform(new NamespaceSymbol(new IdentifierSymbol("Test"), ImmutableArray<TypeSymbol>.Empty));
19+
20+
var result = syntax.ToFullString();
21+
Assert.Equal("namespace Test\n{\n}", result);
22+
}
23+
24+
[Fact]
25+
public void NamespaceHasCorrectIdentifier()
26+
{
27+
var syntax = Transform
28+
(new NamespaceSymbol(new IdentifierSymbol("Test"), ImmutableArray<TypeSymbol>.Empty)) as
29+
NamespaceDeclarationSyntax;
30+
31+
Assert.NotNull(syntax);
32+
33+
Assert.Equal("Test", Assert.IsType<IdentifierNameSyntax>(syntax!.Name).Identifier.Text);
34+
}
35+
}

0 commit comments

Comments
 (0)