Skip to content

Commit

Permalink
Tests: basic type dumps
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Oct 26, 2024
1 parent 147d5f7 commit c0eeabc
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,6 @@ MigrationBackup/

.idea/

.DS_Store
.DS_Store

*.received.txt
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Contributor Guide
=================

Prerequisites
-------------
.NET SDK 7.0 is required to build and run the project.

Building
--------
To build the project, execute the following shell command:
```console
$ cd src && dotnet build
```

Tests
-----
To run the tests, execute the following shell command:
```console
$ cd src && dotnet test
```

If you made changes and want to update the test data, run the following shell command (PowerShell Core is required):
```console
$ pwsh ./scripts/Approve-TestResults.ps1
```
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ will generate FrameworkList for all passed DLL files with root tag
## Links

* [Reference assembly specs](https://docs.microsoft.com/en-us/dotnet/standard/assembly/reference-assemblies)

## Documentation
- [Contributor Guide][docs.contributing]
- [License (Apache-2.0)][docs.license]

[docs.contributing]: CONTRIBUTING.md
[docs.license]: LICENSE
17 changes: 17 additions & 0 deletions scripts/Approve-TestResults.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
param (
$SolutionRoot = "$PSScriptRoot/.."
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

if (-Not $PSScriptRoot) {
Write-Error "\`$PSScriptRoot variable isn't set. Do you run this code as a script? Please, follow instructions at https://github.com/ForNeVeR/Cesium/blob/main/docs/tests.md"
}
else {
Get-ChildItem -Recurse $SolutionRoot -Filter "*.received.txt" | ForEach-Object {
$receivedTestResult = $_.FullName
$approvedTestResult = $receivedTestResult.Replace('.received.txt', '.verified.txt')
Move-Item -Force -LiteralPath $receivedTestResult $approvedTestResult
}
}
2 changes: 1 addition & 1 deletion src/RefasmerTestAssembly/RefasmerTestAssembly.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;net6.0;net7.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand Down
68 changes: 68 additions & 0 deletions tests/Refasmer.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Text;
using Medallion.Shell;
using Mono.Cecil;

namespace JetBrains.Refasmer.Tests;

public class IntegrationTests
{
[TestCase("RefasmerTestAssembly.PublicClassWithPrivateFields")]
[TestCase("RefasmerTestAssembly.PublicStructWithPrivateFields")]
[TestCase("RefasmerTestAssembly.UnsafeClassWithFunctionPointer")]
public async Task CheckRefasmedType(string typeName)
{
var assemblyPath = await BuildTestAssembly();
var resultAssembly = RefasmTestAssembly(assemblyPath);
await VerifyTypeContent(resultAssembly, typeName);
}

private static async Task<string> BuildTestAssembly()
{
var root = FindSourceRoot();
var testProject = Path.Combine(root, "src/RefasmerTestAssembly/RefasmerTestAssembly.csproj");
Console.WriteLine($"Building project {testProject}…");
var result = await Command.Run("dotnet", "build", testProject, "--configuration", "Release").Task;

Assert.That(
result.ExitCode,
Is.EqualTo(0),
$"Failed to build test assembly, exit code {result.ExitCode}. StdOut:\n{result.StandardOutput}\nStdErr: {result.StandardError}");

return Path.Combine(root, "src/RefasmerTestAssembly/bin/Release/net6.0/RefasmerTestAssembly.dll");
}

private static string FindSourceRoot()
{
var current = Directory.GetCurrentDirectory();
while (current != null)
{
if (File.Exists(Path.Combine(current, "README.md")))
return current;
current = Path.GetDirectoryName(current);
}
throw new Exception("Cannot find source root.");
}

private static string RefasmTestAssembly(string assemblyPath)
{
var tempLocation = Path.GetTempFileName();
var exitCode = Program.Main(new[] { $"-v", $"--output={tempLocation}", assemblyPath });
Assert.That(exitCode, Is.EqualTo(0));

return tempLocation;
}

private static Task VerifyTypeContent(string assemblyPath, string typeName)
{
var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);
var type = assembly.MainModule.GetType(typeName);
var printout = new StringBuilder();
Printer.PrintType(type, printout);

var verifySettings = new VerifySettings();
verifySettings.DisableDiff();
verifySettings.UseDirectory("data");
verifySettings.UseParameters(typeName);
return Verify(printout, verifySettings);
}
}
44 changes: 44 additions & 0 deletions tests/Refasmer.Tests/Printer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Text;
using Mono.Cecil;

namespace JetBrains.Refasmer.Tests;

public static class Printer
{
public static void PrintType(TypeDefinition type, StringBuilder printout)
{
printout.AppendLine($"type: {type.FullName}");
if (type.HasFields)
{
printout.AppendLine("fields:");
foreach (var field in type.Fields)
{
printout.AppendLine($"- {field.Name}: {field.FieldType}");
}
}

if (type.HasMethods)
{
printout.AppendLine("methods:");
foreach (var method in type.Methods)
{
printout.Append($"- {method.Name}(");
var parameters = method.Parameters;
for (var i = 0; i < parameters.Count; i++)
{
printout.Append($"{parameters[i].ParameterType} {parameters[i].Name}");
if (i < parameters.Count - 1)
{
printout.Append(", ");
}
}

printout.AppendLine($"): {method.ReturnType}:");
foreach (var instruction in method.Body.Instructions)
{
printout.AppendLine($" - {instruction}");
}
}
}
}
}
12 changes: 9 additions & 3 deletions tests/Refasmer.Tests/Refasmer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>JetBrains.Refasmer.Tests</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -9,11 +10,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MedallionShell" Version="1.6.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Mono.Cecil" Version="0.11.6" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Verify.NUnit" Version="27.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\RefasmerExe\RefasmerExe.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: RefasmerTestAssembly.PublicClassWithPrivateFields
fields:
- PublicInt: System.Int32
methods:
- .ctor(): System.Void:
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: RefasmerTestAssembly.PublicStructWithPrivateFields
fields:
- PrivateInt: System.Int32
- PrivateInt2: System.Int32
- PublicInt: System.Int32
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: RefasmerTestAssembly.UnsafeClassWithFunctionPointer
methods:
- MethodWithFunctionPointer(method System.Void *() functionPointer): System.Void:
- .ctor(): System.Void:

0 comments on commit c0eeabc

Please sign in to comment.