Skip to content

Commit

Permalink
Create & Test Namespace Emission (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
HurricanKai authored May 7, 2022
1 parent 3a7d89b commit 7cf0a74
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Emitter/CSharpEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,27 @@ protected override FieldSymbol VisitField(FieldSymbol fieldSymbol)
return fieldSymbol;
}

protected override NamespaceSymbol VisitNamespace(NamespaceSymbol namespaceSymbol)
{
AssertClearState();

VisitIdentifier(namespaceSymbol.Identifier);
if (_syntax is not IdentifierNameSyntax namespaceIdentifierSyntax)
throw new InvalidOperationException("Namespace Identifier was not visited correctly");
ClearState();

// TODO visit members

_syntax = NamespaceDeclaration
(
List<AttributeListSyntax>(), TokenList(), namespaceIdentifierSyntax.WithTrailingTrivia(LineFeed),
List<ExternAliasDirectiveSyntax>(), List<UsingDirectiveSyntax>(), List<MemberDeclarationSyntax>()
)
.WithNamespaceKeyword(Token(SyntaxTriviaList.Empty, SyntaxKind.NamespaceKeyword, TriviaList(Space)))
.WithOpenBraceToken(Token(SyntaxTriviaList.Empty, SyntaxKind.OpenBraceToken, TriviaList(LineFeed)));
return namespaceSymbol;
}

protected override IdentifierSymbol VisitIdentifier(IdentifierSymbol identifierSymbol)
{
AssertClearState();
Expand Down
35 changes: 35 additions & 0 deletions tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Silk.NET.SilkTouch.Symbols;
using Xunit;

namespace Silk.NET.SilkTouch.Emitter.Tests;

public class EmitterNamespaceTests : EmitterTest
{
[Fact]
public void NamespaceIntegration()
{
var syntax = Transform(new NamespaceSymbol(new IdentifierSymbol("Test"), ImmutableArray<TypeSymbol>.Empty));

var result = syntax.ToFullString();
Assert.Equal("namespace Test\n{\n}", result);
}

[Fact]
public void NamespaceHasCorrectIdentifier()
{
var syntax = Transform
(new NamespaceSymbol(new IdentifierSymbol("Test"), ImmutableArray<TypeSymbol>.Empty)) as
NamespaceDeclarationSyntax;

Assert.NotNull(syntax);

Assert.Equal("Test", Assert.IsType<IdentifierNameSyntax>(syntax!.Name).Identifier.Text);
}
}

0 comments on commit 7cf0a74

Please sign in to comment.