Skip to content

Commit 5e609aa

Browse files
authored
Create & Test Namespace Symbol (#912)
1 parent 5f5d836 commit 5e609aa

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
6+
namespace Silk.NET.SilkTouch.Symbols;
7+
8+
/// <summary>
9+
/// A <see cref="Symbol"/> representing a <c>namespace</c>
10+
/// </summary>
11+
public sealed record NamespaceSymbol(IdentifierSymbol Identifier, ImmutableArray<TypeSymbol> Types) : Symbol
12+
{
13+
}

src/generators/Silk.NET.SilkTouch.Symbols/SymbolVisitor.cs

+15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public virtual Symbol Visit(Symbol symbol)
1919
{
2020
if (symbol is TypeSymbol ts) return VisitType(ts);
2121
if (symbol is MemberSymbol ms) return VisitMember(ms);
22+
if (symbol is NamespaceSymbol ns) return VisitNamespace(ns);
2223

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

@@ -97,6 +98,20 @@ protected virtual IdentifierSymbol VisitIdentifier(IdentifierSymbol identifierSy
9798
return identifierSymbol;
9899
}
99100

101+
/// <summary>
102+
/// Visit a <see cref="NamespaceSymbol"/>.
103+
/// </summary>
104+
/// <param name="namespaceSymbol">The Namespace to Visit.</param>
105+
/// <returns>The rewritten symbol</returns>
106+
protected virtual NamespaceSymbol VisitNamespace(NamespaceSymbol namespaceSymbol)
107+
{
108+
return namespaceSymbol with
109+
{
110+
Identifier = VisitIdentifier(namespaceSymbol.Identifier),
111+
Types = namespaceSymbol.Types.Select(VisitType).ToImmutableArray()
112+
};
113+
}
114+
100115
private static T ThrowUnknownSymbol<T>(Symbol symbol)
101116
{
102117
throw new NotImplementedException($"Unknown symbol of type {symbol.GetType().FullName} subclass of {typeof(T).Name}");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 Moq;
6+
using Moq.Protected;
7+
using Xunit;
8+
9+
namespace Silk.NET.SilkTouch.Symbols.Tests.SymbolVisitorTests;
10+
11+
public class NamespaceTests
12+
{
13+
[Fact]
14+
public void NamespaceIdentifierIsVisited()
15+
{
16+
var symbol = new NamespaceSymbol(new IdentifierSymbol(""), ImmutableArray<TypeSymbol>.Empty);
17+
18+
var visitor = new Mock<SymbolVisitor>
19+
{
20+
CallBase = true
21+
};
22+
23+
visitor.Object.Visit(symbol);
24+
25+
visitor.Protected()
26+
.Verify<IdentifierSymbol>("VisitIdentifier", Times.Once(), ItExpr.IsAny<IdentifierSymbol>());
27+
}
28+
29+
[Fact]
30+
public void NamespaceMemberIsVisited()
31+
{
32+
var symbol = new NamespaceSymbol(new IdentifierSymbol(""), new []
33+
{
34+
(TypeSymbol)new StructSymbol(new IdentifierSymbol(""), StructLayout.Empty)
35+
}.ToImmutableArray());
36+
37+
var visitor = new Mock<SymbolVisitor>
38+
{
39+
CallBase = true
40+
};
41+
42+
visitor.Object.Visit(symbol);
43+
44+
visitor.Protected()
45+
.Verify<StructSymbol>("VisitStruct", Times.Once(), ItExpr.IsAny<StructSymbol>());
46+
}
47+
}

0 commit comments

Comments
 (0)