Skip to content

Complete tests #1014

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 12 commits into from
Aug 9, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ Parent Symbols (Unlisted, abstract):
| TypeReference | [here](../type-references.md) |
| TypeSymbol | |

| Name | Symbol Layer File | Symbol Layer Tests | Emitter Tests |
| ----------------------- | ----------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| ExternalTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/ExternalTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/ExternalTypeReferenceTests.cs) | TODO!!! |
| FieldSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/FieldSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/FieldTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterFieldTests.cs) |
| IdentifierSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/IdentifierSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/IdentifierTests.cs) | TODO!!! |
| InternalTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/InternalTypeReference.cs) | TODO!!! | TODO!!! |
| NamespaceSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/NamespaceSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/NamespaceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceTests.cs) |
| PointerTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/PointerTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/PointerTypeReferenceTests.cs) | TODO!!! |
| StructSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/StructSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/StructTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterStructTests.cs) |
| UnresolvedTypeReference | [here](src/generators/Silk.NET.SilkTouch.Symbols/UnresolvedTypeReference.cs) | [here](tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/UnresolvedTypeReferenceTests.cs) | - |
| Name | Symbol Layer File | Symbol Layer Tests | Emitter Tests |
| ----------------------- | ----------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| ExternalTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/ExternalTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/ExternalTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/ExternalTypeReferenceTests.cs) |
| FieldSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/FieldSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/FieldTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterFieldTests.cs) |
| IdentifierSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/IdentifierSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/IdentifierTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/IdentifierSymbolTests.cs) |
| InternalTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/InternalTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/InternalTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/InternalTypeReferenceTests.cs) |
| NamespaceSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/NamespaceSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/NamespaceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterNamespaceTests.cs) |
| PointerTypeReference | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/PointerTypeReference.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/PointerTypeReferenceTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/PointerTypeReferenceTests.cs) |
| StructSymbol | [here](../../../../../src/generators/Silk.NET.SilkTouch.Symbols/StructSymbol.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/StructTests.cs) | [here](../../../../../tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterStructTests.cs) |
| UnresolvedTypeReference | [here](src/generators/Silk.NET.SilkTouch.Symbols/UnresolvedTypeReference.cs) | [here](tests/Silk.NET.SilkTouch.Symbols.Tests/SymbolVisitorTests/UnresolvedTypeReferenceTests.cs) | - |

## How to create a symbol

Expand Down
60 changes: 60 additions & 0 deletions src/generators/Silk.NET.SilkTouch.Emitter/CSharpEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,66 @@ protected override NamespaceSymbol VisitNamespace(NamespaceSymbol namespaceSymbo
return namespaceSymbol;
}

protected override InternalTypeReference VisitInternalTypeReference(InternalTypeReference typeReference)
{
AssertClearState();

if (!TypeStore.TryResolve(typeReference.ReferencedTypeId, out var type))
{
throw new NotImplementedException("Cannot handle unresolvable type ID");
}

// TODO: Fully resolve types
VisitIdentifier(type!.Identifier);

return typeReference;
}

protected override PointerTypeReference VisitPointerTypeReference(PointerTypeReference pointerTypeReference)
{
AssertClearState();

VisitTypeReference(pointerTypeReference.Underlying);
if (_syntaxToken is not {} innerToken)
throw new InvalidOperationException("Type Reference was not visited correctly");
ClearState();

_syntaxToken = Identifier(innerToken.Text + "*");
_syntax = IdentifierName(_syntaxToken.Value);

return pointerTypeReference;
}

protected override ExternalTypeReference VisitExternalTypeReference(ExternalTypeReference typeReference)
{
AssertClearState();

if (typeReference.Namespace is null)
{
// if namespace is null, the reference is just equivalent to the type identifier
VisitIdentifier(typeReference.TypeIdentifier);
}
else
{
VisitIdentifier(typeReference.Namespace);
if (_syntaxToken is not {} @namespace)
throw new InvalidOperationException("Namespace Identifier was not visited correctly");
ClearState();

VisitIdentifier(typeReference.TypeIdentifier);

if (_syntaxToken is not {} typeIdentifier)
throw new InvalidOperationException("External Type Identifier was not visited correctly");
ClearState();

_syntaxToken = Identifier(@namespace.Text + "." + typeIdentifier.Text);
_syntax = IdentifierName(_syntaxToken.Value);
return typeReference;
}

return typeReference;
}

protected override IdentifierSymbol VisitIdentifier(IdentifierSymbol identifierSymbol)
{
AssertClearState();
Expand Down
4 changes: 2 additions & 2 deletions tests/Silk.NET.SilkTouch.Emitter.Tests/EmitterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ protected CSharpEmitter CreateEmitter()
return new CSharpEmitter();
}

