From 202461aad1dd44714369bd5fa2af31808664f89c Mon Sep 17 00:00:00 2001 From: Kai Jellinghaus Date: Sun, 8 May 2022 00:42:30 +0200 Subject: [PATCH] Create & Test Namespace Scraping --- .../ClangScraper.cs | 4 +- .../Silk.NET.SilkTouch.Scraper/XmlVisitor.cs | 46 +++++++++++++------ .../NamespaceScrapingTests.cs | 28 +++++++++++ .../StructScrapingTests.cs | 2 +- 4 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 tests/Silk.NET.SilkTouch.Scraper.Tests/NamespaceScrapingTests.cs diff --git a/src/generators/Silk.NET.SilkTouch.Scraper/ClangScraper.cs b/src/generators/Silk.NET.SilkTouch.Scraper/ClangScraper.cs index 19a8d46d46..d4cc68a5fd 100644 --- a/src/generators/Silk.NET.SilkTouch.Scraper/ClangScraper.cs +++ b/src/generators/Silk.NET.SilkTouch.Scraper/ClangScraper.cs @@ -43,9 +43,7 @@ public IEnumerable ScrapeXML(XmlDocument document) } var visitor = new XmlVisitor(); - visitor.Visit(bindings); - - return visitor.Symbols; + return visitor.Visit(bindings); } /// diff --git a/src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs b/src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs index 8223cdf0a6..bddf336a00 100644 --- a/src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs +++ b/src/generators/Silk.NET.SilkTouch.Scraper/XmlVisitor.cs @@ -3,7 +3,9 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; +using System.Reflection.Metadata; using System.Xml; using Silk.NET.SilkTouch.Symbols; @@ -11,27 +13,45 @@ namespace Silk.NET.SilkTouch.Scraper; internal sealed class XmlVisitor { - private List _symbols = new(); - - public IEnumerable Symbols => _symbols; - - public void Visit(XmlNode node) + public IEnumerable Visit(XmlNode node) { switch (node) { case XmlElement { Name: "bindings" } bindings: - { - foreach (var child in bindings.ChildNodes.Cast()) - { - if (child is null) continue; - Visit(child); - } - break; - } + return VisitBinding(bindings); + case XmlElement { Name: "namespace" } @namespace: + return VisitNamespace(@namespace); default: { throw new NotImplementedException(); } } } + + private IEnumerable VisitBinding(XmlElement bindings) + { + return bindings.ChildNodes.Cast().Where(x => x is not null).SelectMany(Visit); + } + + private IEnumerable VisitNamespace(XmlElement @namespace) + { + return new[] + { + new NamespaceSymbol + ( + new IdentifierSymbol(@namespace.Attributes?["name"]?.Value ?? throw new InvalidOperationException()), + @namespace.ChildNodes.Cast() + .Select(Visit) + .Select + ( + x => + { + if (x is not TypeSymbol ts) throw new InvalidOperationException(); + return ts; + } + ) + .ToImmutableArray() + ) + }; + } } diff --git a/tests/Silk.NET.SilkTouch.Scraper.Tests/NamespaceScrapingTests.cs b/tests/Silk.NET.SilkTouch.Scraper.Tests/NamespaceScrapingTests.cs new file mode 100644 index 0000000000..5a43a9c4b3 --- /dev/null +++ b/tests/Silk.NET.SilkTouch.Scraper.Tests/NamespaceScrapingTests.cs @@ -0,0 +1,28 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Xml; +using Silk.NET.SilkTouch.Symbols; +using Xunit; + +namespace Silk.NET.SilkTouch.Scraper.Tests; + +public class NamespaceScrapingTests +{ + [Fact] + public void NamespaceXMLGeneratesNamespaceSymbol() + { + var doc = new XmlDocument(); + doc.LoadXml(@" + + + +"); + + var symbols = new ClangScraper().ScrapeXML(doc); + + var symbol = Assert.Single(symbols); + var @namespace = Assert.IsType(symbol); + Assert.Equal(ClangScraper.LibraryNamespacePlaceholder, @namespace.Identifier.Value); + } +} diff --git a/tests/Silk.NET.SilkTouch.Scraper.Tests/StructScrapingTests.cs b/tests/Silk.NET.SilkTouch.Scraper.Tests/StructScrapingTests.cs index 05ad8c4617..56512c68cf 100644 --- a/tests/Silk.NET.SilkTouch.Scraper.Tests/StructScrapingTests.cs +++ b/tests/Silk.NET.SilkTouch.Scraper.Tests/StructScrapingTests.cs @@ -10,6 +10,6 @@ public class StructScrapingTests [Fact] public void StructXMLGeneratesStructSymbol() { - + } }