Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create & Test Namespace Emission #914

Merged
merged 1 commit into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}