Skip to content
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

Il structure testing #517

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/Draco.Compiler.Tests/CodeGeneration/MsilStructureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Reflection;
using Draco.Compiler.Internal;

namespace Draco.Compiler.Tests.CodeGeneration;

public sealed class MsilStructureTests
{
private readonly struct RelayDisposable(Action dispose) : IDisposable
{
public void Dispose() => dispose();
}

private const BindingFlags AllBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

private string? CurrentNamespace => this.namespaceStack.Count == 0
? null
: string.Join(".", this.namespaceStack.Reverse());
private Type CurrentType => this.typeStack.Peek();

private Assembly assembly = null!;
private readonly Stack<Type> typeStack = new();
private readonly Stack<string> namespaceStack = new();

private void Compile(string source, string? rootModulePath = null)
{
this.assembly = TestUtilities.CompileToAssembly(
sourceCode: source,
rootModulePath: rootModulePath);
}

private RelayDisposable Ns(string name)
{
this.namespaceStack.Push(name);
return new RelayDisposable(() => this.namespaceStack.Pop());
}

private RelayDisposable C(string name, Func<Type, bool> predicate)
{
var type = this.typeStack.Count == 0
// Look up in root assembly
? this.assembly.GetType(name)
// Look up in current type
: this.CurrentType.GetNestedType(name, AllBindingFlags);

Assert.NotNull(type);
Assert.Equal(this.CurrentNamespace, type.Namespace);
Assert.True(predicate(type));
this.typeStack.Push(type);

return new RelayDisposable(() => this.typeStack.Pop());
}

private RelayDisposable StaticC(string name, Func<Type, bool> predicate) =>
this.C(name, t => t.IsAbstract && t.IsSealed && predicate(t));

private void M(string name, Func<MethodInfo, bool> predicate)
{
var method = this.CurrentType.GetMethod(name, AllBindingFlags);
Assert.NotNull(method);
Assert.True(predicate(method));
}

[Fact]
public void HelloWorld()
{
this.Compile("""
import System.Console;

func main() {
WriteLine("Hello, World!");
}
""");

using (this.StaticC(CompilerConstants.DefaultModuleName, t => !t.IsPublic))
{
this.M("main", m => !m.IsPublic && m.IsStatic);
}
}

[Fact]
public void HelloWorldInModule()
{
this.Compile("""
import System.Console;

func main() {
WriteLine("Hello, World!");
}
""",
rootModulePath: "foo/bar");

using (this.Ns("foo"))
{
using (this.StaticC("bar", t => !t.IsPublic))
{
using (this.StaticC(CompilerConstants.DefaultModuleName, t => !t.IsPublic))
{
this.M("main", m => !m.IsPublic && m.IsStatic);
}
}
}
}
}

Unchanged files with check annotations Beta

/// </summary>
internal sealed class DebugAdapterLifecycle(
IDebugAdapter adapter,
IJsonRpcConnection connection) : IDebugAdapterLifecycle

Check warning on line 17 in src/Draco.Dap/Adapter/DebugAdapterLifecycle.cs

GitHub Actions / tests-ubuntu-latest

Parameter 'connection' is unread.

Check warning on line 17 in src/Draco.Dap/Adapter/DebugAdapterLifecycle.cs

GitHub Actions / tests-windows-latest

Parameter 'connection' is unread.

Check warning on line 17 in src/Draco.Dap/Adapter/DebugAdapterLifecycle.cs

GitHub Actions / tests-macOS-latest

Parameter 'connection' is unread.

Check warning on line 17 in src/Draco.Dap/Adapter/DebugAdapterLifecycle.cs

GitHub Actions / tests-ubuntu-latest

Parameter 'connection' is unread.

Check warning on line 17 in src/Draco.Dap/Adapter/DebugAdapterLifecycle.cs

GitHub Actions / tests-windows-latest

Parameter 'connection' is unread.

Check warning on line 17 in src/Draco.Dap/Adapter/DebugAdapterLifecycle.cs

GitHub Actions / tests-macOS-latest

Parameter 'connection' is unread.
{
public async Task<Model.Capabilities> InitializeAsync(InitializeRequestArguments args)
{