From d0e13c80bff79bb7a83c163e6a3000dc0521f951 Mon Sep 17 00:00:00 2001 From: Ivan Migalev Date: Sat, 26 Oct 2024 18:38:28 +0200 Subject: [PATCH] Tests: check private nested types --- tests/Refasmer.Tests/IntegrationTests.cs | 1 + tests/Refasmer.Tests/Printer.cs | 37 +++++++++++++++---- ....PublicClassWithPrivateFields.verified.txt | 2 +- ...PublicStructWithPrivateFields.verified.txt | 2 +- ....StructWithNestedPrivateTypes.verified.txt | 7 ++++ ...nsafeClassWithFunctionPointer.verified.txt | 2 +- .../StructWithNestedPrivateTypes.cs | 11 ++++++ 7 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.StructWithNestedPrivateTypes.verified.txt create mode 100644 tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs diff --git a/tests/Refasmer.Tests/IntegrationTests.cs b/tests/Refasmer.Tests/IntegrationTests.cs index 5e06c38..40a94a1 100644 --- a/tests/Refasmer.Tests/IntegrationTests.cs +++ b/tests/Refasmer.Tests/IntegrationTests.cs @@ -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(); diff --git a/tests/Refasmer.Tests/Printer.cs b/tests/Refasmer.Tests/Printer.cs index 673ebd2..36a0a9c 100644 --- a/tests/Refasmer.Tests/Printer.cs +++ b/tests/Refasmer.Tests/Printer.cs @@ -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++) { @@ -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"; } } diff --git a/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.PublicClassWithPrivateFields.verified.txt b/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.PublicClassWithPrivateFields.verified.txt index 3f76c25..b3096b0 100644 --- a/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.PublicClassWithPrivateFields.verified.txt +++ b/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.PublicClassWithPrivateFields.verified.txt @@ -1,4 +1,4 @@ -type: RefasmerTestAssembly.PublicClassWithPrivateFields +public type: RefasmerTestAssembly.PublicClassWithPrivateFields fields: - PublicInt: System.Int32 methods: diff --git a/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.PublicStructWithPrivateFields.verified.txt b/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.PublicStructWithPrivateFields.verified.txt index cbff474..1dfe7c8 100644 --- a/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.PublicStructWithPrivateFields.verified.txt +++ b/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.PublicStructWithPrivateFields.verified.txt @@ -1,4 +1,4 @@ -type: RefasmerTestAssembly.PublicStructWithPrivateFields +public type: RefasmerTestAssembly.PublicStructWithPrivateFields fields: - PrivateInt: System.Int32 - PrivateInt2: System.Int32 diff --git a/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.StructWithNestedPrivateTypes.verified.txt b/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.StructWithNestedPrivateTypes.verified.txt new file mode 100644 index 0000000..71576b1 --- /dev/null +++ b/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.StructWithNestedPrivateTypes.verified.txt @@ -0,0 +1,7 @@ +public type: RefasmerTestAssembly.StructWithNestedPrivateTypes +fields: +- PrivateField: RefasmerTestAssembly.StructWithNestedPrivateTypes/NestedPrivateStruct +types: + private type: RefasmerTestAssembly.StructWithNestedPrivateTypes/NestedPrivateStruct + fields: + - Field: System.Int32 diff --git a/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.UnsafeClassWithFunctionPointer.verified.txt b/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.UnsafeClassWithFunctionPointer.verified.txt index f9eb906..679ec12 100644 --- a/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.UnsafeClassWithFunctionPointer.verified.txt +++ b/tests/Refasmer.Tests/data/IntegrationTests.CheckRefasmedType_typeName=RefasmerTestAssembly.UnsafeClassWithFunctionPointer.verified.txt @@ -1,4 +1,4 @@ -type: RefasmerTestAssembly.UnsafeClassWithFunctionPointer +public type: RefasmerTestAssembly.UnsafeClassWithFunctionPointer methods: - MethodWithFunctionPointer(method System.Void *() functionPointer): System.Void: - .ctor(): System.Void: diff --git a/tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs b/tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs new file mode 100644 index 0000000..b311a0c --- /dev/null +++ b/tests/RefasmerTestAssembly/StructWithNestedPrivateTypes.cs @@ -0,0 +1,11 @@ +namespace RefasmerTestAssembly; + +public struct StructWithNestedPrivateTypes +{ + private struct NestedPrivateStruct + { + private int Field; + } + + private NestedPrivateStruct PrivateField; +} \ No newline at end of file