Skip to content

Create & Test Namespace Symbol #912

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

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
13 changes: 13 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Symbols/NamespaceSymbol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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;

namespace Silk.NET.SilkTouch.Symbols;

/// <summary>
/// A <see cref="Symbol"/> representing a <c>namespace</c>
/// </summary>
public sealed record NamespaceSymbol(IdentifierSymbol Identifier, ImmutableArray<TypeSymbol> Types) : Symbol
{
}
15 changes: 15 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Symbols/SymbolVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public virtual Symbol Visit(Symbol symbol)
{
if (symbol is TypeSymbol ts) return VisitType(ts);
if (symbol is MemberSymbol ms) return VisitMember(ms);
if (symbol is NamespaceSymbol ns) return VisitNamespace(ns);

if (symbol is IdentifierSymbol @is) return VisitIdentifier(@is);

Expand Down Expand Up @@ -97,6 +98,20 @@ protected virtual IdentifierSymbol VisitIdentifier(IdentifierSymbol identifierSy
return identifierSymbol;
}

/// <summary>
/// Visit a <see cref="NamespaceSymbol"/>.
/// </summary>
/// <param name="namespaceSymbol">The Namespace to Visit.</param>
/// <returns>The rewritten symbol</returns>
protected virtual NamespaceSymbol VisitNamespace(NamespaceSymbol namespaceSymbol)
{
return namespaceSymbol with
{
Identifier = VisitIdentifier(namespaceSymbol.Identifier),
Types = namespaceSymbol.Types.Select(VisitType).ToImmutableArray()
};
}

private static T ThrowUnknownSymbol<T>(Symbol symbol)
{
throw new NotImplementedException($"Unknown symbol of type {symbol.GetType().FullName} subclass of {typeof(T).Name}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 Moq;
using Moq.Protected;
using Xunit;

namespace Silk.NET.SilkTouch.Symbols.Tests.SymbolVisitorTests;

public class NamespaceTests
{
[Fact]
public void NamespaceIdentifierIsVisited()
{
var symbol = new NamespaceSymbol(new IdentifierSymbol(""), ImmutableArray<TypeSymbol>.Empty);

var visitor = new Mock<SymbolVisitor>
{
CallBase = true
};

visitor.Object.Visit(symbol);

visitor.Protected()
.Verify<IdentifierSymbol>("VisitIdentifier", Times.Once(), ItExpr.IsAny<IdentifierSymbol>());
}

[Fact]
public void NamespaceMemberIsVisited()
{
var symbol = new NamespaceSymbol(new IdentifierSymbol(""), new []
{
(TypeSymbol)new StructSymbol(new IdentifierSymbol(""), StructLayout.Empty)
}.ToImmutableArray());

var visitor = new Mock<SymbolVisitor>
{
CallBase = true
};

visitor.Object.Visit(symbol);

visitor.Protected()
.Verify<StructSymbol>("VisitStruct", Times.Once(), ItExpr.IsAny<StructSymbol>());
}
}