Skip to content

Commit

Permalink
Create & Test Namespace Symbol (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
HurricanKai authored May 7, 2022
1 parent 5f5d836 commit 5e609aa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
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>());
}
}

0 comments on commit 5e609aa

Please sign in to comment.