protected CSharpSyntaxNode Transform(Symbol symbol)
protected CSharpSyntaxNode Transform(Symbol symbol, TypeStore? typeStore = null)
{
return CreateEmitter().Transform(symbol, new TypeStore());
return CreateEmitter().Transform(symbol, typeStore ?? new TypeStore());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Security.Cryptography.X509Certificates;
using Silk.NET.SilkTouch.Symbols;
using Xunit;

namespace Silk.NET.SilkTouch.Emitter.Tests;

public class ExternalTypeReferenceTests : EmitterTest
{
[Fact, Trait("Category", "Symbols"),
Trait("Target Language", "C#")]
public void StringTestNoNamespace()
{
var symbol = new ExternalTypeReference(null, new IdentifierSymbol("ETR1"));

var transformed = Transform(symbol);

Assert.Equal("ETR1", transformed.ToFullString());
}

[Fact, Trait("Category", "Symbols"),
Trait("Target Language", "C#")]
public void StringTestWithNamespace()
{
var symbol = new ExternalTypeReference(new IdentifierSymbol("Namespace"), new IdentifierSymbol("ETR1"));

var transformed = Transform(symbol);

Assert.Equal("Namespace.ETR1", transformed.ToFullString());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using Silk.NET.SilkTouch.Symbols;
using Xunit;

namespace Silk.NET.SilkTouch.Emitter.Tests;

public sealed class IdentifierTests : EmitterTest
public class IdentifierSymbolTests : EmitterTest
{
[Theory, Trait("Category", "Symbols"),
Trait("Target Language", "C#")]
[InlineData("a", "a")]
[InlineData("int", "int")]
[InlineData("using", "using")]
[InlineData("veryLongIdentifierName", "veryLongIdentifierName")]
[InlineData("this is invalid # /**/", "this is invalid # /**/")]
public void StringTest(string identifierText, string csharp)
{
var symbol = new IdentifierSymbol(identifierText);

var transformed = Transform(symbol);

Assert.Equal(csharp, transformed.ToFullString());
}

[Fact,
Trait("Category", "Emitter"),
Trait("Target Language", "C#")]
Expand All @@ -29,14 +46,4 @@ public void IdentifierHasNoTrailingTrivia()
Assert.Empty(node.GetTrailingTrivia());
Assert.False(node.HasTrailingTrivia);
}

[Fact,
Trait("Category", "Emitter"),
Trait("Target Language", "C#")]
public void IdentifierIntegration()
{
var node = Transform(new IdentifierSymbol("Test"));

Assert.Equal("Test", node.ToFullString());
}
}
Original file line number Diff line number Diff line change
@@ -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.Collections.Immutable;
using System.ComponentModel;
using Silk.NET.SilkTouch.Symbols;
using Xunit;

namespace Silk.NET.SilkTouch.Emitter.Tests;

public class InternalTypeReferenceTests : EmitterTest
{
[Fact, Trait("Category", "Type Resolution"), Trait("Category", "Symbols")]
public void StringTest()
{
var typeStore = new TypeStore();

var typeId = TypeId.CreateNew();
var actualType = new StructSymbol
(typeId, new IdentifierSymbol("Identifier"), ImmutableArray<FieldSymbol>.Empty);
typeStore.Store(actualType);
var symbol = new InternalTypeReference(typeId);

var transformed = Transform(symbol, typeStore);

Assert.Equal("Identifier", transformed.ToFullString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Silk.NET.SilkTouch.Symbols;
using Xunit;

namespace Silk.NET.SilkTouch.Emitter.Tests;

public class PointerTypeReferenceTests : EmitterTest
{
[Fact, Trait("Category", "Symbols")]
public void StringTest()
{
var symbol = new PointerTypeReference(new ExternalTypeReference(null, new IdentifierSymbol("ETR1")));

var transformed = Transform(symbol);

Assert.Equal("ETR1*", transformed.ToFullString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using Moq;
using Moq.Protected;
using Xunit;

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

public class InternalTypeReferenceTests
{
[Fact, Trait("Category", "Symbols")]
public void VisitedAsSelf()
{
var symbol = new InternalTypeReference(TypeId.CreateNew());

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

visitor.Object.Visit(symbol);

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

[Fact, Trait("Category", "Symbols")]
public void VisitedAsParent()
{
var symbol = new InternalTypeReference(TypeId.CreateNew());

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

visitor.Object.Visit(symbol);

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


[Fact, Trait("Category", "Symbols")]
public void TypeIdIsVisited()
{
var id = TypeId.CreateNew();
var symbol = new InternalTypeReference(id);

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

visitor.Object.Visit(symbol);

visitor.Protected().Verify<TypeId>("VisitTypeId", Times.Once(), ItExpr.Is<TypeId>(x => x == id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Silk.NET.SilkTouch.Symbols.Tests.SymbolVisitorTests;

public class PointerTypeReferenceTests
{
[Fact, Trait("Category", "Type Resolution")]
[Fact, Trait("Category", "Symbols")]
public void RefIsVisitedAsSelf()
{
var symbol = new PointerTypeReference(new InternalTypeReference(TypeId.CreateNew()));
Expand All @@ -23,7 +23,7 @@ public void RefIsVisitedAsSelf()
("VisitPointerTypeReference", Times.Once(), ItExpr.IsAny<PointerTypeReference>());
}

[Fact, Trait("Category", "Type Resolution")]
[Fact, Trait("Category", "Symbols")]
public void RefIsVisitedAsRef()
{
var symbol = new PointerTypeReference(new InternalTypeReference(TypeId.CreateNew()));
Expand All @@ -37,7 +37,7 @@ public void RefIsVisitedAsRef()
("VisitTypeReference", Times.Once(), ItExpr.Is<TypeReference>(x => ReferenceEquals(x, symbol)));
}

[Fact, Trait("Category", "Type Resolution")]
[Fact, Trait("Category", "Symbols")]
public void RefUnderlyingIsVisitedAsRef()
{
var underlying = new InternalTypeReference(TypeId.CreateNew());
Expand Down