Skip to content

Commit 2de28f4

Browse files
committed
Create & Test Namespace Scraping
1 parent 1159c65 commit 2de28f4

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

src/generators/Silk.NET.SilkTouch.Scraper/ClangScraper.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public IEnumerable<Symbol> ScrapeXML(XmlDocument document)
4343
}
4444

4545
var visitor = new XmlVisitor();
46-
visitor.Visit(bindings);
47-
48-
return visitor.Symbols;
46+
return visitor.Visit(bindings);
4947
}
5048

5149
/// <summary>

src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs

+33-13
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,55 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Collections.Immutable;
67
using System.Linq;
8+
using System.Reflection.Metadata;
79
using System.Xml;
810
using Silk.NET.SilkTouch.Symbols;
911

1012
namespace Silk.NET.SilkTouch.Scraper;
1113

1214
internal sealed class XmlVisitor
1315
{
14-
private List<Symbol> _symbols = new();
15-
16-
public IEnumerable<Symbol> Symbols => _symbols;
17-
18-
public void Visit(XmlNode node)
16+
public IEnumerable<Symbol> Visit(XmlNode node)
1917
{
2018
switch (node)
2119
{
2220
case XmlElement { Name: "bindings" } bindings:
23-
{
24-
foreach (var child in bindings.ChildNodes.Cast<XmlNode>())
25-
{
26-
if (child is null) continue;
27-
Visit(child);
28-
}
29-
break;
30-
}
21+
return VisitBinding(bindings);
22+
case XmlElement { Name: "namespace" } @namespace:
23+
return VisitNamespace(@namespace);
3124
default:
3225
{
3326
throw new NotImplementedException();
3427
}
3528
}
3629
}
30+
31+
private IEnumerable<Symbol> VisitBinding(XmlElement bindings)
32+
{
33+
return bindings.ChildNodes.Cast<XmlNode>().Where(x => x is not null).SelectMany(Visit);
34+
}
35+
36+
private IEnumerable<Symbol> VisitNamespace(XmlElement @namespace)
37+
{
38+
return new[]
39+
{
40+
new NamespaceSymbol
41+
(
42+
new IdentifierSymbol(@namespace.Attributes?["name"]?.Value ?? throw new InvalidOperationException()),
43+
@namespace.ChildNodes.Cast<XmlNode>()
44+
.Select(Visit)
45+
.Select
46+
(
47+
x =>
48+
{
49+
if (x is not TypeSymbol ts) throw new InvalidOperationException();
50+
return ts;
51+
}
52+
)
53+
.ToImmutableArray()
54+
)
55+
};
56+
}
3757
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.Xml;
5+
using Silk.NET.SilkTouch.Symbols;
6+
using Xunit;
7+
8+
namespace Silk.NET.SilkTouch.Scraper.Tests;
9+
10+
public class NamespaceScrapingTests
11+
{
12+
[Fact]
13+
public void NamespaceXMLGeneratesNamespaceSymbol()
14+
{
15+
var doc = new XmlDocument();
16+
doc.LoadXml(@"<bindings>
17+
<namespace name=""" + ClangScraper.LibraryNamespacePlaceholder + @""">
18+
</namespace>
19+
</bindings>
20+
");
21+
22+
var symbols = new ClangScraper().ScrapeXML(doc);
23+
24+
var symbol = Assert.Single(symbols);
25+
var @namespace = Assert.IsType<NamespaceSymbol>(symbol);
26+
Assert.Equal(ClangScraper.LibraryNamespacePlaceholder, @namespace.Identifier.Value);
27+
}
28+
}

tests/Silk.NET.SilkTouch.Scraper.Tests/StructScrapingTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public class StructScrapingTests
1010
[Fact]
1111
public void StructXMLGeneratesStructSymbol()
1212
{
13-
13+
1414
}
1515
}

0 commit comments

Comments
 (0)