Skip to content

Commit

Permalink
Tests: check private nested types
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Oct 26, 2024
1 parent 13c1d8b commit d0e13c8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions tests/Refasmer.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class IntegrationTests
[TestCase("RefasmerTestAssembly.PublicClassWithPrivateFields")]
[TestCase("RefasmerTestAssembly.PublicStructWithPrivateFields")]
[TestCase("RefasmerTestAssembly.UnsafeClassWithFunctionPointer")]
[TestCase("RefasmerTestAssembly.StructWithNestedPrivateTypes")]
public async Task CheckRefasmedType(string typeName)
{
var assemblyPath = await BuildTestAssembly();
Expand Down
37 changes: 29 additions & 8 deletions tests/Refasmer.Tests/Printer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ namespace JetBrains.Refasmer.Tests;

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

if (type.HasMethods)
{
printout.AppendLine("methods:");
printout.AppendLine($"{indent}methods:");
foreach (var method in type.Methods)
{
printout.Append($"- {method.Name}(");
printout.Append($"{indent}- {method.Name}(");
var parameters = method.Parameters;
for (var i = 0; i < parameters.Count; i++)
{
Expand All @@ -33,12 +34,32 @@ public static void PrintType(TypeDefinition type, StringBuilder printout)
}
}

printout.AppendLine($"): {method.ReturnType}:");
printout.AppendLine($"{indent}): {method.ReturnType}:");
foreach (var instruction in method.Body.Instructions)
{
printout.AppendLine($" - {instruction}");
printout.AppendLine($"{indent} - {instruction}");
}
}
}

if (type.HasNestedTypes)
{
printout.AppendLine($"{indent}types:");
foreach (var nestedType in type.NestedTypes)
{
PrintType(nestedType, printout, indent + " ");
}
}
}

private static string GetAccessString(TypeDefinition type)
{
if (type.IsPublic) return "public";
if (type.IsNestedFamily) return "protected";
if (type.IsNestedPrivate) return "private";
if (type.IsNestedAssembly) return "internal";
if (type.IsNestedFamilyOrAssembly) return "protected internal";
if (type.IsNestedFamilyAndAssembly) return "private protected";
return "internal";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type: RefasmerTestAssembly.PublicClassWithPrivateFields
public type: RefasmerTestAssembly.PublicClassWithPrivateFields
fields:
- PublicInt: System.Int32
methods:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type: RefasmerTestAssembly.PublicStructWithPrivateFields
public type: RefasmerTestAssembly.PublicStructWithPrivateFields
fields:
- PrivateInt: System.Int32
- PrivateInt2: System.Int32
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public type: RefasmerTestAssembly.StructWithNestedPrivateTypes
fields:
- PrivateField: RefasmerTestAssembly.StructWithNestedPrivateTypes/NestedPrivateStruct
types:
private type: RefasmerTestAssembly.StructWithNestedPrivateTypes/NestedPrivateStruct
fields:
- Field: System.Int32
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type: RefasmerTestAssembly.UnsafeClassWithFunctionPointer
public type: RefasmerTestAssembly.UnsafeClassWithFunctionPointer
methods:
- MethodWithFunctionPointer(method System.Void *() functionPointer): System.Void:
- .ctor(): System.Void:
11 changes: 11 additions & 0 deletions tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace RefasmerTestAssembly;

public struct StructWithNestedPrivateTypes
{
private struct NestedPrivateStruct
{
private int Field;

Check warning on line 7 in tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs

View workflow job for this annotation

GitHub Actions / main

The field 'StructWithNestedPrivateTypes.NestedPrivateStruct.Field' is never used

Check warning on line 7 in tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs

View workflow job for this annotation

GitHub Actions / main

The field 'StructWithNestedPrivateTypes.NestedPrivateStruct.Field' is never used

Check warning on line 7 in tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs

View workflow job for this annotation

GitHub Actions / main

The field 'StructWithNestedPrivateTypes.NestedPrivateStruct.Field' is never used

Check warning on line 7 in tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs

View workflow job for this annotation

GitHub Actions / main

The field 'StructWithNestedPrivateTypes.NestedPrivateStruct.Field' is never used
}

private NestedPrivateStruct PrivateField;

Check warning on line 10 in tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs

View workflow job for this annotation

GitHub Actions / main

The field 'StructWithNestedPrivateTypes.PrivateField' is never used

Check warning on line 10 in tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs

View workflow job for this annotation

GitHub Actions / main

The field 'StructWithNestedPrivateTypes.PrivateField' is never used

Check warning on line 10 in tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs

View workflow job for this annotation

GitHub Actions / main

The field 'StructWithNestedPrivateTypes.PrivateField' is never used

Check warning on line 10 in tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs

View workflow job for this annotation

GitHub Actions / main

The field 'StructWithNestedPrivateTypes.PrivateField' is never used
}

0 comments on commit d0e13c8

Please sign in to comment.