From 0e294d62bf8c6f1353afa8abb81ae6249dab4f2c Mon Sep 17 00:00:00 2001 From: brian-reichle <18721383+brian-reichle@users.noreply.github.com> Date: Sat, 14 Feb 2026 11:42:39 +1000 Subject: [PATCH] Use file scoped namespaces. --- .editorconfig | 2 + .../AssemblyNameParsing.cs | 17 +- .../FormatAssemblyName.cs | 33 +- .../FormatTypeName.cs | 33 +- .../Program.cs | 15 +- .../TypeNameParsing.cs | 17 +- .../InsAssemblyExtensionsTest.cs | 341 +++++----- .../InsFormatterTest.cs | 205 +++--- .../InsParserTest.cs | 343 ++++++----- .../InsRewriterTest.cs | 201 +++--- .../InsTypeFactoryTest.cs | 207 ++++--- .../RuntimeCompatibilityTest.cs | 63 +- .../TreeRenderer.cs | 581 +++++++++--------- src/TypeNameInterpretation/BuilderPool.cs | 77 ++- src/TypeNameInterpretation/Delimiters.cs | 17 +- .../ArgumentNullExceptionShims.cs | 21 +- .../CallerArgumentExpressionAttribute.cs | 11 +- .../StringBuilderExtensions.cs | 17 +- .../InsAssemblyExtensions.cs | 437 +++++++------ src/TypeNameInterpretation/InsFormatter.cs | 291 +++++---- src/TypeNameInterpretation/InsParser.cs | 507 ++++++++------- src/TypeNameInterpretation/InsRewriter.cs | 221 ++++--- src/TypeNameInterpretation/InsTypeFactory.cs | 37 +- .../InvalidTypeNameException.NetFramework.cs | 13 +- .../InvalidTypeNameException.cs | 25 +- .../Tree/IInsTypeVisitor.cs | 19 +- .../Tree/InsArrayType.cs | 27 +- .../Tree/InsAssembly.cs | 23 +- .../Tree/InsAssemblyQualification.cs | 23 +- .../Tree/InsByRefType.cs | 23 +- .../Tree/InsElementedType.cs | 17 +- .../Tree/InsGenericType.cs | 31 +- .../Tree/InsNamedType.cs | 55 +- .../Tree/InsPointerType.cs | 23 +- .../Tree/InsSZArrayType.cs | 23 +- src/TypeNameInterpretation/Tree/InsType.cs | 17 +- .../Tree/InsTypeKind.cs | 19 +- .../WellKnownQualificationNames.cs | 17 +- 38 files changed, 2007 insertions(+), 2042 deletions(-) diff --git a/.editorconfig b/.editorconfig index 5c3f827..082c686 100644 --- a/.editorconfig +++ b/.editorconfig @@ -54,6 +54,8 @@ csharp_style_inlined_variable_declaration = true:suggestion csharp_style_throw_expression = false:none csharp_style_conditional_delegate_call = true:suggestion +csharp_style_namespace_declarations = file_scoped:warning + # http://source.roslyn.codeplex.com/#Microsoft.CodeAnalysis.CSharp.Workspaces/Formatting/CSharpFormattingOptions.cs csharp_space_between_method_declaration_name_and_open_parenthesis = false diff --git a/src/TypeNameInterpretation.Benchmark/AssemblyNameParsing.cs b/src/TypeNameInterpretation.Benchmark/AssemblyNameParsing.cs index 1e6909a..7c399f1 100644 --- a/src/TypeNameInterpretation.Benchmark/AssemblyNameParsing.cs +++ b/src/TypeNameInterpretation.Benchmark/AssemblyNameParsing.cs @@ -1,15 +1,14 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using BenchmarkDotNet.Attributes; -namespace TypeNameInterpretation.Benchmark +namespace TypeNameInterpretation.Benchmark; + +[MemoryDiagnoser] +public class AssemblyNameParsing { - [MemoryDiagnoser] - public class AssemblyNameParsing - { - [Params("Foo", "Foo, Bar=\"Qux Quux\"", "Foo, Bar=\"Qux \\\"Quux\"")] - public string Text { get; set; } + [Params("Foo", "Foo, Bar=\"Qux Quux\"", "Foo, Bar=\"Qux \\\"Quux\"")] + public string Text { get; set; } - [Benchmark] - public InsAssembly Parse() => InsTypeFactory.ParseAssemblyName(Text); - } + [Benchmark] + public InsAssembly Parse() => InsTypeFactory.ParseAssemblyName(Text); } diff --git a/src/TypeNameInterpretation.Benchmark/FormatAssemblyName.cs b/src/TypeNameInterpretation.Benchmark/FormatAssemblyName.cs index 2d4d330..cba7f96 100644 --- a/src/TypeNameInterpretation.Benchmark/FormatAssemblyName.cs +++ b/src/TypeNameInterpretation.Benchmark/FormatAssemblyName.cs @@ -2,25 +2,24 @@ using BenchmarkDotNet.Attributes; using static TypeNameInterpretation.InsTypeFactory; -namespace TypeNameInterpretation.Benchmark +namespace TypeNameInterpretation.Benchmark; + +[MemoryDiagnoser] +public class FormatAssemblyName { - [MemoryDiagnoser] - public class FormatAssemblyName - { - [ParamsSource(nameof(TypesToFormat))] - public InsAssembly Type { get; set; } + [ParamsSource(nameof(TypesToFormat))] + public InsAssembly Type { get; set; } - public static IEnumerable TypesToFormat() + public static IEnumerable TypesToFormat() + { + return new[] { - return new[] - { - Assembly("Foo"), - Assembly("Foo", Qualification("Bar", "Qux Quux")), - Assembly("Foo", Qualification("Bar", "Qux \"Quux")), - }; - } - - [Benchmark] - public string Format() => InsFormatter.Format(Type); + Assembly("Foo"), + Assembly("Foo", Qualification("Bar", "Qux Quux")), + Assembly("Foo", Qualification("Bar", "Qux \"Quux")), + }; } + + [Benchmark] + public string Format() => InsFormatter.Format(Type); } diff --git a/src/TypeNameInterpretation.Benchmark/FormatTypeName.cs b/src/TypeNameInterpretation.Benchmark/FormatTypeName.cs index 490da7b..37a9328 100644 --- a/src/TypeNameInterpretation.Benchmark/FormatTypeName.cs +++ b/src/TypeNameInterpretation.Benchmark/FormatTypeName.cs @@ -2,25 +2,24 @@ using BenchmarkDotNet.Attributes; using static TypeNameInterpretation.InsTypeFactory; -namespace TypeNameInterpretation.Benchmark +namespace TypeNameInterpretation.Benchmark; + +[MemoryDiagnoser] +public class FormatTypeName { - [MemoryDiagnoser] - public class FormatTypeName - { - [ParamsSource(nameof(TypesToFormat))] - public InsType Type { get; set; } + [ParamsSource(nameof(TypesToFormat))] + public InsType Type { get; set; } - public static IEnumerable TypesToFormat() + public static IEnumerable TypesToFormat() + { + return new InsType[] { - return new InsType[] - { - NamedType("Foo"), - NamedType("Foo", Assembly("Bar", Qualification("Baz", "Qux Quux"))), - Generic(NamedType("Foo"), NamedType("Bar"), NamedType("Baz")), - }; - } - - [Benchmark] - public string Format() => InsFormatter.Format(Type); + NamedType("Foo"), + NamedType("Foo", Assembly("Bar", Qualification("Baz", "Qux Quux"))), + Generic(NamedType("Foo"), NamedType("Bar"), NamedType("Baz")), + }; } + + [Benchmark] + public string Format() => InsFormatter.Format(Type); } diff --git a/src/TypeNameInterpretation.Benchmark/Program.cs b/src/TypeNameInterpretation.Benchmark/Program.cs index 89d7ea3..2e62d74 100644 --- a/src/TypeNameInterpretation.Benchmark/Program.cs +++ b/src/TypeNameInterpretation.Benchmark/Program.cs @@ -1,15 +1,14 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using BenchmarkDotNet.Running; -namespace TypeNameInterpretation.Benchmark +namespace TypeNameInterpretation.Benchmark; + +static class Program { - static class Program + static void Main(string[] args) { - static void Main(string[] args) - { - BenchmarkSwitcher - .FromAssembly(typeof(Program).Assembly) - .Run(args: args); - } + BenchmarkSwitcher + .FromAssembly(typeof(Program).Assembly) + .Run(args: args); } } diff --git a/src/TypeNameInterpretation.Benchmark/TypeNameParsing.cs b/src/TypeNameInterpretation.Benchmark/TypeNameParsing.cs index c02d538..d28634b 100644 --- a/src/TypeNameInterpretation.Benchmark/TypeNameParsing.cs +++ b/src/TypeNameInterpretation.Benchmark/TypeNameParsing.cs @@ -1,15 +1,14 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using BenchmarkDotNet.Attributes; -namespace TypeNameInterpretation.Benchmark +namespace TypeNameInterpretation.Benchmark; + +[MemoryDiagnoser] +public class TypeNameParsing { - [MemoryDiagnoser] - public class TypeNameParsing - { - [Params("Foo", "Foo`2[[Bar, BarAssembly],[Baz, BazAssembly]]", "Foo, Bar, Baz=\"Qux Quux\"")] - public string Text { get; set; } + [Params("Foo", "Foo`2[[Bar, BarAssembly],[Baz, BazAssembly]]", "Foo, Bar, Baz=\"Qux Quux\"")] + public string Text { get; set; } - [Benchmark] - public InsType Parse() => InsTypeFactory.ParseTypeName(Text); - } + [Benchmark] + public InsType Parse() => InsTypeFactory.ParseTypeName(Text); } diff --git a/src/TypeNameInterpretation.Test/InsAssemblyExtensionsTest.cs b/src/TypeNameInterpretation.Test/InsAssemblyExtensionsTest.cs index 13885b7..5caa877 100644 --- a/src/TypeNameInterpretation.Test/InsAssemblyExtensionsTest.cs +++ b/src/TypeNameInterpretation.Test/InsAssemblyExtensionsTest.cs @@ -3,212 +3,211 @@ using System.Reflection; using NUnit.Framework; -namespace TypeNameInterpretation.Test +namespace TypeNameInterpretation.Test; + +[TestFixture] +class InsAssemblyExtensionsTest { - [TestFixture] - class InsAssemblyExtensionsTest + [TestCase("Foo, Version=4.2.0.0", "4.2.0.0")] + [TestCase("Foo", null)] + public void GetVersion(string assemblyName, string? expectedVersion) { - [TestCase("Foo, Version=4.2.0.0", "4.2.0.0")] - [TestCase("Foo", null)] - public void GetVersion(string assemblyName, string? expectedVersion) - { - using (Assert.EnterMultipleScope()) - { - var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - - if (expectedVersion == null) - { - Assert.That(assembly.TryGetVersion(out var version), Is.False); - Assert.That(version, Is.Null); - } - else - { - Assert.That(assembly.TryGetVersion(out var version), Is.True); - Assert.That(version, Is.EqualTo(Version.Parse(expectedVersion))); - } - } - } - - [TestCase("Foo, Version=invalid")] - public void GetVersion_Invalid(string assemblyName) + using (Assert.EnterMultipleScope()) { var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - Assert.That( - () => assembly.TryGetVersion(out _), - Throws.Exception.TypeOf() - .With.Message.EqualTo("Version qualification was provided, but was in an unrecognised format.")); - } - [TestCase("Foo, PublicKeyToken=0123456789ABCDEF", true, new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF })] - [TestCase("Foo, PublicKeyToken=null", true, null)] - [TestCase("Foo", false, null)] - public void GetPublicKeyToken(string assemblyName, bool success, byte[]? token) - { - using (Assert.EnterMultipleScope()) + if (expectedVersion == null) + { + Assert.That(assembly.TryGetVersion(out var version), Is.False); + Assert.That(version, Is.Null); + } + else { - var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - - if (success) - { - Assert.That(assembly.TryGetPublicKeyToken(out var value), Is.True); - Assert.That(value, Is.EqualTo(token)); - } - else - { - Assert.That(assembly.TryGetPublicKeyToken(out var value), Is.False); - Assert.That(value, Is.Null); - } + Assert.That(assembly.TryGetVersion(out var version), Is.True); + Assert.That(version, Is.EqualTo(Version.Parse(expectedVersion))); } } + } - [TestCase("Foo, PublicKeyToken=0123456789ABCDE")] - public void GetPublicKeyToken_Invalid(string assemblyName) + [TestCase("Foo, Version=invalid")] + public void GetVersion_Invalid(string assemblyName) + { + var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); + Assert.That( + () => assembly.TryGetVersion(out _), + Throws.Exception.TypeOf() + .With.Message.EqualTo("Version qualification was provided, but was in an unrecognised format.")); + } + + [TestCase("Foo, PublicKeyToken=0123456789ABCDEF", true, new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF })] + [TestCase("Foo, PublicKeyToken=null", true, null)] + [TestCase("Foo", false, null)] + public void GetPublicKeyToken(string assemblyName, bool success, byte[]? token) + { + using (Assert.EnterMultipleScope()) { var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - Assert.That( - () => assembly.TryGetPublicKeyToken(out _), - Throws.Exception.TypeOf() - .With.Message.EqualTo("PublicKeyToken qualification was provided, but was in an unrecognised format.")); - } - [TestCase("Foo, PublicKey=0123456789ABCDEF", true, new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF })] - [TestCase("Foo, PublicKey=null", true, null)] - [TestCase("Foo", false, null)] - public void GetPublicKey(string assemblyName, bool success, byte[]? token) - { - using (Assert.EnterMultipleScope()) + if (success) + { + Assert.That(assembly.TryGetPublicKeyToken(out var value), Is.True); + Assert.That(value, Is.EqualTo(token)); + } + else { - var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - - if (success) - { - Assert.That(assembly.TryGetPublicKey(out var value), Is.True); - Assert.That(value, Is.EqualTo(token)); - } - else - { - Assert.That(assembly.TryGetPublicKey(out var value), Is.False); - Assert.That(value, Is.Null); - } + Assert.That(assembly.TryGetPublicKeyToken(out var value), Is.False); + Assert.That(value, Is.Null); } } + } - [TestCase("Foo, PublicKey=0123456789ABCDE")] - public void GetPublicKey_Invalid(string assemblyName) - { - var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - Assert.That( - () => assembly.TryGetPublicKey(out _), - Throws.Exception.TypeOf() - .With.Message.EqualTo("PublicKey qualification was provided, but was in an unrecognised format.")); - } + [TestCase("Foo, PublicKeyToken=0123456789ABCDE")] + public void GetPublicKeyToken_Invalid(string assemblyName) + { + var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); + Assert.That( + () => assembly.TryGetPublicKeyToken(out _), + Throws.Exception.TypeOf() + .With.Message.EqualTo("PublicKeyToken qualification was provided, but was in an unrecognised format.")); + } - [TestCase("Foo", false, ProcessorArchitecture.None)] - [TestCase("Foo, ProcessorArchitecture=None", true, ProcessorArchitecture.None)] - [TestCase("Foo, ProcessorArchitecture=MSIL", true, ProcessorArchitecture.MSIL)] - [TestCase("Foo, ProcessorArchitecture=Amd64", true, ProcessorArchitecture.Amd64)] - public void GetProcessorArchitecture(string assemblyName, bool success, ProcessorArchitecture processorArchitecture) + [TestCase("Foo, PublicKey=0123456789ABCDEF", true, new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF })] + [TestCase("Foo, PublicKey=null", true, null)] + [TestCase("Foo", false, null)] + public void GetPublicKey(string assemblyName, bool success, byte[]? token) + { + using (Assert.EnterMultipleScope()) { var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - using (Assert.EnterMultipleScope()) + + if (success) { - Assert.That(assembly.TryGetProcessorArchitecture(out var architecture), Is.EqualTo(success)); - Assert.That(architecture, Is.EqualTo(processorArchitecture)); + Assert.That(assembly.TryGetPublicKey(out var value), Is.True); + Assert.That(value, Is.EqualTo(token)); } - } - - [TestCase("Bar", ExpectedResult = "A")] - [TestCase("Baz", ExpectedResult = "B")] - [TestCase("Missing", ExpectedResult = null)] - public string? TryGetQualification(string name) - { - var assembly = InsTypeFactory.ParseAssemblyName("Foo, Bar=A, Baz=B, Quux=C"); - - if (!assembly.TryGetQualification(name, out var result)) + else { - return null; + Assert.That(assembly.TryGetPublicKey(out var value), Is.False); + Assert.That(value, Is.Null); } - - return result ?? ""; } + } - [TestCase("Foo", "1.0", ExpectedResult = "Foo, Version=1.0")] - [TestCase("Foo, Version=1.0", "1.0", ExpectedResult = "Foo, Version=1.0")] - [TestCase("Foo, Version=1.0", "2.0", ExpectedResult = "Foo, Version=2.0")] - public string WithVersion(string assemblyName, string version) - { - return InsFormatter.Format( - InsTypeFactory.ParseAssemblyName(assemblyName) - .WithVersion(Version.Parse(version))); - } + [TestCase("Foo, PublicKey=0123456789ABCDE")] + public void GetPublicKey_Invalid(string assemblyName) + { + var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); + Assert.That( + () => assembly.TryGetPublicKey(out _), + Throws.Exception.TypeOf() + .With.Message.EqualTo("PublicKey qualification was provided, but was in an unrecognised format.")); + } - [TestCase("Foo", new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, ExpectedResult = "Foo, PublicKeyToken=0123456789ABCDEF")] - [TestCase("Foo, PublicKeyToken=0123456789ABCDEF", new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, ExpectedResult = "Foo, PublicKeyToken=0123456789ABCDEF")] - [TestCase("Foo, PublicKeyToken=0123456789ABCDEF", new byte[] { 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67 }, ExpectedResult = "Foo, PublicKeyToken=89ABCDEF01234567")] - [TestCase("Foo, PublicKeyToken=0123456789ABCDEF", null, ExpectedResult = "Foo, PublicKeyToken=null")] - public string WithPublicKeyToken(string assemblyName, byte[]? publicKey) + [TestCase("Foo", false, ProcessorArchitecture.None)] + [TestCase("Foo, ProcessorArchitecture=None", true, ProcessorArchitecture.None)] + [TestCase("Foo, ProcessorArchitecture=MSIL", true, ProcessorArchitecture.MSIL)] + [TestCase("Foo, ProcessorArchitecture=Amd64", true, ProcessorArchitecture.Amd64)] + public void GetProcessorArchitecture(string assemblyName, bool success, ProcessorArchitecture processorArchitecture) + { + var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); + using (Assert.EnterMultipleScope()) { - return InsFormatter.Format( - InsTypeFactory.ParseAssemblyName(assemblyName) - .WithPublicKeyToken(publicKey)); + Assert.That(assembly.TryGetProcessorArchitecture(out var architecture), Is.EqualTo(success)); + Assert.That(architecture, Is.EqualTo(processorArchitecture)); } + } - [TestCase("Foo", new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, ExpectedResult = "Foo, PublicKey=0123456789ABCDEF")] - [TestCase("Foo, PublicKey=0123456789ABCDEF", new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, ExpectedResult = "Foo, PublicKey=0123456789ABCDEF")] - [TestCase("Foo, PublicKey=0123456789ABCDEF", new byte[] { 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67 }, ExpectedResult = "Foo, PublicKey=89ABCDEF01234567")] - [TestCase("Foo, PublicKey=0123456789ABCDEF", null, ExpectedResult = "Foo, PublicKey=null")] - public string WithPublicKey(string assemblyName, byte[]? publicKey) - { - return InsFormatter.Format( - InsTypeFactory.ParseAssemblyName(assemblyName) - .WithPublicKey(publicKey)); - } + [TestCase("Bar", ExpectedResult = "A")] + [TestCase("Baz", ExpectedResult = "B")] + [TestCase("Missing", ExpectedResult = null)] + public string? TryGetQualification(string name) + { + var assembly = InsTypeFactory.ParseAssemblyName("Foo, Bar=A, Baz=B, Quux=C"); - [TestCase("Foo", ProcessorArchitecture.MSIL, ExpectedResult = "Foo, ProcessorArchitecture=MSIL")] - [TestCase("Foo, ProcessorArchitecture=MSIL", ProcessorArchitecture.Arm, ExpectedResult = "Foo, ProcessorArchitecture=Arm")] - [TestCase("Foo, ProcessorArchitecture=MSIL", ProcessorArchitecture.X86, ExpectedResult = "Foo, ProcessorArchitecture=X86")] - public string TryGetProcessorArchitecture(string assemblyName, ProcessorArchitecture processorArchitecture) + if (!assembly.TryGetQualification(name, out var result)) { - return InsFormatter.Format( - InsTypeFactory.ParseAssemblyName(assemblyName) - .WithProcessorArchitecture(processorArchitecture)); + return null; } - [TestCase("Foo", "Bar", "value", ExpectedResult = "Foo, Bar=value")] - [TestCase("Foo, Baz=A, Quux=B", "Bar", "value", ExpectedResult = "Foo, Baz=A, Quux=B, Bar=value")] - [TestCase("Foo, Baz=A, Bar=C, Quux=B", "Bar", "value", ExpectedResult = "Foo, Baz=A, Bar=value, Quux=B")] - public string WithQualification(string assemblyName, string name, string value) - { - return InsFormatter.Format( - InsTypeFactory.ParseAssemblyName(assemblyName) - .WithQualification(name, value)); - } + return result ?? ""; + } - [TestCase("Foo, Bar=value", "Bar", "value")] - public void WithQualification_Unchanged(string assemblyName, string name, string value) - { - var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - var newAssembly = assembly.WithQualification(name, value); - Assert.That(newAssembly, Is.SameAs(assembly)); - } + [TestCase("Foo", "1.0", ExpectedResult = "Foo, Version=1.0")] + [TestCase("Foo, Version=1.0", "1.0", ExpectedResult = "Foo, Version=1.0")] + [TestCase("Foo, Version=1.0", "2.0", ExpectedResult = "Foo, Version=2.0")] + public string WithVersion(string assemblyName, string version) + { + return InsFormatter.Format( + InsTypeFactory.ParseAssemblyName(assemblyName) + .WithVersion(Version.Parse(version))); + } - [TestCase("Foo", "Bar", ExpectedResult = "Foo")] - [TestCase("Foo, Bar=A", "Bar", ExpectedResult = "Foo")] - [TestCase("Foo, Baz=A, Bar=B, Quux=C", "Bar", ExpectedResult = "Foo, Baz=A, Quux=C")] - public string WithoutQualification(string assemblyName, string name) - { - return InsFormatter.Format( - InsTypeFactory.ParseAssemblyName(assemblyName) - .WithoutQualification(name)); - } + [TestCase("Foo", new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, ExpectedResult = "Foo, PublicKeyToken=0123456789ABCDEF")] + [TestCase("Foo, PublicKeyToken=0123456789ABCDEF", new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, ExpectedResult = "Foo, PublicKeyToken=0123456789ABCDEF")] + [TestCase("Foo, PublicKeyToken=0123456789ABCDEF", new byte[] { 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67 }, ExpectedResult = "Foo, PublicKeyToken=89ABCDEF01234567")] + [TestCase("Foo, PublicKeyToken=0123456789ABCDEF", null, ExpectedResult = "Foo, PublicKeyToken=null")] + public string WithPublicKeyToken(string assemblyName, byte[]? publicKey) + { + return InsFormatter.Format( + InsTypeFactory.ParseAssemblyName(assemblyName) + .WithPublicKeyToken(publicKey)); + } - [TestCase("Foo", "Bar")] - [TestCase("Foo, Baz=A", "Bar")] - public void WithoutQualification_Unchanged(string assemblyName, string name) - { - var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - var newAssembly = assembly.WithoutQualification(name); - Assert.That(newAssembly, Is.SameAs(assembly)); - } + [TestCase("Foo", new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, ExpectedResult = "Foo, PublicKey=0123456789ABCDEF")] + [TestCase("Foo, PublicKey=0123456789ABCDEF", new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, ExpectedResult = "Foo, PublicKey=0123456789ABCDEF")] + [TestCase("Foo, PublicKey=0123456789ABCDEF", new byte[] { 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67 }, ExpectedResult = "Foo, PublicKey=89ABCDEF01234567")] + [TestCase("Foo, PublicKey=0123456789ABCDEF", null, ExpectedResult = "Foo, PublicKey=null")] + public string WithPublicKey(string assemblyName, byte[]? publicKey) + { + return InsFormatter.Format( + InsTypeFactory.ParseAssemblyName(assemblyName) + .WithPublicKey(publicKey)); + } + + [TestCase("Foo", ProcessorArchitecture.MSIL, ExpectedResult = "Foo, ProcessorArchitecture=MSIL")] + [TestCase("Foo, ProcessorArchitecture=MSIL", ProcessorArchitecture.Arm, ExpectedResult = "Foo, ProcessorArchitecture=Arm")] + [TestCase("Foo, ProcessorArchitecture=MSIL", ProcessorArchitecture.X86, ExpectedResult = "Foo, ProcessorArchitecture=X86")] + public string TryGetProcessorArchitecture(string assemblyName, ProcessorArchitecture processorArchitecture) + { + return InsFormatter.Format( + InsTypeFactory.ParseAssemblyName(assemblyName) + .WithProcessorArchitecture(processorArchitecture)); + } + + [TestCase("Foo", "Bar", "value", ExpectedResult = "Foo, Bar=value")] + [TestCase("Foo, Baz=A, Quux=B", "Bar", "value", ExpectedResult = "Foo, Baz=A, Quux=B, Bar=value")] + [TestCase("Foo, Baz=A, Bar=C, Quux=B", "Bar", "value", ExpectedResult = "Foo, Baz=A, Bar=value, Quux=B")] + public string WithQualification(string assemblyName, string name, string value) + { + return InsFormatter.Format( + InsTypeFactory.ParseAssemblyName(assemblyName) + .WithQualification(name, value)); + } + + [TestCase("Foo, Bar=value", "Bar", "value")] + public void WithQualification_Unchanged(string assemblyName, string name, string value) + { + var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); + var newAssembly = assembly.WithQualification(name, value); + Assert.That(newAssembly, Is.SameAs(assembly)); + } + + [TestCase("Foo", "Bar", ExpectedResult = "Foo")] + [TestCase("Foo, Bar=A", "Bar", ExpectedResult = "Foo")] + [TestCase("Foo, Baz=A, Bar=B, Quux=C", "Bar", ExpectedResult = "Foo, Baz=A, Quux=C")] + public string WithoutQualification(string assemblyName, string name) + { + return InsFormatter.Format( + InsTypeFactory.ParseAssemblyName(assemblyName) + .WithoutQualification(name)); + } + + [TestCase("Foo", "Bar")] + [TestCase("Foo, Baz=A", "Bar")] + public void WithoutQualification_Unchanged(string assemblyName, string name) + { + var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); + var newAssembly = assembly.WithoutQualification(name); + Assert.That(newAssembly, Is.SameAs(assembly)); } } diff --git a/src/TypeNameInterpretation.Test/InsFormatterTest.cs b/src/TypeNameInterpretation.Test/InsFormatterTest.cs index 9796c23..b3eb6ae 100644 --- a/src/TypeNameInterpretation.Test/InsFormatterTest.cs +++ b/src/TypeNameInterpretation.Test/InsFormatterTest.cs @@ -3,122 +3,121 @@ using static TypeNameInterpretation.InsFormatter; using static TypeNameInterpretation.InsTypeFactory; -namespace TypeNameInterpretation.Test +namespace TypeNameInterpretation.Test; + +[TestFixture] +class InsFormatterTest { - [TestFixture] - class InsFormatterTest + [TestCase("Foo", ExpectedResult = "Foo")] + [TestCase("[Foo]", ExpectedResult = "\\[Foo\\]")] + [TestCase("Foo&", ExpectedResult = "Foo\\&")] + [TestCase("Foo*", ExpectedResult = "Foo\\*")] + [TestCase("Foo+", ExpectedResult = "Foo\\+")] + [TestCase("Foo\\Baz", ExpectedResult = "Foo\\\\Baz")] + public string SimpleNamedType(string identifierName) { - [TestCase("Foo", ExpectedResult = "Foo")] - [TestCase("[Foo]", ExpectedResult = "\\[Foo\\]")] - [TestCase("Foo&", ExpectedResult = "Foo\\&")] - [TestCase("Foo*", ExpectedResult = "Foo\\*")] - [TestCase("Foo+", ExpectedResult = "Foo\\+")] - [TestCase("Foo\\Baz", ExpectedResult = "Foo\\\\Baz")] - public string SimpleNamedType(string identifierName) - { - return Format(NamedType(identifierName)); - } - - [Test] - public void AvoidAllocationsOnSimpleAssemblyNames() - { - var name = "Foo"; - Assert.That(Format(Assembly(name)), Is.SameAs(name)); - } + return Format(NamedType(identifierName)); + } - [TestCase("Bar", ExpectedResult = "Foo, Bar")] - [TestCase("[Bar]", ExpectedResult = "Foo, \\[Bar\\]")] - [TestCase("Bar&", ExpectedResult = "Foo, Bar\\&")] - [TestCase("Bar*", ExpectedResult = "Foo, Bar\\*")] - [TestCase("Bar+", ExpectedResult = "Foo, Bar\\+")] - [TestCase("Bar\\Baz", ExpectedResult = "Foo, Bar\\\\Baz")] - public string AssemblyQualifiedNamedType(string identifier) - { - return Format(NamedType("Foo", Assembly(identifier))); - } + [Test] + public void AvoidAllocationsOnSimpleAssemblyNames() + { + var name = "Foo"; + Assert.That(Format(Assembly(name)), Is.SameAs(name)); + } - [TestCase("Culture", "", ExpectedResult = "Foo, Bar, Culture=\"\"")] - [TestCase("Culture", "neutral", ExpectedResult = "Foo, Bar, Culture=neutral")] - [TestCase("x\"y", "x\"y", ExpectedResult = "Foo, Bar, x\\\"y=\"x\\\"y\"")] - [TestCase("x\\y", "x\\y", ExpectedResult = "Foo, Bar, x\\\\y=\"x\\\\y\"")] - [TestCase("[xy]", "[xy]", ExpectedResult = "Foo, Bar, \\[xy\\]=\"[xy]\"")] - [TestCase("Culture", "\"xy\"", ExpectedResult = "Foo, Bar, Culture=\"\\\"xy\\\"\"")] - public string FullyQualifiedNamedType(string name, string value) - { - return Format(NamedType("Foo", Assembly("Bar", Qualification(name, value)))); - } + [TestCase("Bar", ExpectedResult = "Foo, Bar")] + [TestCase("[Bar]", ExpectedResult = "Foo, \\[Bar\\]")] + [TestCase("Bar&", ExpectedResult = "Foo, Bar\\&")] + [TestCase("Bar*", ExpectedResult = "Foo, Bar\\*")] + [TestCase("Bar+", ExpectedResult = "Foo, Bar\\+")] + [TestCase("Bar\\Baz", ExpectedResult = "Foo, Bar\\\\Baz")] + public string AssemblyQualifiedNamedType(string identifier) + { + return Format(NamedType("Foo", Assembly(identifier))); + } - [TestCase(true, true, ExpectedResult = "Foo`2[[Baz, Bar],[Quux, Bar, Culture=neutral]], Bar")] - [TestCase(true, false, ExpectedResult = "Foo`2[[Baz],[Quux]], Bar")] - [TestCase(false, true, ExpectedResult = "Foo`2[[Baz, Bar],[Quux, Bar, Culture=neutral]]")] - [TestCase(false, false, ExpectedResult = "Foo`2[[Baz],[Quux]]")] - public string GenericNamedType(bool qualifiedOuter, bool qualifiedInner) - { - var type = Generic( - NamedType( - "Foo`2", - qualifiedOuter ? _unqualifiedAssembly : null), - NamedType( - "Baz", - qualifiedInner ? _unqualifiedAssembly : null), - NamedType( - "Quux", - qualifiedInner ? _qualifiedAssembly : null)); + [TestCase("Culture", "", ExpectedResult = "Foo, Bar, Culture=\"\"")] + [TestCase("Culture", "neutral", ExpectedResult = "Foo, Bar, Culture=neutral")] + [TestCase("x\"y", "x\"y", ExpectedResult = "Foo, Bar, x\\\"y=\"x\\\"y\"")] + [TestCase("x\\y", "x\\y", ExpectedResult = "Foo, Bar, x\\\\y=\"x\\\\y\"")] + [TestCase("[xy]", "[xy]", ExpectedResult = "Foo, Bar, \\[xy\\]=\"[xy]\"")] + [TestCase("Culture", "\"xy\"", ExpectedResult = "Foo, Bar, Culture=\"\\\"xy\\\"\"")] + public string FullyQualifiedNamedType(string name, string value) + { + return Format(NamedType("Foo", Assembly("Bar", Qualification(name, value)))); + } - return Format(type); - } + [TestCase(true, true, ExpectedResult = "Foo`2[[Baz, Bar],[Quux, Bar, Culture=neutral]], Bar")] + [TestCase(true, false, ExpectedResult = "Foo`2[[Baz],[Quux]], Bar")] + [TestCase(false, true, ExpectedResult = "Foo`2[[Baz, Bar],[Quux, Bar, Culture=neutral]]")] + [TestCase(false, false, ExpectedResult = "Foo`2[[Baz],[Quux]]")] + public string GenericNamedType(bool qualifiedOuter, bool qualifiedInner) + { + var type = Generic( + NamedType( + "Foo`2", + qualifiedOuter ? _unqualifiedAssembly : null), + NamedType( + "Baz", + qualifiedInner ? _unqualifiedAssembly : null), + NamedType( + "Quux", + qualifiedInner ? _qualifiedAssembly : null)); - [TestCase(true, ExpectedResult = "Foo+Baz, Bar")] - [TestCase(false, ExpectedResult = "Foo+Baz")] - public string Nested(bool qualified) - { - return Format(NestedType(NamedType("Foo", qualified ? _unqualifiedAssembly : null), "Baz")); - } + return Format(type); + } - [TestCase(1, false, ExpectedResult = "Foo[*]")] - [TestCase(2, false, ExpectedResult = "Foo[,]")] - [TestCase(1, true, ExpectedResult = "Foo[*], Bar")] - [TestCase(2, true, ExpectedResult = "Foo[,], Bar")] - public string Array(int rank, bool qualified) - { - return Format(ArrayType(NamedType("Foo", qualified ? _unqualifiedAssembly : null), rank)); - } + [TestCase(true, ExpectedResult = "Foo+Baz, Bar")] + [TestCase(false, ExpectedResult = "Foo+Baz")] + public string Nested(bool qualified) + { + return Format(NestedType(NamedType("Foo", qualified ? _unqualifiedAssembly : null), "Baz")); + } - [TestCase(false, ExpectedResult = "Foo[]")] - [TestCase(true, ExpectedResult = "Foo[], Bar")] - public string SZArray(bool qualified) - { - return Format(SZArrayType(NamedType("Foo", qualified ? _unqualifiedAssembly : null))); - } + [TestCase(1, false, ExpectedResult = "Foo[*]")] + [TestCase(2, false, ExpectedResult = "Foo[,]")] + [TestCase(1, true, ExpectedResult = "Foo[*], Bar")] + [TestCase(2, true, ExpectedResult = "Foo[,], Bar")] + public string Array(int rank, bool qualified) + { + return Format(ArrayType(NamedType("Foo", qualified ? _unqualifiedAssembly : null), rank)); + } - [Test] - public void ArrayOfArrays() - { - Assert.That(Format(ArrayType(ArrayType(NamedType("Foo"), 1), 2)), Is.EqualTo("Foo[*][,]")); - } + [TestCase(false, ExpectedResult = "Foo[]")] + [TestCase(true, ExpectedResult = "Foo[], Bar")] + public string SZArray(bool qualified) + { + return Format(SZArrayType(NamedType("Foo", qualified ? _unqualifiedAssembly : null))); + } - [TestCase(false, ExpectedResult = "Foo&")] - [TestCase(true, ExpectedResult = "Foo&, Bar")] - public string ByRef(bool qualified) - { - return Format(ByRefType(NamedType("Foo", qualified ? _unqualifiedAssembly : null))); - } + [Test] + public void ArrayOfArrays() + { + Assert.That(Format(ArrayType(ArrayType(NamedType("Foo"), 1), 2)), Is.EqualTo("Foo[*][,]")); + } - [TestCase(false, ExpectedResult = "Foo*")] - [TestCase(true, ExpectedResult = "Foo*, Bar")] - public string Pointer(bool qualified) - { - return Format(PointerType(NamedType("Foo", qualified ? _unqualifiedAssembly : null))); - } + [TestCase(false, ExpectedResult = "Foo&")] + [TestCase(true, ExpectedResult = "Foo&, Bar")] + public string ByRef(bool qualified) + { + return Format(ByRefType(NamedType("Foo", qualified ? _unqualifiedAssembly : null))); + } - [TestCase(true, ExpectedResult = "Bar, Culture=neutral")] - [TestCase(false, ExpectedResult = "Bar")] - public string FormatAssembly(bool qualified) - { - return Format(qualified ? _qualifiedAssembly : _unqualifiedAssembly); - } + [TestCase(false, ExpectedResult = "Foo*")] + [TestCase(true, ExpectedResult = "Foo*, Bar")] + public string Pointer(bool qualified) + { + return Format(PointerType(NamedType("Foo", qualified ? _unqualifiedAssembly : null))); + } - readonly InsAssembly _unqualifiedAssembly = Assembly("Bar"); - readonly InsAssembly _qualifiedAssembly = Assembly("Bar", Qualification("Culture", "neutral")); + [TestCase(true, ExpectedResult = "Bar, Culture=neutral")] + [TestCase(false, ExpectedResult = "Bar")] + public string FormatAssembly(bool qualified) + { + return Format(qualified ? _qualifiedAssembly : _unqualifiedAssembly); } + + readonly InsAssembly _unqualifiedAssembly = Assembly("Bar"); + readonly InsAssembly _qualifiedAssembly = Assembly("Bar", Qualification("Culture", "neutral")); } diff --git a/src/TypeNameInterpretation.Test/InsParserTest.cs b/src/TypeNameInterpretation.Test/InsParserTest.cs index b134d09..b1b2b61 100644 --- a/src/TypeNameInterpretation.Test/InsParserTest.cs +++ b/src/TypeNameInterpretation.Test/InsParserTest.cs @@ -2,91 +2,91 @@ using System.Linq; using NUnit.Framework; -namespace TypeNameInterpretation.Test +namespace TypeNameInterpretation.Test; + +[TestFixture] +class InsParserTest { - [TestFixture] - class InsParserTest + [TestCase("Foo.Bar", ExpectedResult = "Foo.Bar")] + [TestCase("\\[Foo.Bar\\]", ExpectedResult = "[Foo.Bar]")] + [TestCase("Foo\\\\Bar", ExpectedResult = "Foo\\Bar")] + public string Assembly_UnqualifiedName(string assemblyName) { - [TestCase("Foo.Bar", ExpectedResult = "Foo.Bar")] - [TestCase("\\[Foo.Bar\\]", ExpectedResult = "[Foo.Bar]")] - [TestCase("Foo\\\\Bar", ExpectedResult = "Foo\\Bar")] - public string Assembly_UnqualifiedName(string assemblyName) - { - var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - Assert.That(assembly.Qualifications, Is.Empty); + var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); + Assert.That(assembly.Qualifications, Is.Empty); - return assembly.Name; - } + return assembly.Name; + } - [TestCase("Foo, Culture=\"\"", "Culture|")] - [TestCase("Foo, Culture=neutral", "Culture|neutral")] - [TestCase("Foo, Bar=Baz, Culture=neutral", "Bar|Baz", "Culture|neutral")] - [TestCase("Foo, Bar=Baz", "Bar|Baz")] - [TestCase("Foo, \"Bar\"=\"Baz\"", "Bar|Baz")] - [TestCase("Foo, Bar=Baz\\,", "Bar|Baz,")] - [TestCase("Foo, Bar=\"Baz,\"", "Bar|Baz,")] - [TestCase("Foo, Bar=Ba\\\"z", "Bar|Ba\"z")] - [TestCase("Foo, Bar=\"Ba\\\"z\"", "Bar|Ba\"z")] - public void Assembly_QualifiedName(string assemblyName, params string[] qualifications) + [TestCase("Foo, Culture=\"\"", "Culture|")] + [TestCase("Foo, Culture=neutral", "Culture|neutral")] + [TestCase("Foo, Bar=Baz, Culture=neutral", "Bar|Baz", "Culture|neutral")] + [TestCase("Foo, Bar=Baz", "Bar|Baz")] + [TestCase("Foo, \"Bar\"=\"Baz\"", "Bar|Baz")] + [TestCase("Foo, Bar=Baz\\,", "Bar|Baz,")] + [TestCase("Foo, Bar=\"Baz,\"", "Bar|Baz,")] + [TestCase("Foo, Bar=Ba\\\"z", "Bar|Ba\"z")] + [TestCase("Foo, Bar=\"Ba\\\"z\"", "Bar|Ba\"z")] + public void Assembly_QualifiedName(string assemblyName, params string[] qualifications) + { + var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); + using (Assert.EnterMultipleScope()) { - var assembly = InsTypeFactory.ParseAssemblyName(assemblyName); - using (Assert.EnterMultipleScope()) - { - Assert.That(assembly.Name, Is.EqualTo("Foo"), nameof(assembly.Name)); - Assert.That(assembly.Qualifications.Select(x => x.Name + "|" + x.Value), Is.EqualTo(qualifications), nameof(assembly.Qualifications)); - } + Assert.That(assembly.Name, Is.EqualTo("Foo"), nameof(assembly.Name)); + Assert.That(assembly.Qualifications.Select(x => x.Name + "|" + x.Value), Is.EqualTo(qualifications), nameof(assembly.Qualifications)); } + } - [TestCase("Foo]", ExpectedResult = "Unexpected char at position 3.")] - [TestCase("Foo, Bar,", ExpectedResult = "Unexpected char at position 8.")] - [TestCase("Foo, Bar", ExpectedResult = "Unexpected end of format.")] - [TestCase("Foo, Bar=", ExpectedResult = "Unexpected end of format.")] - [TestCase("Foo, Bar==", ExpectedResult = "Unexpected char at position 9.")] - [TestCase("Foo, Bar=\"Baz", ExpectedResult = "Unexpected end of format.")] - [TestCase("Foo, \\", ExpectedResult = "Unexpected end of format.")] - [TestCase("Foo, \"\\", ExpectedResult = "Unexpected end of format.")] - public string? Assembly_Invalid(string assemblyName) + [TestCase("Foo]", ExpectedResult = "Unexpected char at position 3.")] + [TestCase("Foo, Bar,", ExpectedResult = "Unexpected char at position 8.")] + [TestCase("Foo, Bar", ExpectedResult = "Unexpected end of format.")] + [TestCase("Foo, Bar=", ExpectedResult = "Unexpected end of format.")] + [TestCase("Foo, Bar==", ExpectedResult = "Unexpected char at position 9.")] + [TestCase("Foo, Bar=\"Baz", ExpectedResult = "Unexpected end of format.")] + [TestCase("Foo, \\", ExpectedResult = "Unexpected end of format.")] + [TestCase("Foo, \"\\", ExpectedResult = "Unexpected end of format.")] + public string? Assembly_Invalid(string assemblyName) + { + try { - try - { - InsTypeFactory.ParseAssemblyName(assemblyName); - Assert.Fail("Expected to throw an InvalidTypeNameException."); - return null; - } - catch (InvalidTypeNameException ex) - { - return ex.Message; - } + InsTypeFactory.ParseAssemblyName(assemblyName); + Assert.Fail("Expected to throw an InvalidTypeNameException."); + return null; } - - [Test] - public void Type_Unqualified() + catch (InvalidTypeNameException ex) { - var expected = + return ex.Message; + } + } + + [Test] + public void Type_Unqualified() + { + var expected = @"NamedType: ""Foo.Bar"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar")), Is.EqualTo(expected)); + } - [Test] - public void Type_Qualified() - { - var expected = + [Test] + public void Type_Qualified() + { + var expected = @"NamedType: ""Foo.Bar.Baz"" Assembly: ""FooBar"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar.Baz, FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar.Baz, FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_FullyQualified() - { - var expected = + [Test] + public void Type_FullyQualified() + { + var expected = @"NamedType: ""Foo.Bar.Baz"" Assembly: @@ -99,13 +99,13 @@ public void Type_FullyQualified() ""3.14"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar.Baz, FooBar, Culture=neutral, Version=3.14")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar.Baz, FooBar, Culture=neutral, Version=3.14")), Is.EqualTo(expected)); + } - [Test] - public void Type_Nested() - { - var expected = + [Test] + public void Type_Nested() + { + var expected = @"NamedType: ""Baz"" NamedType: @@ -114,13 +114,13 @@ public void Type_Nested() ""FooBar"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar+Baz, FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar+Baz, FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_GenericWithUnquolifiedArg1() - { - var expected = + [Test] + public void Type_GenericWithUnquolifiedArg1() + { + var expected = @"Generic: NamedType: ""Foo.Bar`1"" @@ -130,13 +130,13 @@ public void Type_GenericWithUnquolifiedArg1() ""Baz"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`1[Baz], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`1[Baz], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_GenericWithUnquolifiedArg2() - { - var expected = + [Test] + public void Type_GenericWithUnquolifiedArg2() + { + var expected = @"Generic: NamedType: ""Foo.Bar`2"" @@ -148,13 +148,13 @@ public void Type_GenericWithUnquolifiedArg2() ""Quux"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`2[Baz,Quux], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`2[Baz,Quux], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_GenericWithUnquolifiedArg3() - { - var expected = + [Test] + public void Type_GenericWithUnquolifiedArg3() + { + var expected = @"Generic: NamedType: ""Foo.Bar`3"" @@ -168,13 +168,13 @@ public void Type_GenericWithUnquolifiedArg3() ""Barry"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`3[Baz,Quux,Barry], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`3[Baz,Quux,Barry], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_GenericWithQuolifiedArg() - { - var expected = + [Test] + public void Type_GenericWithQuolifiedArg() + { + var expected = @"Generic: NamedType: ""Foo.Bar`1"" @@ -186,13 +186,13 @@ public void Type_GenericWithQuolifiedArg() ""FooBaz"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`1[[Baz, FooBaz]], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`1[[Baz, FooBaz]], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_GenericOfArray() - { - var expected = + [Test] + public void Type_GenericOfArray() + { + var expected = @"Generic: NamedType: ""Foo.Bar`1"" @@ -203,13 +203,13 @@ public void Type_GenericOfArray() ""Baz"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`1[Baz[]], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`1[Baz[]], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_ArrayOfGeneric() - { - var expected = + [Test] + public void Type_ArrayOfGeneric() + { + var expected = @"SZArrayType: Generic: NamedType: @@ -220,13 +220,13 @@ public void Type_ArrayOfGeneric() ""Baz"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`1[Baz][], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar`1[Baz][], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_SZArray() - { - var expected = + [Test] + public void Type_SZArray() + { + var expected = @"SZArrayType: NamedType: ""Foo.Bar"" @@ -234,13 +234,13 @@ public void Type_SZArray() ""FooBar"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar[], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar[], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_Array1() - { - var expected = + [Test] + public void Type_Array1() + { + var expected = @"ArrayType: NamedType: ""Foo.Bar"" @@ -249,13 +249,13 @@ public void Type_Array1() 1 "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar[*], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar[*], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_Array2() - { - var expected = + [Test] + public void Type_Array2() + { + var expected = @"ArrayType: NamedType: ""Foo.Bar"" @@ -264,13 +264,13 @@ public void Type_Array2() 2 "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar[,], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar[,], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_Array2_1() - { - var expected = + [Test] + public void Type_Array2_1() + { + var expected = @"ArrayType: ArrayType: NamedType: @@ -281,13 +281,13 @@ public void Type_Array2_1() 1 "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar[,][*], FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar[,][*], FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_Pointer() - { - var expected = + [Test] + public void Type_Pointer() + { + var expected = @"Pointer: NamedType: ""Foo.Bar"" @@ -295,13 +295,13 @@ public void Type_Pointer() ""FooBar"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar*, FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar*, FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_PointerPointer() - { - var expected = + [Test] + public void Type_PointerPointer() + { + var expected = @"Pointer: Pointer: NamedType: @@ -310,13 +310,13 @@ public void Type_PointerPointer() ""FooBar"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar**, FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar**, FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_ByRef() - { - var expected = + [Test] + public void Type_ByRef() + { + var expected = @"ByRef: NamedType: ""Foo.Bar"" @@ -324,13 +324,13 @@ public void Type_ByRef() ""FooBar"" "; - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar&, FooBar")), Is.EqualTo(expected)); - } + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Foo.Bar&, FooBar")), Is.EqualTo(expected)); + } - [Test] - public void Type_ComplexAssemblyLocation() - { - var expected = + [Test] + public void Type_ComplexAssemblyLocation() + { + var expected = @"Generic: NamedType: ""Baz"" @@ -353,27 +353,26 @@ public void Type_ComplexAssemblyLocation() 2 "; - // Ensure we can correctly locate the start of the assembly dispite complex syntax along the way. - Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Baz[[Foo.Bar[,], FooBar, Culture=\"neu\\\"tr]al\", Frob=bar\\]x, Version=3.14]], Quux")), Is.EqualTo(expected)); - } + // Ensure we can correctly locate the start of the assembly dispite complex syntax along the way. + Assert.That(TreeRenderer.Format(InsTypeFactory.ParseTypeName("Baz[[Foo.Bar[,], FooBar, Culture=\"neu\\\"tr]al\", Frob=bar\\]x, Version=3.14]], Quux")), Is.EqualTo(expected)); + } - [TestCase("Foo]", ExpectedResult = "Unexpected char at position 3.")] - [TestCase("Foo[*][A]", ExpectedResult = "Unexpected char at position 7.")] - [TestCase("Foo&[]", ExpectedResult = "Unexpected char at position 4.")] - [TestCase("Foo, Bar,", ExpectedResult = "Unexpected end of format.")] - [TestCase("Foo[", ExpectedResult = "Unexpected end of format.")] - public string? Type_Invalid(string typeName) + [TestCase("Foo]", ExpectedResult = "Unexpected char at position 3.")] + [TestCase("Foo[*][A]", ExpectedResult = "Unexpected char at position 7.")] + [TestCase("Foo&[]", ExpectedResult = "Unexpected char at position 4.")] + [TestCase("Foo, Bar,", ExpectedResult = "Unexpected end of format.")] + [TestCase("Foo[", ExpectedResult = "Unexpected end of format.")] + public string? Type_Invalid(string typeName) + { + try + { + InsTypeFactory.ParseTypeName(typeName); + Assert.Fail("Expected to throw an InvalidTypeNameException."); + return null; + } + catch (InvalidTypeNameException ex) { - try - { - InsTypeFactory.ParseTypeName(typeName); - Assert.Fail("Expected to throw an InvalidTypeNameException."); - return null; - } - catch (InvalidTypeNameException ex) - { - return ex.Message; - } + return ex.Message; } } } diff --git a/src/TypeNameInterpretation.Test/InsRewriterTest.cs b/src/TypeNameInterpretation.Test/InsRewriterTest.cs index f8bffb4..230d5c6 100644 --- a/src/TypeNameInterpretation.Test/InsRewriterTest.cs +++ b/src/TypeNameInterpretation.Test/InsRewriterTest.cs @@ -3,25 +3,25 @@ using NUnit.Framework; using static TypeNameInterpretation.InsTypeFactory; -namespace TypeNameInterpretation.Test +namespace TypeNameInterpretation.Test; + +[TestFixture] +class InsRewriterTest { - [TestFixture] - class InsRewriterTest + [Test] + public void Unchanged() { - [Test] - public void Unchanged() - { - var result = _allTheTypes.Apply(DummyRewriter.Instance, x => x); + var result = _allTheTypes.Apply(DummyRewriter.Instance, x => x); - Assert.That(result, Is.SameAs(_allTheTypes)); - } + Assert.That(result, Is.SameAs(_allTheTypes)); + } - [Test] - public void RewriteAssemblyName() - { - var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "Foo", "Bar")); + [Test] + public void RewriteAssemblyName() + { + var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "Foo", "Bar")); - const string ExpectedDiffs = + const string ExpectedDiffs = @"~ByRef: ~ Pointer: ~ SZArrayType: @@ -42,15 +42,15 @@ public void RewriteAssemblyName() 1 "; - Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); - } + Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); + } - [Test] - public void RewriteTypeName() - { - var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "TypeName", "AnotherName")); + [Test] + public void RewriteTypeName() + { + var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "TypeName", "AnotherName")); - const string ExpectedDiffs = + const string ExpectedDiffs = @"~ByRef: ~ Pointer: ~ SZArrayType: @@ -71,15 +71,15 @@ public void RewriteTypeName() 1 "; - Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); - } + Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); + } - [Test] - public void RewriteQualification() - { - var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "Culture", "AnotherCulture")); + [Test] + public void RewriteQualification() + { + var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "Culture", "AnotherCulture")); - const string ExpectedDiffs = + const string ExpectedDiffs = @"~ByRef: ~ Pointer: ~ SZArrayType: @@ -100,15 +100,15 @@ public void RewriteQualification() 1 "; - Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); - } + Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); + } - [Test] - public void RewriteTypeArg() - { - var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "TArg", "TArgument")); + [Test] + public void RewriteTypeArg() + { + var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "TArg", "TArgument")); - const string ExpectedDiffs = + const string ExpectedDiffs = @"~ByRef: ~ Pointer: ~ SZArrayType: @@ -129,15 +129,15 @@ public void RewriteTypeArg() 1 "; - Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); - } + Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); + } - [Test] - public void RewriteNested() - { - var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "Nested", "AnotherNested")); + [Test] + public void RewriteNested() + { + var result = _allTheTypes.Apply(DummyRewriter.Instance, x => Replace(x, "Nested", "AnotherNested")); - const string ExpectedDiffs = + const string ExpectedDiffs = @"~ByRef: ~ Pointer: ~ SZArrayType: @@ -158,80 +158,79 @@ public void RewriteNested() 1 "; - Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); - } + Assert.That(TreeRenderer.Diff(_allTheTypes, result), Is.EqualTo(ExpectedDiffs)); + } - static string Replace(string text, string oldValue, string newValue) => text == oldValue ? newValue : text; - - readonly InsType _allTheTypes = - ByRefType( - PointerType( - SZArrayType( - ArrayType( - Generic( - NestedType( - NamedType( - "TypeName", - Assembly( - "Foo", - Qualification("Culture", "neutral"))), - "Nested"), - NamedType("TArg")), - 1)))); - - sealed class DummyRewriter : InsRewriter> + static string Replace(string text, string oldValue, string newValue) => text == oldValue ? newValue : text; + + readonly InsType _allTheTypes = + ByRefType( + PointerType( + SZArrayType( + ArrayType( + Generic( + NestedType( + NamedType( + "TypeName", + Assembly( + "Foo", + Qualification("Culture", "neutral"))), + "Nested"), + NamedType("TArg")), + 1)))); + + sealed class DummyRewriter : InsRewriter> + { + public static DummyRewriter Instance { get; } = new DummyRewriter(); + + public override InsType VisitNamed(InsNamedType type, Func mutator) { - public static DummyRewriter Instance { get; } = new DummyRewriter(); + var newName = mutator(type.Name); - public override InsType VisitNamed(InsNamedType type, Func mutator) + if (newName == type.Name) { - var newName = mutator(type.Name); - - if (newName == type.Name) - { - return base.VisitNamed(type, mutator); - } - - if (type.DeclaringType != null) - { - return NestedType( - type.DeclaringType, - newName); - } - else - { - return NamedType( - newName, - type.Assembly == null ? null : VisitAssembly(type.Assembly, mutator)); - } + return base.VisitNamed(type, mutator); } - public override InsAssembly VisitAssembly(InsAssembly assembly, Func mutator) + if (type.DeclaringType != null) + { + return NestedType( + type.DeclaringType, + newName); + } + else { - var result = base.VisitAssembly(assembly, mutator); + return NamedType( + newName, + type.Assembly == null ? null : VisitAssembly(type.Assembly, mutator)); + } + } - var newName = mutator(assembly.Name); + public override InsAssembly VisitAssembly(InsAssembly assembly, Func mutator) + { + var result = base.VisitAssembly(assembly, mutator); - if (newName != assembly.Name) - { - return Assembly(newName, assembly.Qualifications); - } + var newName = mutator(assembly.Name); - return result; + if (newName != assembly.Name) + { + return Assembly(newName, assembly.Qualifications); } - public override InsAssemblyQualification VisitAssemblyQualification(InsAssemblyQualification qualification, Func mutator) - { - var newName = mutator(qualification.Name); - var newValue = mutator(qualification.Value); + return result; + } - if (newName == qualification.Name && newValue == qualification.Value) - { - return qualification; - } + public override InsAssemblyQualification VisitAssemblyQualification(InsAssemblyQualification qualification, Func mutator) + { + var newName = mutator(qualification.Name); + var newValue = mutator(qualification.Value); - return Qualification(newName, newValue); + if (newName == qualification.Name && newValue == qualification.Value) + { + return qualification; } + + return Qualification(newName, newValue); } } } diff --git a/src/TypeNameInterpretation.Test/InsTypeFactoryTest.cs b/src/TypeNameInterpretation.Test/InsTypeFactoryTest.cs index 855c299..932daab 100644 --- a/src/TypeNameInterpretation.Test/InsTypeFactoryTest.cs +++ b/src/TypeNameInterpretation.Test/InsTypeFactoryTest.cs @@ -3,137 +3,136 @@ using NUnit.Framework; using static TypeNameInterpretation.InsTypeFactory; -namespace TypeNameInterpretation.Test +namespace TypeNameInterpretation.Test; + +[TestFixture] +class InsTypeFactoryTest { - [TestFixture] - class InsTypeFactoryTest + [Test] + public void AssemblyQualificationFactory() { - [Test] - public void AssemblyQualificationFactory() + var qualification = Qualification("name", "value"); + using (Assert.EnterMultipleScope()) { - var qualification = Qualification("name", "value"); - using (Assert.EnterMultipleScope()) - { - Assert.That(qualification.Name, Is.EqualTo("name"), nameof(qualification.Name)); - Assert.That(qualification.Value, Is.EqualTo("value"), nameof(qualification.Value)); - } + Assert.That(qualification.Name, Is.EqualTo("name"), nameof(qualification.Name)); + Assert.That(qualification.Value, Is.EqualTo("value"), nameof(qualification.Value)); } + } + + [Test] + public void AssemblyFactory() + { + var qualification = Qualification("A", "B"); + var assembly = Assembly("AssemblyName", qualification); - [Test] - public void AssemblyFactory() + using (Assert.EnterMultipleScope()) { - var qualification = Qualification("A", "B"); - var assembly = Assembly("AssemblyName", qualification); - - using (Assert.EnterMultipleScope()) - { - Assert.That(assembly.Name, Is.EqualTo("AssemblyName"), nameof(assembly.Name)); - Assert.That(assembly.Qualifications.Single(), Is.SameAs(qualification), nameof(assembly.Qualifications)); - } + Assert.That(assembly.Name, Is.EqualTo("AssemblyName"), nameof(assembly.Name)); + Assert.That(assembly.Qualifications.Single(), Is.SameAs(qualification), nameof(assembly.Qualifications)); } + } + + [Test] + public void NamedTypeFactory() + { + var assembly = Assembly("A"); + var type1 = NamedType("B"); + var type2 = NamedType("TypeName", assembly); - [Test] - public void NamedTypeFactory() + using (Assert.EnterMultipleScope()) { - var assembly = Assembly("A"); - var type1 = NamedType("B"); - var type2 = NamedType("TypeName", assembly); - - using (Assert.EnterMultipleScope()) - { - Assert.That(type1.Kind, Is.EqualTo(InsTypeKind.Named), nameof(type1.Kind) + " 1"); - Assert.That(type1.Name, Is.EqualTo("B"), nameof(type1.Name) + " 1"); - Assert.That(type1.Assembly, Is.Null, nameof(type1.Assembly) + " 1"); - Assert.That(type1.DeclaringType, Is.Null, nameof(type1.DeclaringType) + " 1"); - - Assert.That(type2.Kind, Is.EqualTo(InsTypeKind.Named), nameof(type2.Kind) + " 2"); - Assert.That(type2.Name, Is.EqualTo("TypeName"), nameof(type2.Name) + " 2"); - Assert.That(type2.Assembly, Is.SameAs(assembly), nameof(type2.Assembly) + " 2"); - Assert.That(type2.DeclaringType, Is.Null, nameof(type2.DeclaringType) + " 2"); - } + Assert.That(type1.Kind, Is.EqualTo(InsTypeKind.Named), nameof(type1.Kind) + " 1"); + Assert.That(type1.Name, Is.EqualTo("B"), nameof(type1.Name) + " 1"); + Assert.That(type1.Assembly, Is.Null, nameof(type1.Assembly) + " 1"); + Assert.That(type1.DeclaringType, Is.Null, nameof(type1.DeclaringType) + " 1"); + + Assert.That(type2.Kind, Is.EqualTo(InsTypeKind.Named), nameof(type2.Kind) + " 2"); + Assert.That(type2.Name, Is.EqualTo("TypeName"), nameof(type2.Name) + " 2"); + Assert.That(type2.Assembly, Is.SameAs(assembly), nameof(type2.Assembly) + " 2"); + Assert.That(type2.DeclaringType, Is.Null, nameof(type2.DeclaringType) + " 2"); } + } + + [Test] + public void ArrayTypeFactory() + { + var elementType = NamedType("A"); + var type = ArrayType(elementType, 2); - [Test] - public void ArrayTypeFactory() + using (Assert.EnterMultipleScope()) { - var elementType = NamedType("A"); - var type = ArrayType(elementType, 2); - - using (Assert.EnterMultipleScope()) - { - Assert.That(type.Kind, Is.EqualTo(InsTypeKind.Array), nameof(type.Kind)); - Assert.That(type.ElementType, Is.SameAs(elementType), nameof(type.ElementType)); - Assert.That(type.Rank, Is.EqualTo(2), nameof(type.Rank)); - } + Assert.That(type.Kind, Is.EqualTo(InsTypeKind.Array), nameof(type.Kind)); + Assert.That(type.ElementType, Is.SameAs(elementType), nameof(type.ElementType)); + Assert.That(type.Rank, Is.EqualTo(2), nameof(type.Rank)); } + } + + [Test] + public void PointerTypeFactory() + { + var elementType = NamedType("A"); + var type = PointerType(elementType); - [Test] - public void PointerTypeFactory() + using (Assert.EnterMultipleScope()) { - var elementType = NamedType("A"); - var type = PointerType(elementType); - - using (Assert.EnterMultipleScope()) - { - Assert.That(type.Kind, Is.EqualTo(InsTypeKind.Pointer), nameof(type.Kind)); - Assert.That(type.ElementType, Is.SameAs(elementType), nameof(type.ElementType)); - } + Assert.That(type.Kind, Is.EqualTo(InsTypeKind.Pointer), nameof(type.Kind)); + Assert.That(type.ElementType, Is.SameAs(elementType), nameof(type.ElementType)); } + } + + [Test] + public void ByRefTypeFactory() + { + var elementType = NamedType("A"); + var type = ByRefType(elementType); - [Test] - public void ByRefTypeFactory() + using (Assert.EnterMultipleScope()) { - var elementType = NamedType("A"); - var type = ByRefType(elementType); - - using (Assert.EnterMultipleScope()) - { - Assert.That(type.Kind, Is.EqualTo(InsTypeKind.ByRef), nameof(type.Kind)); - Assert.That(type.ElementType, Is.SameAs(elementType), nameof(type.ElementType)); - } + Assert.That(type.Kind, Is.EqualTo(InsTypeKind.ByRef), nameof(type.Kind)); + Assert.That(type.ElementType, Is.SameAs(elementType), nameof(type.ElementType)); } + } + + [Test] + public void NestedFactory() + { + var declaringType = NamedType("B"); + var type = NestedType(declaringType, "TypeName"); - [Test] - public void NestedFactory() + using (Assert.EnterMultipleScope()) { - var declaringType = NamedType("B"); - var type = NestedType(declaringType, "TypeName"); - - using (Assert.EnterMultipleScope()) - { - Assert.That(type.Kind, Is.EqualTo(InsTypeKind.Named), nameof(type.Kind)); - Assert.That(type.Name, Is.EqualTo("TypeName"), nameof(type.Name)); - Assert.That(type.Assembly, Is.Null, nameof(type.Assembly)); - Assert.That(type.DeclaringType, Is.EqualTo(declaringType), nameof(type.DeclaringType)); - } + Assert.That(type.Kind, Is.EqualTo(InsTypeKind.Named), nameof(type.Kind)); + Assert.That(type.Name, Is.EqualTo("TypeName"), nameof(type.Name)); + Assert.That(type.Assembly, Is.Null, nameof(type.Assembly)); + Assert.That(type.DeclaringType, Is.EqualTo(declaringType), nameof(type.DeclaringType)); } + } + + [Test] + public void GenericTypeFactory() + { + var type1 = NamedType("A"); + var type2 = NamedType("B"); + var generic = Generic(type1, type2); - [Test] - public void GenericTypeFactory() + using (Assert.EnterMultipleScope()) { - var type1 = NamedType("A"); - var type2 = NamedType("B"); - var generic = Generic(type1, type2); - - using (Assert.EnterMultipleScope()) - { - Assert.That(generic.Kind, Is.EqualTo(InsTypeKind.Generic), nameof(generic.Kind)); - Assert.That(generic.Definition, Is.EqualTo(type1), nameof(generic.Definition)); - Assert.That(generic.TypeArguments.Single(), Is.SameAs(type2), nameof(generic.TypeArguments)); - } + Assert.That(generic.Kind, Is.EqualTo(InsTypeKind.Generic), nameof(generic.Kind)); + Assert.That(generic.Definition, Is.EqualTo(type1), nameof(generic.Definition)); + Assert.That(generic.TypeArguments.Single(), Is.SameAs(type2), nameof(generic.TypeArguments)); } + } + + [Test] + public void SZArrayTypeFactory() + { + var elementType = NamedType("A"); + var type = SZArrayType(elementType); - [Test] - public void SZArrayTypeFactory() + using (Assert.EnterMultipleScope()) { - var elementType = NamedType("A"); - var type = SZArrayType(elementType); - - using (Assert.EnterMultipleScope()) - { - Assert.That(type.Kind, Is.EqualTo(InsTypeKind.SZArray), nameof(type.Kind)); - Assert.That(type.ElementType, Is.SameAs(elementType), nameof(type.ElementType)); - } + Assert.That(type.Kind, Is.EqualTo(InsTypeKind.SZArray), nameof(type.Kind)); + Assert.That(type.ElementType, Is.SameAs(elementType), nameof(type.ElementType)); } } } diff --git a/src/TypeNameInterpretation.Test/RuntimeCompatibilityTest.cs b/src/TypeNameInterpretation.Test/RuntimeCompatibilityTest.cs index f9191f2..16120a1 100644 --- a/src/TypeNameInterpretation.Test/RuntimeCompatibilityTest.cs +++ b/src/TypeNameInterpretation.Test/RuntimeCompatibilityTest.cs @@ -3,44 +3,43 @@ using System.Collections.Generic; using NUnit.Framework; -namespace TypeNameInterpretation.Test +namespace TypeNameInterpretation.Test; + +[TestFixture] +class RuntimeCompatibilityTest { - [TestFixture] - class RuntimeCompatibilityTest + [TestCase(typeof(int))] + [TestCase(typeof(InsTypeFactory))] + [TestCase(typeof(List<>))] + [TestCase(typeof(List))] + [TestCase(typeof(List.Enumerator))] + [TestCase(typeof(Dictionary>))] + [TestCase(typeof(string[]))] + [TestCase(typeof(string[,][]))] + [TestCaseSource(nameof(AdditionalTypes))] + public void RoundTrip(Type type) { - [TestCase(typeof(int))] - [TestCase(typeof(InsTypeFactory))] - [TestCase(typeof(List<>))] - [TestCase(typeof(List))] - [TestCase(typeof(List.Enumerator))] - [TestCase(typeof(Dictionary>))] - [TestCase(typeof(string[]))] - [TestCase(typeof(string[,][]))] - [TestCaseSource(nameof(AdditionalTypes))] - public void RoundTrip(Type type) - { - // We want to assert that InsTypeFactory.ParseTypeName should be able to handle whatever Type.AssemblyQualifiedName produces and - // InsFormatter.Format should produce something that Type.GetType can understand, but we don't want to tie ourselves to any - // particular assembly name or qualifications that a given target framework uses. We could also assert that we format the type - // name in the same way as AssemblyQualifiedName, but then we run the risk of breaking if different frameworks format it - // differently. - var insType = InsTypeFactory.ParseTypeName(type.AssemblyQualifiedName!); - var resultType = Type.GetType(InsFormatter.Format(insType)); - Assert.That(resultType, Is.EqualTo(type)); - } + // We want to assert that InsTypeFactory.ParseTypeName should be able to handle whatever Type.AssemblyQualifiedName produces and + // InsFormatter.Format should produce something that Type.GetType can understand, but we don't want to tie ourselves to any + // particular assembly name or qualifications that a given target framework uses. We could also assert that we format the type + // name in the same way as AssemblyQualifiedName, but then we run the risk of breaking if different frameworks format it + // differently. + var insType = InsTypeFactory.ParseTypeName(type.AssemblyQualifiedName!); + var resultType = Type.GetType(InsFormatter.Format(insType)); + Assert.That(resultType, Is.EqualTo(type)); + } - protected static IEnumerable AdditionalTypes + protected static IEnumerable AdditionalTypes + { + get { - get - { - var baseType = typeof(int); + var baseType = typeof(int); - yield return Test(baseType.MakeArrayType(1)); - yield return Test(baseType.MakePointerType()); - yield return Test(baseType.MakeByRefType()); + yield return Test(baseType.MakeArrayType(1)); + yield return Test(baseType.MakePointerType()); + yield return Test(baseType.MakeByRefType()); - static TestCaseData Test(Type type) => new(type); - } + static TestCaseData Test(Type type) => new(type); } } } diff --git a/src/TypeNameInterpretation.Test/TreeRenderer.cs b/src/TypeNameInterpretation.Test/TreeRenderer.cs index 2d9cc30..61959e8 100644 --- a/src/TypeNameInterpretation.Test/TreeRenderer.cs +++ b/src/TypeNameInterpretation.Test/TreeRenderer.cs @@ -3,392 +3,391 @@ using System.Collections.Immutable; using System.Text; -namespace TypeNameInterpretation.Test +namespace TypeNameInterpretation.Test; + +static class TreeRenderer { - static class TreeRenderer + public static string Diff(InsType type1, InsType type2) + { + var builder = new StringBuilder(); + new Context(builder).DiffType(0, type1, type2); + return builder.ToString(); + } + + public static string Format(InsType type) + { + var builder = new StringBuilder(); + new Context(builder).FormatType('\0', 0, type); + return builder.ToString(); + } + + const char Value1 = '-'; + const char Value2 = '+'; + const char Similar = '~'; + const char Same = ' '; + + readonly struct Context(StringBuilder builder) { - public static string Diff(InsType type1, InsType type2) + public void DiffType(int indent, InsType? type1, InsType? type2) { - var builder = new StringBuilder(); - new Context(builder).DiffType(0, type1, type2); - return builder.ToString(); + if (type1 == type2) + { + FormatType(Same, indent, type1); + return; + } + + if (type1 == null || type2 == null || type1.Kind != type2.Kind) + { + FormatType(Value1, indent, type1); + FormatType(Value2, indent, type2); + return; + } + + switch (type1.Kind) + { + case InsTypeKind.Array: + DiffArrayType(indent, (InsArrayType)type1, (InsArrayType)type2); + break; + + case InsTypeKind.ByRef: + DiffByRefType(indent, (InsByRefType)type1, (InsByRefType)type2); + break; + + case InsTypeKind.Generic: + DiffGenericType(indent, (InsGenericType)type1, (InsGenericType)type2); + break; + + case InsTypeKind.Named: + DiffNamedType(indent, (InsNamedType)type1, (InsNamedType)type2); + break; + + case InsTypeKind.Pointer: + DiffPointerType(indent, (InsPointerType)type1, (InsPointerType)type2); + break; + + case InsTypeKind.SZArray: + DiffSZArray(indent, (InsSZArrayType)type1, (InsSZArrayType)type2); + break; + } } - public static string Format(InsType type) + void DiffArrayType(int indent, InsArrayType type1, InsArrayType type2) { - var builder = new StringBuilder(); - new Context(builder).FormatType('\0', 0, type); - return builder.ToString(); + FormatLabel(Similar, indent, "ArrayType"); + + indent++; + DiffType(indent, type1.ElementType, type2.ElementType); + DiffLiteral(indent, type1.Rank, type2.Rank); } - const char Value1 = '-'; - const char Value2 = '+'; - const char Similar = '~'; - const char Same = ' '; + void DiffByRefType(int indent, InsByRefType type1, InsByRefType type2) + { + FormatLabel(Similar, indent, "ByRef"); - readonly struct Context(StringBuilder builder) + indent++; + DiffType(indent, type1.ElementType, type2.ElementType); + } + + void DiffGenericType(int indent, InsGenericType type1, InsGenericType type2) { - public void DiffType(int indent, InsType? type1, InsType? type2) - { - if (type1 == type2) - { - FormatType(Same, indent, type1); - return; - } + FormatLabel(Similar, indent, "Generic"); - if (type1 == null || type2 == null || type1.Kind != type2.Kind) - { - FormatType(Value1, indent, type1); - FormatType(Value2, indent, type2); - return; - } + indent++; + DiffType(indent, type1.Definition, type2.Definition); + DiffTypes(indent, type1.TypeArguments, type2.TypeArguments); + } - switch (type1.Kind) - { - case InsTypeKind.Array: - DiffArrayType(indent, (InsArrayType)type1, (InsArrayType)type2); - break; + void DiffNamedType(int indent, InsNamedType type1, InsNamedType type2) + { + FormatLabel(Similar, indent, "NamedType"); - case InsTypeKind.ByRef: - DiffByRefType(indent, (InsByRefType)type1, (InsByRefType)type2); - break; + indent++; + DiffLiteral(indent, type1.Name, type2.Name); + DiffType(indent, type1.DeclaringType, type2.DeclaringType); + DiffAssembly(indent, type1.Assembly, type2.Assembly); + } - case InsTypeKind.Generic: - DiffGenericType(indent, (InsGenericType)type1, (InsGenericType)type2); - break; + void DiffPointerType(int indent, InsPointerType type1, InsPointerType type2) + { + FormatLabel(Similar, indent, "Pointer"); - case InsTypeKind.Named: - DiffNamedType(indent, (InsNamedType)type1, (InsNamedType)type2); - break; + indent++; + DiffType(indent, type1.ElementType, type2.ElementType); + } - case InsTypeKind.Pointer: - DiffPointerType(indent, (InsPointerType)type1, (InsPointerType)type2); - break; + void DiffSZArray(int indent, InsSZArrayType type1, InsSZArrayType type2) + { + FormatLabel(Similar, indent, "SZArrayType"); - case InsTypeKind.SZArray: - DiffSZArray(indent, (InsSZArrayType)type1, (InsSZArrayType)type2); - break; - } - } + indent++; + DiffType(indent, type1.ElementType, type2.ElementType); + } - void DiffArrayType(int indent, InsArrayType type1, InsArrayType type2) + void DiffTypes(int indent, ImmutableArray types1, ImmutableArray types2) + { + if (types1 == types2) { - FormatLabel(Similar, indent, "ArrayType"); - - indent++; - DiffType(indent, type1.ElementType, type2.ElementType); - DiffLiteral(indent, type1.Rank, type2.Rank); + FormatTypes(Same, indent, types1); } - - void DiffByRefType(int indent, InsByRefType type1, InsByRefType type2) + else { - FormatLabel(Similar, indent, "ByRef"); - - indent++; - DiffType(indent, type1.ElementType, type2.ElementType); + for (var i = 0; i < types1.Length; i++) + { + DiffType(indent, types1[i], types2[i]); + } } + } - void DiffGenericType(int indent, InsGenericType type1, InsGenericType type2) + void DiffAssembly(int indent, InsAssembly? assembly1, InsAssembly? assembly2) + { + if (ReferenceEquals(assembly1, assembly2)) { - FormatLabel(Similar, indent, "Generic"); - - indent++; - DiffType(indent, type1.Definition, type2.Definition); - DiffTypes(indent, type1.TypeArguments, type2.TypeArguments); + FormatAssembly(Same, indent, assembly1); } - - void DiffNamedType(int indent, InsNamedType type1, InsNamedType type2) + else if (assembly1 == null || assembly2 == null) { - FormatLabel(Similar, indent, "NamedType"); - - indent++; - DiffLiteral(indent, type1.Name, type2.Name); - DiffType(indent, type1.DeclaringType, type2.DeclaringType); - DiffAssembly(indent, type1.Assembly, type2.Assembly); + FormatAssembly(Value1, indent, assembly1); + FormatAssembly(Value2, indent, assembly2); } - - void DiffPointerType(int indent, InsPointerType type1, InsPointerType type2) + else { - FormatLabel(Similar, indent, "Pointer"); + FormatLabel(Similar, indent, "Assembly"); indent++; - DiffType(indent, type1.ElementType, type2.ElementType); + DiffLiteral(indent, assembly1.Name, assembly2.Name); + DiffQualifications(indent, assembly1.Qualifications, assembly2.Qualifications); } + } - void DiffSZArray(int indent, InsSZArrayType type1, InsSZArrayType type2) + void DiffQualifications(int indent, ImmutableArray qualifications1, ImmutableArray qualifications2) + { + if (qualifications1 == qualifications2) { - FormatLabel(Similar, indent, "SZArrayType"); - - indent++; - DiffType(indent, type1.ElementType, type2.ElementType); + FormatQualifications(Same, indent, qualifications1); } - - void DiffTypes(int indent, ImmutableArray types1, ImmutableArray types2) + else { - if (types1 == types2) - { - FormatTypes(Same, indent, types1); - } - else + for (var i = 0; i < qualifications1.Length; i++) { - for (var i = 0; i < types1.Length; i++) - { - DiffType(indent, types1[i], types2[i]); - } + DiffQualification(indent, qualifications1[i], qualifications2[i]); } } + } - void DiffAssembly(int indent, InsAssembly? assembly1, InsAssembly? assembly2) + void DiffQualification(int indent, InsAssemblyQualification qualification1, InsAssemblyQualification qualification2) + { + if (ReferenceEquals(qualification1, qualification2)) { - if (ReferenceEquals(assembly1, assembly2)) - { - FormatAssembly(Same, indent, assembly1); - } - else if (assembly1 == null || assembly2 == null) - { - FormatAssembly(Value1, indent, assembly1); - FormatAssembly(Value2, indent, assembly2); - } - else - { - FormatLabel(Similar, indent, "Assembly"); + FormatQualification(Same, indent, qualification1); + } + else + { + FormatLabel(Similar, indent, "Qualification"); - indent++; - DiffLiteral(indent, assembly1.Name, assembly2.Name); - DiffQualifications(indent, assembly1.Qualifications, assembly2.Qualifications); - } + indent++; + DiffLiteral(indent, qualification1.Name, qualification2.Name); + DiffLiteral(indent, qualification1.Value, qualification2.Value); } + } - void DiffQualifications(int indent, ImmutableArray qualifications1, ImmutableArray qualifications2) + void DiffLiteral(int indent, string value1, string value2) + { + if (ReferenceEquals(value1, value2)) { - if (qualifications1 == qualifications2) - { - FormatQualifications(Same, indent, qualifications1); - } - else - { - for (var i = 0; i < qualifications1.Length; i++) - { - DiffQualification(indent, qualifications1[i], qualifications2[i]); - } - } + FormatLiteral(Same, indent, value1); } - - void DiffQualification(int indent, InsAssemblyQualification qualification1, InsAssemblyQualification qualification2) + else if (value1 == value2) { - if (ReferenceEquals(qualification1, qualification2)) - { - FormatQualification(Same, indent, qualification1); - } - else - { - FormatLabel(Similar, indent, "Qualification"); - - indent++; - DiffLiteral(indent, qualification1.Name, qualification2.Name); - DiffLiteral(indent, qualification1.Value, qualification2.Value); - } + FormatLiteral(Similar, indent, value1); } - - void DiffLiteral(int indent, string value1, string value2) + else { - if (ReferenceEquals(value1, value2)) - { - FormatLiteral(Same, indent, value1); - } - else if (value1 == value2) - { - FormatLiteral(Similar, indent, value1); - } - else - { - FormatLiteral(Value1, indent, value1); - FormatLiteral(Value2, indent, value2); - } + FormatLiteral(Value1, indent, value1); + FormatLiteral(Value2, indent, value2); } + } - void DiffLiteral(int indent, int value1, int value2) + void DiffLiteral(int indent, int value1, int value2) + { + if (value1 == value2) { - if (value1 == value2) - { - FormatLiteral(Same, indent, value1); - } - else - { - FormatLiteral(Value1, indent, value1); - FormatLiteral(Value2, indent, value2); - } + FormatLiteral(Same, indent, value1); } + else + { + FormatLiteral(Value1, indent, value1); + FormatLiteral(Value2, indent, value2); + } + } - public void FormatType(char prefix, int indent, InsType? type) + public void FormatType(char prefix, int indent, InsType? type) + { + if (type == null) { - if (type == null) - { - return; - } + return; + } - switch (type.Kind) - { - case InsTypeKind.Array: - FormatArray(prefix, indent, (InsArrayType)type); - break; + switch (type.Kind) + { + case InsTypeKind.Array: + FormatArray(prefix, indent, (InsArrayType)type); + break; - case InsTypeKind.ByRef: - FormatByRef(prefix, indent, (InsByRefType)type); - break; + case InsTypeKind.ByRef: + FormatByRef(prefix, indent, (InsByRefType)type); + break; - case InsTypeKind.Generic: - FormatGeneric(prefix, indent, (InsGenericType)type); - break; + case InsTypeKind.Generic: + FormatGeneric(prefix, indent, (InsGenericType)type); + break; - case InsTypeKind.Named: - FormatNamed(prefix, indent, (InsNamedType)type); - break; + case InsTypeKind.Named: + FormatNamed(prefix, indent, (InsNamedType)type); + break; - case InsTypeKind.Pointer: - FormatPointer(prefix, indent, (InsPointerType)type); - break; + case InsTypeKind.Pointer: + FormatPointer(prefix, indent, (InsPointerType)type); + break; - case InsTypeKind.SZArray: - FormatSZArray(prefix, indent, (InsSZArrayType)type); - break; + case InsTypeKind.SZArray: + FormatSZArray(prefix, indent, (InsSZArrayType)type); + break; - default: - throw new ArgumentException("Unknown type kind: " + type.Kind, nameof(type)); - } + default: + throw new ArgumentException("Unknown type kind: " + type.Kind, nameof(type)); } + } - void FormatArray(char prefix, int indent, InsArrayType type) - { - FormatLabel(prefix, indent, "ArrayType"); - - indent++; - FormatType(prefix, indent, type.ElementType); - FormatLiteral(prefix, indent, type.Rank); - } - - void FormatByRef(char prefix, int indent, InsByRefType type) - { - FormatLabel(prefix, indent, "ByRef"); - - indent++; - FormatType(prefix, indent, type.ElementType); - } + void FormatArray(char prefix, int indent, InsArrayType type) + { + FormatLabel(prefix, indent, "ArrayType"); - void FormatGeneric(char prefix, int indent, InsGenericType type) - { - FormatLabel(prefix, indent, "Generic"); + indent++; + FormatType(prefix, indent, type.ElementType); + FormatLiteral(prefix, indent, type.Rank); + } - indent++; - FormatType(prefix, indent, type.Definition); - FormatTypes(prefix, indent, type.TypeArguments); - } + void FormatByRef(char prefix, int indent, InsByRefType type) + { + FormatLabel(prefix, indent, "ByRef"); - void FormatNamed(char prefix, int indent, InsNamedType type) - { - FormatLabel(prefix, indent, "NamedType"); + indent++; + FormatType(prefix, indent, type.ElementType); + } - indent++; - FormatLiteral(prefix, indent, type.Name); - FormatType(prefix, indent, type.DeclaringType); - FormatAssembly(prefix, indent, type.Assembly); - } + void FormatGeneric(char prefix, int indent, InsGenericType type) + { + FormatLabel(prefix, indent, "Generic"); - void FormatPointer(char prefix, int indent, InsPointerType type) - { - FormatLabel(prefix, indent, "Pointer"); + indent++; + FormatType(prefix, indent, type.Definition); + FormatTypes(prefix, indent, type.TypeArguments); + } - indent++; - FormatType(prefix, indent, type.ElementType); - } + void FormatNamed(char prefix, int indent, InsNamedType type) + { + FormatLabel(prefix, indent, "NamedType"); - void FormatSZArray(char prefix, int indent, InsSZArrayType type) - { - FormatLabel(prefix, indent, "SZArrayType"); + indent++; + FormatLiteral(prefix, indent, type.Name); + FormatType(prefix, indent, type.DeclaringType); + FormatAssembly(prefix, indent, type.Assembly); + } - indent++; - FormatType(prefix, indent, type.ElementType); - } + void FormatPointer(char prefix, int indent, InsPointerType type) + { + FormatLabel(prefix, indent, "Pointer"); - void FormatTypes(char prefix, int indent, ImmutableArray types) - { - foreach (var typeArg in types) - { - FormatType(prefix, indent, typeArg); - } - } + indent++; + FormatType(prefix, indent, type.ElementType); + } - void FormatAssembly(char prefix, int indent, InsAssembly? assembly) - { - if (assembly != null) - { - FormatLabel(prefix, indent, "Assembly"); + void FormatSZArray(char prefix, int indent, InsSZArrayType type) + { + FormatLabel(prefix, indent, "SZArrayType"); - indent++; - FormatLiteral(prefix, indent, assembly.Name); - FormatQualifications(prefix, indent, assembly.Qualifications); - } - } + indent++; + FormatType(prefix, indent, type.ElementType); + } - void FormatQualifications(char prefix, int indent, ImmutableArray qualifications) + void FormatTypes(char prefix, int indent, ImmutableArray types) + { + foreach (var typeArg in types) { - foreach (var qualification in qualifications) - { - FormatQualification(prefix, indent, qualification); - } + FormatType(prefix, indent, typeArg); } + } - void FormatQualification(char prefix, int indent, InsAssemblyQualification qualification) + void FormatAssembly(char prefix, int indent, InsAssembly? assembly) + { + if (assembly != null) { - FormatLabel(prefix, indent, "Qualification"); + FormatLabel(prefix, indent, "Assembly"); indent++; - FormatLiteral(prefix, indent, qualification.Name); - FormatLiteral(prefix, indent, qualification.Value); + FormatLiteral(prefix, indent, assembly.Name); + FormatQualifications(prefix, indent, assembly.Qualifications); } + } - void FormatLabel(char prefix, int indent, string label) + void FormatQualifications(char prefix, int indent, ImmutableArray qualifications) + { + foreach (var qualification in qualifications) { - WriteIndent(prefix, indent); - _builder.Append(label).Append(':').AppendLine(); + FormatQualification(prefix, indent, qualification); } + } - void FormatLiteral(char prefix, int indent, string value) - { - WriteIndent(prefix, indent); + void FormatQualification(char prefix, int indent, InsAssemblyQualification qualification) + { + FormatLabel(prefix, indent, "Qualification"); - if (value == null) - { - _builder.Append("null"); - return; - } + indent++; + FormatLiteral(prefix, indent, qualification.Name); + FormatLiteral(prefix, indent, qualification.Value); + } - _builder - .Append('"') - .Append(value) - .Append('"') - .AppendLine(); - } + void FormatLabel(char prefix, int indent, string label) + { + WriteIndent(prefix, indent); + _builder.Append(label).Append(':').AppendLine(); + } + + void FormatLiteral(char prefix, int indent, string value) + { + WriteIndent(prefix, indent); - void FormatLiteral(char prefix, int indent, int value) + if (value == null) { - WriteIndent(prefix, indent); - _builder - .Append(value) - .AppendLine(); + _builder.Append("null"); + return; } - void WriteIndent(char prefix, int indent) - { - if (prefix != '\0') - { - _builder.Append(prefix); - } + _builder + .Append('"') + .Append(value) + .Append('"') + .AppendLine(); + } - _builder.Append(' ', indent * 2); + void FormatLiteral(char prefix, int indent, int value) + { + WriteIndent(prefix, indent); + _builder + .Append(value) + .AppendLine(); + } + + void WriteIndent(char prefix, int indent) + { + if (prefix != '\0') + { + _builder.Append(prefix); } - readonly StringBuilder _builder = builder; + _builder.Append(' ', indent * 2); } + + readonly StringBuilder _builder = builder; } } diff --git a/src/TypeNameInterpretation/BuilderPool.cs b/src/TypeNameInterpretation/BuilderPool.cs index abb285a..f5ca1c9 100644 --- a/src/TypeNameInterpretation/BuilderPool.cs +++ b/src/TypeNameInterpretation/BuilderPool.cs @@ -2,57 +2,56 @@ using System; using System.Text; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +static class BuilderPool { - static class BuilderPool + public static StringBuilder Rent() + { + var result = _cache ?? new StringBuilder(); + _cache = null; + return result; + } + + public static StringBuilder Rent(int capacity) { - public static StringBuilder Rent() + var result = _cache; + _cache = null; + + if (result == null) { - var result = _cache ?? new StringBuilder(); - _cache = null; - return result; + return new StringBuilder(capacity); } - - public static StringBuilder Rent(int capacity) + else { - var result = _cache; - _cache = null; - - if (result == null) - { - return new StringBuilder(capacity); - } - else - { - result.EnsureCapacity(capacity); - return result; - } + result.EnsureCapacity(capacity); + return result; } + } - public static void Return(StringBuilder builder) + public static void Return(StringBuilder builder) + { + if (builder.Capacity > 512) { - if (builder.Capacity > 512) - { - return; - } - - var existing = _cache; - - if (existing == null || existing.Capacity < builder.Capacity) - { - builder.Length = 0; - _cache = builder; - } + return; } - public static string ToStringAndReturn(this StringBuilder builder) + var existing = _cache; + + if (existing == null || existing.Capacity < builder.Capacity) { - var result = builder.ToString(); - Return(builder); - return result; + builder.Length = 0; + _cache = builder; } + } - [ThreadStatic] - static StringBuilder? _cache; + public static string ToStringAndReturn(this StringBuilder builder) + { + var result = builder.ToString(); + Return(builder); + return result; } + + [ThreadStatic] + static StringBuilder? _cache; } diff --git a/src/TypeNameInterpretation/Delimiters.cs b/src/TypeNameInterpretation/Delimiters.cs index ecd1b65..9ff9d43 100644 --- a/src/TypeNameInterpretation/Delimiters.cs +++ b/src/TypeNameInterpretation/Delimiters.cs @@ -2,18 +2,17 @@ using System; using System.Buffers; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +static class Delimiters { - static class Delimiters - { #if NET - public static SearchValues All { get; } = SearchValues.Create(_allDelimiterChars); - public static SearchValues Quote { get; } = SearchValues.Create(_allDelimiterChars.AsSpan(0, 2)); + public static SearchValues All { get; } = SearchValues.Create(_allDelimiterChars); + public static SearchValues Quote { get; } = SearchValues.Create(_allDelimiterChars.AsSpan(0, 2)); #else - public static ReadOnlySpan All => _allDelimiterChars.AsSpan(); - public static ReadOnlySpan Quote => _allDelimiterChars.AsSpan(0, 2); + public static ReadOnlySpan All => _allDelimiterChars.AsSpan(); + public static ReadOnlySpan Quote => _allDelimiterChars.AsSpan(0, 2); #endif - const string _allDelimiterChars = "\\\"[],+&*="; - } + const string _allDelimiterChars = "\\\"[],+&*="; } diff --git a/src/TypeNameInterpretation/FrameworkExtensions/ArgumentNullExceptionShims.cs b/src/TypeNameInterpretation/FrameworkExtensions/ArgumentNullExceptionShims.cs index 0193def..4517b3d 100644 --- a/src/TypeNameInterpretation/FrameworkExtensions/ArgumentNullExceptionShims.cs +++ b/src/TypeNameInterpretation/FrameworkExtensions/ArgumentNullExceptionShims.cs @@ -4,23 +4,22 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -namespace System +namespace System; + +static class ArgumentNullExceptionShims { - static class ArgumentNullExceptionShims + extension(ArgumentNullException) { - extension(ArgumentNullException) + public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) { - public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) + if (argument is null) { - if (argument is null) - { - ThrowNullReferenceException(paramName); - } + ThrowNullReferenceException(paramName); } } - - [DoesNotReturn] - static void ThrowNullReferenceException(string? paramName) => throw new ArgumentNullException(paramName); } + + [DoesNotReturn] + static void ThrowNullReferenceException(string? paramName) => throw new ArgumentNullException(paramName); } #endif diff --git a/src/TypeNameInterpretation/FrameworkExtensions/Attributes/CallerArgumentExpressionAttribute.cs b/src/TypeNameInterpretation/FrameworkExtensions/Attributes/CallerArgumentExpressionAttribute.cs index d29c22c..e4e4dde 100644 --- a/src/TypeNameInterpretation/FrameworkExtensions/Attributes/CallerArgumentExpressionAttribute.cs +++ b/src/TypeNameInterpretation/FrameworkExtensions/Attributes/CallerArgumentExpressionAttribute.cs @@ -1,12 +1,11 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. #if !NET -namespace System.Runtime.CompilerServices +namespace System.Runtime.CompilerServices; + +[AttributeUsage(AttributeTargets.Parameter)] +sealed class CallerArgumentExpressionAttribute(string parameterName) : Attribute { - [AttributeUsage(AttributeTargets.Parameter)] - sealed class CallerArgumentExpressionAttribute(string parameterName) : Attribute - { - public string ParameterName { get; } = parameterName; - } + public string ParameterName { get; } = parameterName; } #endif diff --git a/src/TypeNameInterpretation/FrameworkExtensions/StringBuilderExtensions.cs b/src/TypeNameInterpretation/FrameworkExtensions/StringBuilderExtensions.cs index bb65619..2cbe972 100644 --- a/src/TypeNameInterpretation/FrameworkExtensions/StringBuilderExtensions.cs +++ b/src/TypeNameInterpretation/FrameworkExtensions/StringBuilderExtensions.cs @@ -1,18 +1,17 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. #if !NETSTANDARD2_1_OR_GREATER && !NET -namespace System.Text +namespace System.Text; + +static class StringBuilderExtensions { - static class StringBuilderExtensions + public static unsafe StringBuilder Append(this StringBuilder builder, ReadOnlySpan text) { - public static unsafe StringBuilder Append(this StringBuilder builder, ReadOnlySpan text) + fixed (char* c = text) { - fixed (char* c = text) - { - builder.Append(c, text.Length); - } - - return builder; + builder.Append(c, text.Length); } + + return builder; } } #endif diff --git a/src/TypeNameInterpretation/InsAssemblyExtensions.cs b/src/TypeNameInterpretation/InsAssemblyExtensions.cs index 829755f..8a4b21d 100644 --- a/src/TypeNameInterpretation/InsAssemblyExtensions.cs +++ b/src/TypeNameInterpretation/InsAssemblyExtensions.cs @@ -4,312 +4,311 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public static class InsAssemblyExtensions { - public static class InsAssemblyExtensions + public static bool TryGetVersion(this InsAssembly assembly, [NotNullWhen(true)] out Version? version) { - public static bool TryGetVersion(this InsAssembly assembly, [NotNullWhen(true)] out Version? version) - { - ArgumentNullException.ThrowIfNull(assembly); - - if (!assembly.TryGetQualification(WellKnownQualificationNames.Version, out var value)) - { - version = null; - return false; - } + ArgumentNullException.ThrowIfNull(assembly); - if (!Version.TryParse(value, out version)) - { - throw new FormatException("Version qualification was provided, but was in an unrecognised format."); - } - - return true; + if (!assembly.TryGetQualification(WellKnownQualificationNames.Version, out var value)) + { + version = null; + return false; } - public static bool TryGetPublicKey(this InsAssembly assembly, out byte[]? publicKey) + if (!Version.TryParse(value, out version)) { - ArgumentNullException.ThrowIfNull(assembly); + throw new FormatException("Version qualification was provided, but was in an unrecognised format."); + } - if (!assembly.TryGetQualification(WellKnownQualificationNames.PublicKey, out var value)) - { - publicKey = null; - return false; - } + return true; + } - if (!TryParseBlob(value, out publicKey)) - { - throw new FormatException("PublicKey qualification was provided, but was in an unrecognised format."); - } + public static bool TryGetPublicKey(this InsAssembly assembly, out byte[]? publicKey) + { + ArgumentNullException.ThrowIfNull(assembly); - return true; + if (!assembly.TryGetQualification(WellKnownQualificationNames.PublicKey, out var value)) + { + publicKey = null; + return false; } - public static bool TryGetPublicKeyToken(this InsAssembly assembly, out byte[]? publicKeyToken) + if (!TryParseBlob(value, out publicKey)) { - ArgumentNullException.ThrowIfNull(assembly); + throw new FormatException("PublicKey qualification was provided, but was in an unrecognised format."); + } - if (!assembly.TryGetQualification(WellKnownQualificationNames.PublicKeyToken, out var value)) - { - publicKeyToken = null; - return false; - } + return true; + } - if (!TryParseBlob(value, out publicKeyToken)) - { - throw new FormatException("PublicKeyToken qualification was provided, but was in an unrecognised format."); - } + public static bool TryGetPublicKeyToken(this InsAssembly assembly, out byte[]? publicKeyToken) + { + ArgumentNullException.ThrowIfNull(assembly); - return true; + if (!assembly.TryGetQualification(WellKnownQualificationNames.PublicKeyToken, out var value)) + { + publicKeyToken = null; + return false; } - public static bool TryGetProcessorArchitecture(this InsAssembly assembly, out ProcessorArchitecture processorArchitecture) + if (!TryParseBlob(value, out publicKeyToken)) { - ArgumentNullException.ThrowIfNull(assembly); - - if (!assembly.TryGetQualification(WellKnownQualificationNames.ProcessorArchitecture, out var value)) - { - processorArchitecture = default; - return false; - } - - if (!Enum.TryParse(value, out processorArchitecture)) - { - throw new FormatException("ProcessorArchitecture qualification was provided, but was unrecognised."); - } - - return true; + throw new FormatException("PublicKeyToken qualification was provided, but was in an unrecognised format."); } - public static bool TryGetQualification(this InsAssembly assembly, string name, [NotNullWhen(true)] out string? value) - { - ArgumentNullException.ThrowIfNull(assembly); - ArgumentNullException.ThrowIfNull(name); + return true; + } - foreach (var qualification in assembly.Qualifications) - { - if (qualification.Name == name) - { - value = qualification.Value; - return true; - } - } + public static bool TryGetProcessorArchitecture(this InsAssembly assembly, out ProcessorArchitecture processorArchitecture) + { + ArgumentNullException.ThrowIfNull(assembly); - value = null; + if (!assembly.TryGetQualification(WellKnownQualificationNames.ProcessorArchitecture, out var value)) + { + processorArchitecture = default; return false; } - public static InsAssembly WithVersion(this InsAssembly assembly, Version version) + if (!Enum.TryParse(value, out processorArchitecture)) { - ArgumentNullException.ThrowIfNull(assembly); - ArgumentNullException.ThrowIfNull(version); - return assembly.WithQualification(WellKnownQualificationNames.Version, version.ToString()); + throw new FormatException("ProcessorArchitecture qualification was provided, but was unrecognised."); } - public static InsAssembly WithPublicKey(this InsAssembly assembly, ReadOnlySpan publicKey) - { - ArgumentNullException.ThrowIfNull(assembly); - return assembly.WithQualification(WellKnownQualificationNames.PublicKey, FormatBlob(publicKey)); - } + return true; + } - public static InsAssembly WithPublicKey(this InsAssembly assembly, byte[]? publicKey) - { - ArgumentNullException.ThrowIfNull(assembly); + public static bool TryGetQualification(this InsAssembly assembly, string name, [NotNullWhen(true)] out string? value) + { + ArgumentNullException.ThrowIfNull(assembly); + ArgumentNullException.ThrowIfNull(name); - if (publicKey == null) - { - return assembly.WithQualification(WellKnownQualificationNames.PublicKey, NullBlob); - } - else + foreach (var qualification in assembly.Qualifications) + { + if (qualification.Name == name) { - return assembly.WithPublicKey(publicKey.AsSpan()); + value = qualification.Value; + return true; } } - public static InsAssembly WithPublicKeyToken(this InsAssembly assembly, ReadOnlySpan publicKeyToken) - { - ArgumentNullException.ThrowIfNull(assembly); - return assembly.WithQualification(WellKnownQualificationNames.PublicKeyToken, FormatBlob(publicKeyToken)); - } + value = null; + return false; + } - public static InsAssembly WithPublicKeyToken(this InsAssembly assembly, byte[]? publicKeyToken) - { - ArgumentNullException.ThrowIfNull(assembly); + public static InsAssembly WithVersion(this InsAssembly assembly, Version version) + { + ArgumentNullException.ThrowIfNull(assembly); + ArgumentNullException.ThrowIfNull(version); + return assembly.WithQualification(WellKnownQualificationNames.Version, version.ToString()); + } - if (publicKeyToken == null) - { - return assembly.WithQualification(WellKnownQualificationNames.PublicKeyToken, NullBlob); - } - else - { - return assembly.WithPublicKeyToken(publicKeyToken.AsSpan()); - } - } + public static InsAssembly WithPublicKey(this InsAssembly assembly, ReadOnlySpan publicKey) + { + ArgumentNullException.ThrowIfNull(assembly); + return assembly.WithQualification(WellKnownQualificationNames.PublicKey, FormatBlob(publicKey)); + } + + public static InsAssembly WithPublicKey(this InsAssembly assembly, byte[]? publicKey) + { + ArgumentNullException.ThrowIfNull(assembly); - public static InsAssembly WithProcessorArchitecture(this InsAssembly assembly, ProcessorArchitecture processorArchitecture) + if (publicKey == null) { - ArgumentNullException.ThrowIfNull(assembly); - return assembly.WithQualification(WellKnownQualificationNames.ProcessorArchitecture, processorArchitecture.ToString()); + return assembly.WithQualification(WellKnownQualificationNames.PublicKey, NullBlob); } - - public static InsAssembly WithQualification(this InsAssembly assembly, string name, string value) + else { - ArgumentNullException.ThrowIfNull(assembly); - ArgumentNullException.ThrowIfNull(name); - return assembly.WithQualifications(assembly.Qualifications.WithQualification(name, value)); + return assembly.WithPublicKey(publicKey.AsSpan()); } + } + + public static InsAssembly WithPublicKeyToken(this InsAssembly assembly, ReadOnlySpan publicKeyToken) + { + ArgumentNullException.ThrowIfNull(assembly); + return assembly.WithQualification(WellKnownQualificationNames.PublicKeyToken, FormatBlob(publicKeyToken)); + } - public static InsAssembly WithoutQualification(this InsAssembly assembly, string name) + public static InsAssembly WithPublicKeyToken(this InsAssembly assembly, byte[]? publicKeyToken) + { + ArgumentNullException.ThrowIfNull(assembly); + + if (publicKeyToken == null) { - ArgumentNullException.ThrowIfNull(assembly); - ArgumentNullException.ThrowIfNull(name); - return assembly.WithQualifications(assembly.Qualifications.WithoutQualification(name)); + return assembly.WithQualification(WellKnownQualificationNames.PublicKeyToken, NullBlob); } - - static InsAssembly WithQualifications(this InsAssembly assembly, ImmutableArray newQualifications) + else { - if (newQualifications == assembly.Qualifications) - { - return assembly; - } - - return new InsAssembly(assembly.Name, newQualifications); + return assembly.WithPublicKeyToken(publicKeyToken.AsSpan()); } + } - static ImmutableArray WithQualification(this ImmutableArray qualifications, string name, string value) - { - for (var i = 0; i < qualifications.Length; i++) - { - var qualification = qualifications[i]; + public static InsAssembly WithProcessorArchitecture(this InsAssembly assembly, ProcessorArchitecture processorArchitecture) + { + ArgumentNullException.ThrowIfNull(assembly); + return assembly.WithQualification(WellKnownQualificationNames.ProcessorArchitecture, processorArchitecture.ToString()); + } - if (qualification.Name == name) - { - if (qualification.Value == value) - { - return qualifications; - } + public static InsAssembly WithQualification(this InsAssembly assembly, string name, string value) + { + ArgumentNullException.ThrowIfNull(assembly); + ArgumentNullException.ThrowIfNull(name); + return assembly.WithQualifications(assembly.Qualifications.WithQualification(name, value)); + } - return qualifications.SetItem(i, new InsAssemblyQualification(name, value)); - } - } + public static InsAssembly WithoutQualification(this InsAssembly assembly, string name) + { + ArgumentNullException.ThrowIfNull(assembly); + ArgumentNullException.ThrowIfNull(name); + return assembly.WithQualifications(assembly.Qualifications.WithoutQualification(name)); + } - return qualifications.Add(new InsAssemblyQualification(name, value)); + static InsAssembly WithQualifications(this InsAssembly assembly, ImmutableArray newQualifications) + { + if (newQualifications == assembly.Qualifications) + { + return assembly; } - static ImmutableArray WithoutQualification(this ImmutableArray qualifications, string name) + return new InsAssembly(assembly.Name, newQualifications); + } + + static ImmutableArray WithQualification(this ImmutableArray qualifications, string name, string value) + { + for (var i = 0; i < qualifications.Length; i++) { - for (var i = 0; i < qualifications.Length; i++) + var qualification = qualifications[i]; + + if (qualification.Name == name) { - if (qualifications[i].Name == name) + if (qualification.Value == value) { - return qualifications.RemoveAt(i); + return qualifications; } - } - return qualifications; + return qualifications.SetItem(i, new InsAssemblyQualification(name, value)); + } } - static bool TryParseBlob(string value, out byte[]? blob) + return qualifications.Add(new InsAssemblyQualification(name, value)); + } + + static ImmutableArray WithoutQualification(this ImmutableArray qualifications, string name) + { + for (var i = 0; i < qualifications.Length; i++) { - if (value.Length == 0) + if (qualifications[i].Name == name) { - blob = Array.Empty(); - return true; - } - else if (value == NullBlob) - { - blob = null; - return true; + return qualifications.RemoveAt(i); } - else if ((value.Length & 1) == 1) + } + + return qualifications; + } + + static bool TryParseBlob(string value, out byte[]? blob) + { + if (value.Length == 0) + { + blob = Array.Empty(); + return true; + } + else if (value == NullBlob) + { + blob = null; + return true; + } + else if ((value.Length & 1) == 1) + { + blob = null; + return false; + } + + var result = new byte[value.Length >> 1]; + +#if NET9_0_OR_GREATER + if (Convert.FromHexString(value, result, out _, out _) != System.Buffers.OperationStatus.Done) + { + blob = null; + return false; + } +#else + for (var i = 0; i < result.Length; i++) + { + var baseIndex = i << 1; + var n1 = CharValue(value[baseIndex]); + + if (n1 < 0) { blob = null; return false; } - var result = new byte[value.Length >> 1]; + var n2 = CharValue(value[baseIndex + 1]); -#if NET9_0_OR_GREATER - if (Convert.FromHexString(value, result, out _, out _) != System.Buffers.OperationStatus.Done) + if (n2 < 0) { blob = null; return false; } -#else - for (var i = 0; i < result.Length; i++) - { - var baseIndex = i << 1; - var n1 = CharValue(value[baseIndex]); - - if (n1 < 0) - { - blob = null; - return false; - } - - var n2 = CharValue(value[baseIndex + 1]); - - if (n2 < 0) - { - blob = null; - return false; - } - result[i] = (byte)((n1 << 4) | n2); - } + result[i] = (byte)((n1 << 4) | n2); + } #endif - blob = result; - return true; - } + blob = result; + return true; + } - static string FormatBlob(ReadOnlySpan blob) - { + static string FormatBlob(ReadOnlySpan blob) + { #if NET - return Convert.ToHexString(blob); + return Convert.ToHexString(blob); #else - if (blob.Length == 0) - { - return string.Empty; - } + if (blob.Length == 0) + { + return string.Empty; + } - var builder = BuilderPool.Rent(blob.Length * 2); - var charLookup = "0123456789ABCDEF"; + var builder = BuilderPool.Rent(blob.Length * 2); + var charLookup = "0123456789ABCDEF"; - for (var i = 0; i < blob.Length; i++) - { - var b = blob[i]; + for (var i = 0; i < blob.Length; i++) + { + var b = blob[i]; - builder - .Append(charLookup[b >> 4]) - .Append(charLookup[b & 0xF]); - } + builder + .Append(charLookup[b >> 4]) + .Append(charLookup[b & 0xF]); + } - return builder.ToStringAndReturn(); + return builder.ToStringAndReturn(); #endif - } + } #if !NET9_0_OR_GREATER - static int CharValue(char c) + static int CharValue(char c) + { + if (c >= '0' && c <= '9') { - if (c >= '0' && c <= '9') - { - return c - '0'; - } - else if (c >= 'a' && c <= 'f') - { - return c - 'a' + 10; - } - else if (c >= 'A' && c <= 'F') - { - return c - 'A' + 10; - } - else - { - return -1; - } + return c - '0'; } + else if (c >= 'a' && c <= 'f') + { + return c - 'a' + 10; + } + else if (c >= 'A' && c <= 'F') + { + return c - 'A' + 10; + } + else + { + return -1; + } + } #endif - const string NullBlob = "null"; - } + const string NullBlob = "null"; } diff --git a/src/TypeNameInterpretation/InsFormatter.cs b/src/TypeNameInterpretation/InsFormatter.cs index ae61018..6aa54d6 100644 --- a/src/TypeNameInterpretation/InsFormatter.cs +++ b/src/TypeNameInterpretation/InsFormatter.cs @@ -3,218 +3,217 @@ using System.Buffers; using System.Text; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public static class InsFormatter { - public static class InsFormatter + public static StringBuilder Write(StringBuilder builder, InsType type) { - public static StringBuilder Write(StringBuilder builder, InsType type) - { - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(type); - return Writer.Instance.WriteComplexType(type, builder); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(type); + return Writer.Instance.WriteComplexType(type, builder); + } + + public static StringBuilder Write(StringBuilder builder, InsAssembly assembly) + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(assembly); + return Writer.WriteAssembly(assembly, builder); + } + + public static string Format(InsType type) + { + ArgumentNullException.ThrowIfNull(type); + return Write(BuilderPool.Rent(), type).ToStringAndReturn(); + } - public static StringBuilder Write(StringBuilder builder, InsAssembly assembly) + public static string Format(InsAssembly assembly) + { + ArgumentNullException.ThrowIfNull(assembly); + + if (assembly.Qualifications.Length == 0 && assembly.Name.AsSpan().IndexOfAny(Delimiters.All) < 0) { - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(assembly); - return Writer.WriteAssembly(assembly, builder); + return assembly.Name; } - public static string Format(InsType type) + return Write(BuilderPool.Rent(), assembly).ToStringAndReturn(); + } + + sealed class Writer : IInsTypeVisitor + { + public static Writer Instance { get; } = new Writer(); + + Writer() { - ArgumentNullException.ThrowIfNull(type); - return Write(BuilderPool.Rent(), type).ToStringAndReturn(); } - public static string Format(InsAssembly assembly) + public StringBuilder WriteComplexType(InsType type, StringBuilder builder) { - ArgumentNullException.ThrowIfNull(assembly); + type.Apply(this, builder); + + var assembly = type.Apply(AssemblyLocator.Instance, builder); - if (assembly.Qualifications.Length == 0 && assembly.Name.AsSpan().IndexOfAny(Delimiters.All) < 0) + if (assembly != null) { - return assembly.Name; + builder.Append(", "); + WriteAssembly(assembly, builder); } - return Write(BuilderPool.Rent(), assembly).ToStringAndReturn(); + return builder; } - sealed class Writer : IInsTypeVisitor + public StringBuilder VisitArray(InsArrayType type, StringBuilder builder) { - public static Writer Instance { get; } = new Writer(); + type.ElementType.Apply(this, builder).Append('['); - Writer() + if (type.Rank == 1) { + builder.Append('*'); } - - public StringBuilder WriteComplexType(InsType type, StringBuilder builder) + else { - type.Apply(this, builder); + builder.Append(',', type.Rank - 1); + } - var assembly = type.Apply(AssemblyLocator.Instance, builder); + return builder.Append(']'); + } - if (assembly != null) - { - builder.Append(", "); - WriteAssembly(assembly, builder); - } + public StringBuilder VisitByRef(InsByRefType type, StringBuilder builder) + => type.ElementType.Apply(this, builder).Append('&'); - return builder; - } + public StringBuilder VisitGeneric(InsGenericType type, StringBuilder builder) + { + type.Definition.Apply(this, builder); - public StringBuilder VisitArray(InsArrayType type, StringBuilder builder) + var typeArguments = type.TypeArguments; + + if (typeArguments.Length > 0) { - type.ElementType.Apply(this, builder).Append('['); + builder.Append("[["); + WriteComplexType(typeArguments[0], builder); - if (type.Rank == 1) + for (var i = 1; i < typeArguments.Length; i++) { - builder.Append('*'); - } - else - { - builder.Append(',', type.Rank - 1); + builder.Append("],["); + WriteComplexType(typeArguments[i], builder); } - return builder.Append(']'); + builder.Append("]]"); } - public StringBuilder VisitByRef(InsByRefType type, StringBuilder builder) - => type.ElementType.Apply(this, builder).Append('&'); + return builder; + } - public StringBuilder VisitGeneric(InsGenericType type, StringBuilder builder) + public StringBuilder VisitNamed(InsNamedType type, StringBuilder builder) + { + if (type.DeclaringType != null) { - type.Definition.Apply(this, builder); - - var typeArguments = type.TypeArguments; - - if (typeArguments.Length > 0) - { - builder.Append("[["); - WriteComplexType(typeArguments[0], builder); - - for (var i = 1; i < typeArguments.Length; i++) - { - builder.Append("],["); - WriteComplexType(typeArguments[i], builder); - } - - builder.Append("]]"); - } - - return builder; + type.DeclaringType.Apply(this, builder); + builder.Append('+'); } - public StringBuilder VisitNamed(InsNamedType type, StringBuilder builder) - { - if (type.DeclaringType != null) - { - type.DeclaringType.Apply(this, builder); - builder.Append('+'); - } + WriteIdentifier(type.Name, builder); - WriteIdentifier(type.Name, builder); + return builder; + } - return builder; - } + public StringBuilder VisitPointer(InsPointerType type, StringBuilder builder) + => type.ElementType.Apply(this, builder).Append('*'); - public StringBuilder VisitPointer(InsPointerType type, StringBuilder builder) - => type.ElementType.Apply(this, builder).Append('*'); + public StringBuilder VisitSZArray(InsSZArrayType type, StringBuilder argument) + => type.ElementType.Apply(this, argument).Append("[]"); - public StringBuilder VisitSZArray(InsSZArrayType type, StringBuilder argument) - => type.ElementType.Apply(this, argument).Append("[]"); + public static StringBuilder WriteAssembly(InsAssembly assembly, StringBuilder builder) + { + WriteIdentifier(assembly.Name, builder); - public static StringBuilder WriteAssembly(InsAssembly assembly, StringBuilder builder) + foreach (var qualification in assembly.Qualifications) { - WriteIdentifier(assembly.Name, builder); + builder.Append(", "); + WriteIdentifier(qualification.Name, builder); + builder.Append('='); - foreach (var qualification in assembly.Qualifications) + if (RequiresQuoting(qualification.Value)) { - builder.Append(", "); - WriteIdentifier(qualification.Name, builder); - builder.Append('='); - - if (RequiresQuoting(qualification.Value)) - { - WriteQuotedIdentifier(qualification.Value, builder); - } - else - { - WriteIdentifier(qualification.Value, builder); - } + WriteQuotedIdentifier(qualification.Value, builder); + } + else + { + WriteIdentifier(qualification.Value, builder); } - - return builder; } - static void WriteIdentifier(string identifier, StringBuilder builder) - { - WriteWithEscapedDelimiters(builder, identifier, Delimiters.All); - } + return builder; + } - static void WriteQuotedIdentifier(string identifier, StringBuilder builder) - { - builder.Append('"'); - WriteWithEscapedDelimiters(builder, identifier, Delimiters.Quote); - builder.Append('"'); - } + static void WriteIdentifier(string identifier, StringBuilder builder) + { + WriteWithEscapedDelimiters(builder, identifier, Delimiters.All); + } + + static void WriteQuotedIdentifier(string identifier, StringBuilder builder) + { + builder.Append('"'); + WriteWithEscapedDelimiters(builder, identifier, Delimiters.Quote); + builder.Append('"'); + } #if NET - static void WriteWithEscapedDelimiters(StringBuilder builder, string identifier, SearchValues delimiters) + static void WriteWithEscapedDelimiters(StringBuilder builder, string identifier, SearchValues delimiters) #else - static void WriteWithEscapedDelimiters(StringBuilder builder, string identifier, ReadOnlySpan delimiters) + static void WriteWithEscapedDelimiters(StringBuilder builder, string identifier, ReadOnlySpan delimiters) #endif + { + var start = 0; + + while (start < identifier.Length) { - var start = 0; + var remaining = identifier.AsSpan(start); + + var index = remaining.IndexOfAny(delimiters); - while (start < identifier.Length) + if (index < 0) { - var remaining = identifier.AsSpan(start); + builder.Append(remaining); + return; + } - var index = remaining.IndexOfAny(delimiters); + builder + .Append(remaining.Slice(0, index)) + .Append('\\') + .Append(remaining[index]); - if (index < 0) - { - builder.Append(remaining); - return; - } + start = start + index + 1; + } + } - builder - .Append(remaining.Slice(0, index)) - .Append('\\') - .Append(remaining[index]); + static bool RequiresQuoting(string identifier) + => string.IsNullOrEmpty(identifier) || identifier.AsSpan().IndexOfAny(Delimiters.All) >= 0; + } - start = start + index + 1; - } - } + sealed class AssemblyLocator : IInsTypeVisitor + { + public static AssemblyLocator Instance { get; } = new AssemblyLocator(); - static bool RequiresQuoting(string identifier) - => string.IsNullOrEmpty(identifier) || identifier.AsSpan().IndexOfAny(Delimiters.All) >= 0; + AssemblyLocator() + { } - sealed class AssemblyLocator : IInsTypeVisitor - { - public static AssemblyLocator Instance { get; } = new AssemblyLocator(); + public InsAssembly? VisitArray(InsArrayType type, object argument) => type.ElementType.Apply(this, argument); + public InsAssembly? VisitByRef(InsByRefType type, object argument) => type.ElementType.Apply(this, argument); + public InsAssembly? VisitGeneric(InsGenericType type, object argument) => AssemblyFromNamed(type.Definition); + public InsAssembly? VisitNamed(InsNamedType type, object argument) => AssemblyFromNamed(type); + public InsAssembly? VisitPointer(InsPointerType type, object argument) => type.ElementType.Apply(this, argument); + public InsAssembly? VisitSZArray(InsSZArrayType type, object argument) => type.ElementType.Apply(this, argument); - AssemblyLocator() + static InsAssembly? AssemblyFromNamed(InsNamedType type) + { + while (type.DeclaringType != null) { + type = type.DeclaringType; } - public InsAssembly? VisitArray(InsArrayType type, object argument) => type.ElementType.Apply(this, argument); - public InsAssembly? VisitByRef(InsByRefType type, object argument) => type.ElementType.Apply(this, argument); - public InsAssembly? VisitGeneric(InsGenericType type, object argument) => AssemblyFromNamed(type.Definition); - public InsAssembly? VisitNamed(InsNamedType type, object argument) => AssemblyFromNamed(type); - public InsAssembly? VisitPointer(InsPointerType type, object argument) => type.ElementType.Apply(this, argument); - public InsAssembly? VisitSZArray(InsSZArrayType type, object argument) => type.ElementType.Apply(this, argument); - - static InsAssembly? AssemblyFromNamed(InsNamedType type) - { - while (type.DeclaringType != null) - { - type = type.DeclaringType; - } - - return type.Assembly; - } + return type.Assembly; } } } diff --git a/src/TypeNameInterpretation/InsParser.cs b/src/TypeNameInterpretation/InsParser.cs index 0b58461..5de6cee 100644 --- a/src/TypeNameInterpretation/InsParser.cs +++ b/src/TypeNameInterpretation/InsParser.cs @@ -4,364 +4,363 @@ using System.Collections.Immutable; using System.Text; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +static class InsParser { - static class InsParser + public static InsType ParseType(ReadOnlySpan value) { - public static InsType ParseType(ReadOnlySpan value) + var index = 0; + var result = new Context(value).ParseQualified(ref index); + + if (index != value.Length) { - var index = 0; - var result = new Context(value).ParseQualified(ref index); + ThrowUnexpected(index); + } - if (index != value.Length) - { - ThrowUnexpected(index); - } + return result; + } - return result; + public static InsAssembly ParseAssembly(ReadOnlySpan value) + { + var index = 0; + var result = new Context(value).ParseAssembly(ref index); + + if (index != value.Length) + { + ThrowUnexpected(index); } - public static InsAssembly ParseAssembly(ReadOnlySpan value) + return result; + } + + static void ThrowEOF() => throw new InvalidTypeNameException("Unexpected end of format."); + static void ThrowUnexpected(int index) => throw new InvalidTypeNameException("Unexpected char at position " + index + "."); + + readonly ref struct Context(ReadOnlySpan buffer) + { + public InsType ParseQualified(ref int index) { - var index = 0; - var result = new Context(value).ParseAssembly(ref index); + var assemblyStart = LocateStartOfAssembly(index); - if (index != value.Length) + if (assemblyStart < 0) { - ThrowUnexpected(index); + return ParseUnqualified(ref index, null); } - return result; - } + var assemblyEnd = assemblyStart + 1; + DiscardWhitespace(ref assemblyEnd); + var assembly = ParseAssembly(ref assemblyEnd); - static void ThrowEOF() => throw new InvalidTypeNameException("Unexpected end of format."); - static void ThrowUnexpected(int index) => throw new InvalidTypeNameException("Unexpected char at position " + index + "."); + var result = ParseUnqualified(ref index, assembly); - readonly ref struct Context(ReadOnlySpan buffer) - { - public InsType ParseQualified(ref int index) + if (index != assemblyStart) { - var assemblyStart = LocateStartOfAssembly(index); + ThrowUnexpected(index); + } - if (assemblyStart < 0) - { - return ParseUnqualified(ref index, null); - } + index = assemblyEnd; + return result; + } - var assemblyEnd = assemblyStart + 1; - DiscardWhitespace(ref assemblyEnd); - var assembly = ParseAssembly(ref assemblyEnd); + InsType ParseUnqualified(ref int index, InsAssembly? assembly) + { + var identifier = ParseIdentifier(ref index); + var baseType = new InsNamedType(identifier, assembly); - var result = ParseUnqualified(ref index, assembly); + while (TryReadChar(ref index, '+')) + { + AssertNotEOF(index); + identifier = ParseIdentifier(ref index); + baseType = new InsNamedType(identifier, baseType); + } - if (index != assemblyStart) - { - ThrowUnexpected(index); - } + InsType type; - index = assemblyEnd; - return result; + if (index + 1 < _buffer.Length && + _buffer[index] == '[' && + _buffer[index + 1] is not ']' and not ',' and not '*') + { + var typeArguments = ParseTypeArguments(ref index); + type = new InsGenericType(baseType, typeArguments); + } + else + { + type = baseType; } - InsType ParseUnqualified(ref int index, InsAssembly? assembly) + while (index < _buffer.Length) { - var identifier = ParseIdentifier(ref index); - var baseType = new InsNamedType(identifier, assembly); + var c = _buffer[index]; - while (TryReadChar(ref index, '+')) + if (c == '*') { - AssertNotEOF(index); - identifier = ParseIdentifier(ref index); - baseType = new InsNamedType(identifier, baseType); + type = new InsPointerType(type); + index++; } - - InsType type; - - if (index + 1 < _buffer.Length && - _buffer[index] == '[' && - _buffer[index + 1] is not ']' and not ',' and not '*') + else if (c == '[') { - var typeArguments = ParseTypeArguments(ref index); - type = new InsGenericType(baseType, typeArguments); + type = ParseArrayDetails(ref index, type); } else { - type = baseType; + break; } + } - while (index < _buffer.Length) - { - var c = _buffer[index]; - - if (c == '*') - { - type = new InsPointerType(type); - index++; - } - else if (c == '[') - { - type = ParseArrayDetails(ref index, type); - } - else - { - break; - } - } + if (TryReadChar(ref index, '&')) + { + type = new InsByRefType(type); + } - if (TryReadChar(ref index, '&')) - { - type = new InsByRefType(type); - } + return type; + } - return type; - } + InsType ParseArrayDetails(ref int index, InsType elementType) + { + index++; - InsType ParseArrayDetails(ref int index, InsType elementType) + if (TryReadChar(ref index, ']')) { - index++; + return new InsSZArrayType(elementType); + } - if (TryReadChar(ref index, ']')) - { - return new InsSZArrayType(elementType); - } + var rank = 1; + ParseArrayRank(ref index); - var rank = 1; + while (TryReadChar(ref index, ',')) + { + rank++; ParseArrayRank(ref index); + } - while (TryReadChar(ref index, ',')) - { - rank++; - ParseArrayRank(ref index); - } + ReadChar(ref index, ']'); + return new InsArrayType(elementType, rank); + } - ReadChar(ref index, ']'); - return new InsArrayType(elementType, rank); - } + void ParseArrayRank(ref int index) + { + TryReadChar(ref index, '*'); + } - void ParseArrayRank(ref int index) - { - TryReadChar(ref index, '*'); - } + ImmutableArray ParseTypeArguments(ref int index) + { + ReadChar(ref index, '['); + var firstType = ParseTypeArgument(ref index); - ImmutableArray ParseTypeArguments(ref int index) + if (TryReadChar(ref index, ']')) { - ReadChar(ref index, '['); - var firstType = ParseTypeArgument(ref index); + return ImmutableArray.Create(firstType); + } - if (TryReadChar(ref index, ']')) - { - return ImmutableArray.Create(firstType); - } + ReadChar(ref index, ','); - ReadChar(ref index, ','); + var secondType = ParseTypeArgument(ref index); - var secondType = ParseTypeArgument(ref index); + if (TryReadChar(ref index, ']')) + { + return ImmutableArray.Create(firstType, secondType); + } - if (TryReadChar(ref index, ']')) - { - return ImmutableArray.Create(firstType, secondType); - } + ReadChar(ref index, ','); - ReadChar(ref index, ','); + var builder = ImmutableArray.CreateBuilder(); + builder.Add(firstType); + builder.Add(secondType); - var builder = ImmutableArray.CreateBuilder(); - builder.Add(firstType); - builder.Add(secondType); + do + { + builder.Add(ParseTypeArgument(ref index)); + } + while (TryReadChar(ref index, ',')); - do - { - builder.Add(ParseTypeArgument(ref index)); - } - while (TryReadChar(ref index, ',')); + ReadChar(ref index, ']'); + return builder.ToImmutable(); + } + InsType ParseTypeArgument(ref int index) + { + if (TryReadChar(ref index, '[')) + { + var result = ParseQualified(ref index); ReadChar(ref index, ']'); - return builder.ToImmutable(); + return result; } - - InsType ParseTypeArgument(ref int index) + else { - if (TryReadChar(ref index, '[')) - { - var result = ParseQualified(ref index); - ReadChar(ref index, ']'); - return result; - } - else - { - return ParseUnqualified(ref index, null); - } + return ParseUnqualified(ref index, null); } + } + + public InsAssembly ParseAssembly(ref int index) + { + AssertNotEOF(index); + var identifier = ParseIdentifier(ref index); + ImmutableArray.Builder? builder = null; - public InsAssembly ParseAssembly(ref int index) + while (TryReadChar(ref index, ',')) { + DiscardWhitespace(ref index); AssertNotEOF(index); - var identifier = ParseIdentifier(ref index); - ImmutableArray.Builder? builder = null; - - while (TryReadChar(ref index, ',')) - { - DiscardWhitespace(ref index); - AssertNotEOF(index); - builder ??= ImmutableArray.CreateBuilder(); - builder.Add(ParseQualification(ref index)); - } - - return new InsAssembly(identifier, builder?.ToImmutable() ?? ImmutableArray.Empty); + builder ??= ImmutableArray.CreateBuilder(); + builder.Add(ParseQualification(ref index)); } - InsAssemblyQualification ParseQualification(ref int index) - { - AssertNotEOF(index); + return new InsAssembly(identifier, builder?.ToImmutable() ?? ImmutableArray.Empty); + } - var name = ParseQuoteableIdentifier(ref index); - ReadChar(ref index, '='); - var value = ParseQuoteableIdentifier(ref index); + InsAssemblyQualification ParseQualification(ref int index) + { + AssertNotEOF(index); - return new InsAssemblyQualification(name, value); - } + var name = ParseQuoteableIdentifier(ref index); + ReadChar(ref index, '='); + var value = ParseQuoteableIdentifier(ref index); - string ParseQuoteableIdentifier(ref int index) - { - if (TryReadChar(ref index, '"')) - { - var result = ParseIdentifierCore(ref index, Delimiters.Quote); - ReadChar(ref index, '"'); - return result; - } + return new InsAssemblyQualification(name, value); + } - return ParseIdentifier(ref index); + string ParseQuoteableIdentifier(ref int index) + { + if (TryReadChar(ref index, '"')) + { + var result = ParseIdentifierCore(ref index, Delimiters.Quote); + ReadChar(ref index, '"'); + return result; } - string ParseIdentifier(ref int index) => ParseIdentifierCore(ref index, Delimiters.All); + return ParseIdentifier(ref index); + } + + string ParseIdentifier(ref int index) => ParseIdentifierCore(ref index, Delimiters.All); #if NET - string ParseIdentifierCore(ref int index, SearchValues delimiters) + string ParseIdentifierCore(ref int index, SearchValues delimiters) #else - string ParseIdentifierCore(ref int index, ReadOnlySpan delimiters) + string ParseIdentifierCore(ref int index, ReadOnlySpan delimiters) #endif - { - AssertNotEOF(index); - - var start = index; - StringBuilder? builder = null; - - while (true) - { - var i = _buffer.Slice(index).IndexOfAny(delimiters); + { + AssertNotEOF(index); - if (i < 0) - { - index = _buffer.Length; - break; - } + var start = index; + StringBuilder? builder = null; - index += i; + while (true) + { + var i = _buffer.Slice(index).IndexOfAny(delimiters); - if (_buffer[index] != '\\') - { - break; - } + if (i < 0) + { + index = _buffer.Length; + break; + } - builder ??= BuilderPool.Rent(); - builder.Append(_buffer.Slice(start, index - start)); - start = index + 1; + index += i; - AssertNotEOF(start); - index = start + 1; + if (_buffer[index] != '\\') + { + break; } - var section = _buffer.Slice(start, index - start); + builder ??= BuilderPool.Rent(); + builder.Append(_buffer.Slice(start, index - start)); + start = index + 1; - return builder == null - ? section.ToString() - : builder.Append(section).ToStringAndReturn(); + AssertNotEOF(start); + index = start + 1; } - void DiscardWhitespace(ref int index) + var section = _buffer.Slice(start, index - start); + + return builder == null + ? section.ToString() + : builder.Append(section).ToStringAndReturn(); + } + + void DiscardWhitespace(ref int index) + { + while (index < _buffer.Length && char.IsWhiteSpace(_buffer[index])) { - while (index < _buffer.Length && char.IsWhiteSpace(_buffer[index])) - { - index++; - } + index++; } + } - bool TryReadChar(ref int index, char c) + bool TryReadChar(ref int index, char c) + { + if (index < _buffer.Length && _buffer[index] == c) { - if (index < _buffer.Length && _buffer[index] == c) - { - index++; - return true; - } - - return false; + index++; + return true; } - void ReadChar(ref int index, char c) - { - AssertNotEOF(index); + return false; + } - if (_buffer[index] != c) - { - ThrowUnexpected(index); - } + void ReadChar(ref int index, char c) + { + AssertNotEOF(index); - index++; + if (_buffer[index] != c) + { + ThrowUnexpected(index); } - void AssertNotEOF(int index) + index++; + } + + void AssertNotEOF(int index) + { + if (index >= _buffer.Length) { - if (index >= _buffer.Length) - { - ThrowEOF(); - } + ThrowEOF(); } + } + + int LocateStartOfAssembly(int index) + { + var depth = 0; + var quoted = false; - int LocateStartOfAssembly(int index) + while (index < _buffer.Length) { - var depth = 0; - var quoted = false; + var c = _buffer[index]; - while (index < _buffer.Length) + if (c == '\\') { - var c = _buffer[index]; - - if (c == '\\') - { - index += 2; - continue; - } - else if (c == '"') - { - quoted = !quoted; - } - else if (quoted) - { - } - else if (c == '[') - { - depth++; - } - else if (c == ']') - { - depth--; + index += 2; + continue; + } + else if (c == '"') + { + quoted = !quoted; + } + else if (quoted) + { + } + else if (c == '[') + { + depth++; + } + else if (c == ']') + { + depth--; - if (depth < 0) - { - return -1; - } - } - else if (c == ',' && depth == 0) + if (depth < 0) { - return index; + return -1; } - - index++; + } + else if (c == ',' && depth == 0) + { + return index; } - return -1; + index++; } - readonly ReadOnlySpan _buffer = buffer; + return -1; } + + readonly ReadOnlySpan _buffer = buffer; } } diff --git a/src/TypeNameInterpretation/InsRewriter.cs b/src/TypeNameInterpretation/InsRewriter.cs index fd19f80..81652d0 100644 --- a/src/TypeNameInterpretation/InsRewriter.cs +++ b/src/TypeNameInterpretation/InsRewriter.cs @@ -2,164 +2,163 @@ using System; using System.Collections.Immutable; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public abstract class InsRewriter : IInsTypeVisitor { - public abstract class InsRewriter : IInsTypeVisitor + public virtual InsType VisitArray(InsArrayType type, TArgument argument) { - public virtual InsType VisitArray(InsArrayType type, TArgument argument) + ArgumentNullException.ThrowIfNull(type); + var elementType = type.ElementType.Apply(this, argument); + + if (elementType == type.ElementType) { - ArgumentNullException.ThrowIfNull(type); - var elementType = type.ElementType.Apply(this, argument); + return type; + } - if (elementType == type.ElementType) - { - return type; - } + return new InsArrayType(elementType, type.Rank); + } - return new InsArrayType(elementType, type.Rank); - } + public virtual InsType VisitByRef(InsByRefType type, TArgument argument) + { + ArgumentNullException.ThrowIfNull(type); + var inner = type.ElementType.Apply(this, argument); - public virtual InsType VisitByRef(InsByRefType type, TArgument argument) + if (inner == type.ElementType) { - ArgumentNullException.ThrowIfNull(type); - var inner = type.ElementType.Apply(this, argument); + return type; + } - if (inner == type.ElementType) - { - return type; - } + return new InsByRefType(inner); + } + + public virtual InsType VisitGeneric(InsGenericType type, TArgument argument) + { + ArgumentNullException.ThrowIfNull(type); + var definition = (InsNamedType)type.Definition.Apply(this, argument); + var typeArguments = VisitTypes(type.TypeArguments, argument); - return new InsByRefType(inner); + if (definition == type.Definition && typeArguments == type.TypeArguments) + { + return type; } - public virtual InsType VisitGeneric(InsGenericType type, TArgument argument) + return new InsGenericType(definition, typeArguments); + } + + public virtual InsType VisitNamed(InsNamedType type, TArgument argument) + { + ArgumentNullException.ThrowIfNull(type); + if (type.DeclaringType != null) { - ArgumentNullException.ThrowIfNull(type); - var definition = (InsNamedType)type.Definition.Apply(this, argument); - var typeArguments = VisitTypes(type.TypeArguments, argument); + var declaringType = (InsNamedType)type.DeclaringType.Apply(this, argument); - if (definition == type.Definition && typeArguments == type.TypeArguments) + if (declaringType == type.DeclaringType) { return type; } - return new InsGenericType(definition, typeArguments); + return new InsNamedType(type.Name, declaringType); } - - public virtual InsType VisitNamed(InsNamedType type, TArgument argument) + else { - ArgumentNullException.ThrowIfNull(type); - if (type.DeclaringType != null) - { - var declaringType = (InsNamedType)type.DeclaringType.Apply(this, argument); + var assembly = type.Assembly == null ? null : VisitAssembly(type.Assembly, argument); - if (declaringType == type.DeclaringType) - { - return type; - } - - return new InsNamedType(type.Name, declaringType); - } - else + if (assembly == type.Assembly) { - var assembly = type.Assembly == null ? null : VisitAssembly(type.Assembly, argument); - - if (assembly == type.Assembly) - { - return type; - } - - return new InsNamedType(type.Name, assembly); + return type; } + + return new InsNamedType(type.Name, assembly); } + } + + public virtual InsType VisitPointer(InsPointerType type, TArgument argument) + { + ArgumentNullException.ThrowIfNull(type); + var inner = type.ElementType.Apply(this, argument); - public virtual InsType VisitPointer(InsPointerType type, TArgument argument) + if (inner == type.ElementType) { - ArgumentNullException.ThrowIfNull(type); - var inner = type.ElementType.Apply(this, argument); + return type; + } - if (inner == type.ElementType) - { - return type; - } + return new InsPointerType(inner); + } - return new InsPointerType(inner); - } + public virtual InsType VisitSZArray(InsSZArrayType type, TArgument argument) + { + ArgumentNullException.ThrowIfNull(type); + var elementType = type.ElementType.Apply(this, argument); - public virtual InsType VisitSZArray(InsSZArrayType type, TArgument argument) + if (elementType == type.ElementType) { - ArgumentNullException.ThrowIfNull(type); - var elementType = type.ElementType.Apply(this, argument); + return type; + } - if (elementType == type.ElementType) - { - return type; - } + return new InsSZArrayType(elementType); + } - return new InsSZArrayType(elementType); - } + public virtual ImmutableArray VisitTypes(ImmutableArray typeArguments, TArgument argument) + { + ImmutableArray.Builder? builder = null; - public virtual ImmutableArray VisitTypes(ImmutableArray typeArguments, TArgument argument) + for (var i = 0; i < typeArguments.Length; i++) { - ImmutableArray.Builder? builder = null; + var type = typeArguments[i].Apply(this, argument); - for (var i = 0; i < typeArguments.Length; i++) + if (builder != null) { - var type = typeArguments[i].Apply(this, argument); - - if (builder != null) - { - builder[i] = type; - } - else if (type != typeArguments[i]) - { - builder = ImmutableArray.CreateBuilder(typeArguments.Length); - builder.AddRange(typeArguments, i); - builder.Count = typeArguments.Length; - builder[i] = type; - } + builder[i] = type; + } + else if (type != typeArguments[i]) + { + builder = ImmutableArray.CreateBuilder(typeArguments.Length); + builder.AddRange(typeArguments, i); + builder.Count = typeArguments.Length; + builder[i] = type; } - - return builder == null ? typeArguments : builder.MoveToImmutable(); } - public virtual InsAssembly VisitAssembly(InsAssembly assembly, TArgument argument) - { - ArgumentNullException.ThrowIfNull(assembly); - var qualifications = VisitAssemblyQualifications(assembly.Qualifications, argument); + return builder == null ? typeArguments : builder.MoveToImmutable(); + } - if (qualifications == assembly.Qualifications) - { - return assembly; - } + public virtual InsAssembly VisitAssembly(InsAssembly assembly, TArgument argument) + { + ArgumentNullException.ThrowIfNull(assembly); + var qualifications = VisitAssemblyQualifications(assembly.Qualifications, argument); - return new InsAssembly(assembly.Name, qualifications); + if (qualifications == assembly.Qualifications) + { + return assembly; } - public virtual ImmutableArray VisitAssemblyQualifications(ImmutableArray qualifications, TArgument argument) + return new InsAssembly(assembly.Name, qualifications); + } + + public virtual ImmutableArray VisitAssemblyQualifications(ImmutableArray qualifications, TArgument argument) + { + ImmutableArray.Builder? builder = null; + + for (var i = 0; i < qualifications.Length; i++) { - ImmutableArray.Builder? builder = null; + var type = VisitAssemblyQualification(qualifications[i], argument); - for (var i = 0; i < qualifications.Length; i++) + if (builder != null) { - var type = VisitAssemblyQualification(qualifications[i], argument); - - if (builder != null) - { - builder[i] = type; - } - else if (type != qualifications[i]) - { - builder = ImmutableArray.CreateBuilder(qualifications.Length); - builder.AddRange(qualifications, i); - builder.Count = qualifications.Length; - builder[i] = type; - } + builder[i] = type; + } + else if (type != qualifications[i]) + { + builder = ImmutableArray.CreateBuilder(qualifications.Length); + builder.AddRange(qualifications, i); + builder.Count = qualifications.Length; + builder[i] = type; } - - return builder == null ? qualifications : builder.MoveToImmutable(); } - public virtual InsAssemblyQualification VisitAssemblyQualification(InsAssemblyQualification qualification, TArgument argument) => qualification; + return builder == null ? qualifications : builder.MoveToImmutable(); } + + public virtual InsAssemblyQualification VisitAssemblyQualification(InsAssemblyQualification qualification, TArgument argument) => qualification; } diff --git a/src/TypeNameInterpretation/InsTypeFactory.cs b/src/TypeNameInterpretation/InsTypeFactory.cs index 9ece078..e99ef78 100644 --- a/src/TypeNameInterpretation/InsTypeFactory.cs +++ b/src/TypeNameInterpretation/InsTypeFactory.cs @@ -2,28 +2,27 @@ using System; using System.Collections.Immutable; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public static class InsTypeFactory { - public static class InsTypeFactory - { - public static InsArrayType ArrayType(InsType elementType, int rank) => new(elementType, rank); - public static InsByRefType ByRefType(InsType elementType) => new(elementType); - public static InsGenericType Generic(InsNamedType definition, params InsType[] typeArguments) => Generic(definition, ImmutableArray.Create(typeArguments)); - public static InsGenericType Generic(InsNamedType definition, ImmutableArray typeArguments) => new(definition, typeArguments); - public static InsNamedType NamedType(string name, InsAssembly? assembly = null) => new(name, assembly); - public static InsNamedType NestedType(InsNamedType declaringType, string name) => new(name, declaringType); - public static InsPointerType PointerType(InsType elementType) => new(elementType); - public static InsSZArrayType SZArrayType(InsType elementType) => new(elementType); + public static InsArrayType ArrayType(InsType elementType, int rank) => new(elementType, rank); + public static InsByRefType ByRefType(InsType elementType) => new(elementType); + public static InsGenericType Generic(InsNamedType definition, params InsType[] typeArguments) => Generic(definition, ImmutableArray.Create(typeArguments)); + public static InsGenericType Generic(InsNamedType definition, ImmutableArray typeArguments) => new(definition, typeArguments); + public static InsNamedType NamedType(string name, InsAssembly? assembly = null) => new(name, assembly); + public static InsNamedType NestedType(InsNamedType declaringType, string name) => new(name, declaringType); + public static InsPointerType PointerType(InsType elementType) => new(elementType); + public static InsSZArrayType SZArrayType(InsType elementType) => new(elementType); - public static InsAssembly Assembly(string name, params InsAssemblyQualification[] qualifications) => new(name, ImmutableArray.Create(qualifications)); - public static InsAssembly Assembly(string name, ImmutableArray qualifications) => new(name, qualifications); + public static InsAssembly Assembly(string name, params InsAssemblyQualification[] qualifications) => new(name, ImmutableArray.Create(qualifications)); + public static InsAssembly Assembly(string name, ImmutableArray qualifications) => new(name, qualifications); - public static InsAssemblyQualification Qualification(string name, string value) => new(name, value); + public static InsAssemblyQualification Qualification(string name, string value) => new(name, value); - public static InsAssembly ParseAssemblyName(string value) => InsParser.ParseAssembly(value.AsSpan()); - public static InsAssembly ParseAssemblyName(ReadOnlySpan value) => InsParser.ParseAssembly(value); + public static InsAssembly ParseAssemblyName(string value) => InsParser.ParseAssembly(value.AsSpan()); + public static InsAssembly ParseAssemblyName(ReadOnlySpan value) => InsParser.ParseAssembly(value); - public static InsType ParseTypeName(string value) => InsParser.ParseType(value.AsSpan()); - public static InsType ParseTypeName(ReadOnlySpan value) => InsParser.ParseType(value); - } + public static InsType ParseTypeName(string value) => InsParser.ParseType(value.AsSpan()); + public static InsType ParseTypeName(ReadOnlySpan value) => InsParser.ParseType(value); } diff --git a/src/TypeNameInterpretation/InvalidTypeNameException.NetFramework.cs b/src/TypeNameInterpretation/InvalidTypeNameException.NetFramework.cs index 7ac07da..575a207 100644 --- a/src/TypeNameInterpretation/InvalidTypeNameException.NetFramework.cs +++ b/src/TypeNameInterpretation/InvalidTypeNameException.NetFramework.cs @@ -3,15 +3,14 @@ using System; using System.Runtime.Serialization; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +[Serializable] +partial class InvalidTypeNameException { - [Serializable] - partial class InvalidTypeNameException + InvalidTypeNameException(SerializationInfo info, StreamingContext context) + : base(info, context) { - InvalidTypeNameException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } #endif diff --git a/src/TypeNameInterpretation/InvalidTypeNameException.cs b/src/TypeNameInterpretation/InvalidTypeNameException.cs index bfbe882..6c13624 100644 --- a/src/TypeNameInterpretation/InvalidTypeNameException.cs +++ b/src/TypeNameInterpretation/InvalidTypeNameException.cs @@ -1,22 +1,21 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using System; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed partial class InvalidTypeNameException : FormatException { - public sealed partial class InvalidTypeNameException : FormatException + public InvalidTypeNameException() { - public InvalidTypeNameException() - { - } + } - public InvalidTypeNameException(string message) - : base(message) - { - } + public InvalidTypeNameException(string message) + : base(message) + { + } - public InvalidTypeNameException(string message, Exception innerException) - : base(message, innerException) - { - } + public InvalidTypeNameException(string message, Exception innerException) + : base(message, innerException) + { } } diff --git a/src/TypeNameInterpretation/Tree/IInsTypeVisitor.cs b/src/TypeNameInterpretation/Tree/IInsTypeVisitor.cs index d8c6fa2..776d44d 100644 --- a/src/TypeNameInterpretation/Tree/IInsTypeVisitor.cs +++ b/src/TypeNameInterpretation/Tree/IInsTypeVisitor.cs @@ -1,13 +1,12 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public interface IInsTypeVisitor { - public interface IInsTypeVisitor - { - TReturn VisitArray(InsArrayType type, TArgument argument); - TReturn VisitByRef(InsByRefType type, TArgument argument); - TReturn VisitGeneric(InsGenericType type, TArgument argument); - TReturn VisitNamed(InsNamedType type, TArgument argument); - TReturn VisitPointer(InsPointerType type, TArgument argument); - TReturn VisitSZArray(InsSZArrayType type, TArgument argument); - } + TReturn VisitArray(InsArrayType type, TArgument argument); + TReturn VisitByRef(InsByRefType type, TArgument argument); + TReturn VisitGeneric(InsGenericType type, TArgument argument); + TReturn VisitNamed(InsNamedType type, TArgument argument); + TReturn VisitPointer(InsPointerType type, TArgument argument); + TReturn VisitSZArray(InsSZArrayType type, TArgument argument); } diff --git a/src/TypeNameInterpretation/Tree/InsArrayType.cs b/src/TypeNameInterpretation/Tree/InsArrayType.cs index 86c63fa..d1f0286 100644 --- a/src/TypeNameInterpretation/Tree/InsArrayType.cs +++ b/src/TypeNameInterpretation/Tree/InsArrayType.cs @@ -1,23 +1,22 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using System; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed class InsArrayType : InsElementedType { - public sealed class InsArrayType : InsElementedType + internal InsArrayType(InsType elementType, int rank) + : base(elementType) { - internal InsArrayType(InsType elementType, int rank) - : base(elementType) - { - Rank = rank; - } + Rank = rank; + } - public int Rank { get; } - public override InsTypeKind Kind => InsTypeKind.Array; + public int Rank { get; } + public override InsTypeKind Kind => InsTypeKind.Array; - public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) - { - ArgumentNullException.ThrowIfNull(visitor); - return visitor.VisitArray(this, argument); - } + public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) + { + ArgumentNullException.ThrowIfNull(visitor); + return visitor.VisitArray(this, argument); } } diff --git a/src/TypeNameInterpretation/Tree/InsAssembly.cs b/src/TypeNameInterpretation/Tree/InsAssembly.cs index 6ff37cb..a476f6d 100644 --- a/src/TypeNameInterpretation/Tree/InsAssembly.cs +++ b/src/TypeNameInterpretation/Tree/InsAssembly.cs @@ -2,19 +2,18 @@ using System; using System.Collections.Immutable; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed class InsAssembly { - public sealed class InsAssembly + internal InsAssembly(string name, ImmutableArray qualifications) { - internal InsAssembly(string name, ImmutableArray qualifications) - { - ArgumentNullException.ThrowIfNull(name); - Name = name; - Qualifications = qualifications; - } - - public string Name { get; } - public ImmutableArray Qualifications { get; } - public override string ToString() => InsFormatter.Format(this); + ArgumentNullException.ThrowIfNull(name); + Name = name; + Qualifications = qualifications; } + + public string Name { get; } + public ImmutableArray Qualifications { get; } + public override string ToString() => InsFormatter.Format(this); } diff --git a/src/TypeNameInterpretation/Tree/InsAssemblyQualification.cs b/src/TypeNameInterpretation/Tree/InsAssemblyQualification.cs index 37a0b13..b72a067 100644 --- a/src/TypeNameInterpretation/Tree/InsAssemblyQualification.cs +++ b/src/TypeNameInterpretation/Tree/InsAssemblyQualification.cs @@ -1,19 +1,18 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using System; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed class InsAssemblyQualification { - public sealed class InsAssemblyQualification + internal InsAssemblyQualification(string name, string value) { - internal InsAssemblyQualification(string name, string value) - { - ArgumentNullException.ThrowIfNull(name); - ArgumentNullException.ThrowIfNull(value); - Name = name; - Value = value; - } - - public string Name { get; } - public string Value { get; } + ArgumentNullException.ThrowIfNull(name); + ArgumentNullException.ThrowIfNull(value); + Name = name; + Value = value; } + + public string Name { get; } + public string Value { get; } } diff --git a/src/TypeNameInterpretation/Tree/InsByRefType.cs b/src/TypeNameInterpretation/Tree/InsByRefType.cs index e684898..dbf63df 100644 --- a/src/TypeNameInterpretation/Tree/InsByRefType.cs +++ b/src/TypeNameInterpretation/Tree/InsByRefType.cs @@ -1,21 +1,20 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using System; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed class InsByRefType : InsElementedType { - public sealed class InsByRefType : InsElementedType + internal InsByRefType(InsType elementType) + : base(elementType) { - internal InsByRefType(InsType elementType) - : base(elementType) - { - } + } - public override InsTypeKind Kind => InsTypeKind.ByRef; + public override InsTypeKind Kind => InsTypeKind.ByRef; - public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) - { - ArgumentNullException.ThrowIfNull(visitor); - return visitor.VisitByRef(this, argument); - } + public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) + { + ArgumentNullException.ThrowIfNull(visitor); + return visitor.VisitByRef(this, argument); } } diff --git a/src/TypeNameInterpretation/Tree/InsElementedType.cs b/src/TypeNameInterpretation/Tree/InsElementedType.cs index 6d089c4..49cf5b4 100644 --- a/src/TypeNameInterpretation/Tree/InsElementedType.cs +++ b/src/TypeNameInterpretation/Tree/InsElementedType.cs @@ -1,16 +1,15 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using System; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public abstract class InsElementedType : InsType { - public abstract class InsElementedType : InsType + private protected InsElementedType(InsType elementType) { - private protected InsElementedType(InsType elementType) - { - ArgumentNullException.ThrowIfNull(elementType); - ElementType = elementType; - } - - public InsType ElementType { get; } + ArgumentNullException.ThrowIfNull(elementType); + ElementType = elementType; } + + public InsType ElementType { get; } } diff --git a/src/TypeNameInterpretation/Tree/InsGenericType.cs b/src/TypeNameInterpretation/Tree/InsGenericType.cs index bad4396..5939870 100644 --- a/src/TypeNameInterpretation/Tree/InsGenericType.cs +++ b/src/TypeNameInterpretation/Tree/InsGenericType.cs @@ -2,25 +2,24 @@ using System; using System.Collections.Immutable; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed class InsGenericType : InsType { - public sealed class InsGenericType : InsType + internal InsGenericType(InsNamedType definition, ImmutableArray typeArguments) { - internal InsGenericType(InsNamedType definition, ImmutableArray typeArguments) - { - ArgumentNullException.ThrowIfNull(definition); - Definition = definition; - TypeArguments = typeArguments; - } + ArgumentNullException.ThrowIfNull(definition); + Definition = definition; + TypeArguments = typeArguments; + } - public InsNamedType Definition { get; } - public ImmutableArray TypeArguments { get; } - public override InsTypeKind Kind => InsTypeKind.Generic; + public InsNamedType Definition { get; } + public ImmutableArray TypeArguments { get; } + public override InsTypeKind Kind => InsTypeKind.Generic; - public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) - { - ArgumentNullException.ThrowIfNull(visitor); - return visitor.VisitGeneric(this, argument); - } + public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) + { + ArgumentNullException.ThrowIfNull(visitor); + return visitor.VisitGeneric(this, argument); } } diff --git a/src/TypeNameInterpretation/Tree/InsNamedType.cs b/src/TypeNameInterpretation/Tree/InsNamedType.cs index d92b9d3..55e75d4 100644 --- a/src/TypeNameInterpretation/Tree/InsNamedType.cs +++ b/src/TypeNameInterpretation/Tree/InsNamedType.cs @@ -1,38 +1,37 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using System; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed class InsNamedType : InsType { - public sealed class InsNamedType : InsType + internal InsNamedType( + string name, + InsAssembly? assembly) { - internal InsNamedType( - string name, - InsAssembly? assembly) - { - ArgumentNullException.ThrowIfNull(name); - Name = name; - Assembly = assembly; - } + ArgumentNullException.ThrowIfNull(name); + Name = name; + Assembly = assembly; + } - internal InsNamedType( - string name, - InsNamedType? declaringType) - { - ArgumentNullException.ThrowIfNull(name); - ArgumentNullException.ThrowIfNull(declaringType); - Name = name; - DeclaringType = declaringType; - } + internal InsNamedType( + string name, + InsNamedType? declaringType) + { + ArgumentNullException.ThrowIfNull(name); + ArgumentNullException.ThrowIfNull(declaringType); + Name = name; + DeclaringType = declaringType; + } - public string Name { get; } - public InsNamedType? DeclaringType { get; } - public InsAssembly? Assembly { get; } - public override InsTypeKind Kind => InsTypeKind.Named; + public string Name { get; } + public InsNamedType? DeclaringType { get; } + public InsAssembly? Assembly { get; } + public override InsTypeKind Kind => InsTypeKind.Named; - public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) - { - ArgumentNullException.ThrowIfNull(visitor); - return visitor.VisitNamed(this, argument); - } + public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) + { + ArgumentNullException.ThrowIfNull(visitor); + return visitor.VisitNamed(this, argument); } } diff --git a/src/TypeNameInterpretation/Tree/InsPointerType.cs b/src/TypeNameInterpretation/Tree/InsPointerType.cs index ccd798f..94c7323 100644 --- a/src/TypeNameInterpretation/Tree/InsPointerType.cs +++ b/src/TypeNameInterpretation/Tree/InsPointerType.cs @@ -1,21 +1,20 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using System; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed class InsPointerType : InsElementedType { - public sealed class InsPointerType : InsElementedType + internal InsPointerType(InsType elementType) + : base(elementType) { - internal InsPointerType(InsType elementType) - : base(elementType) - { - } + } - public override InsTypeKind Kind => InsTypeKind.Pointer; + public override InsTypeKind Kind => InsTypeKind.Pointer; - public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) - { - ArgumentNullException.ThrowIfNull(visitor); - return visitor.VisitPointer(this, argument); - } + public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) + { + ArgumentNullException.ThrowIfNull(visitor); + return visitor.VisitPointer(this, argument); } } diff --git a/src/TypeNameInterpretation/Tree/InsSZArrayType.cs b/src/TypeNameInterpretation/Tree/InsSZArrayType.cs index 9e8e1d7..3de4fb0 100644 --- a/src/TypeNameInterpretation/Tree/InsSZArrayType.cs +++ b/src/TypeNameInterpretation/Tree/InsSZArrayType.cs @@ -1,21 +1,20 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. using System; -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public sealed class InsSZArrayType : InsElementedType { - public sealed class InsSZArrayType : InsElementedType + internal InsSZArrayType(InsType elementType) + : base(elementType) { - internal InsSZArrayType(InsType elementType) - : base(elementType) - { - } + } - public override InsTypeKind Kind => InsTypeKind.SZArray; + public override InsTypeKind Kind => InsTypeKind.SZArray; - public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) - { - ArgumentNullException.ThrowIfNull(visitor); - return visitor.VisitSZArray(this, argument); - } + public override TReturn Apply(IInsTypeVisitor visitor, TArgument argument) + { + ArgumentNullException.ThrowIfNull(visitor); + return visitor.VisitSZArray(this, argument); } } diff --git a/src/TypeNameInterpretation/Tree/InsType.cs b/src/TypeNameInterpretation/Tree/InsType.cs index 81758d6..66c98e8 100644 --- a/src/TypeNameInterpretation/Tree/InsType.cs +++ b/src/TypeNameInterpretation/Tree/InsType.cs @@ -1,14 +1,13 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public abstract class InsType { - public abstract class InsType + private protected InsType() { - private protected InsType() - { - } - - public abstract InsTypeKind Kind { get; } - public override string ToString() => InsFormatter.Format(this); - public abstract TReturn Apply(IInsTypeVisitor visitor, TArgument argument); } + + public abstract InsTypeKind Kind { get; } + public override string ToString() => InsFormatter.Format(this); + public abstract TReturn Apply(IInsTypeVisitor visitor, TArgument argument); } diff --git a/src/TypeNameInterpretation/Tree/InsTypeKind.cs b/src/TypeNameInterpretation/Tree/InsTypeKind.cs index 7592467..58b2a02 100644 --- a/src/TypeNameInterpretation/Tree/InsTypeKind.cs +++ b/src/TypeNameInterpretation/Tree/InsTypeKind.cs @@ -1,13 +1,12 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public enum InsTypeKind { - public enum InsTypeKind - { - Array, - ByRef, - Generic, - Named, - Pointer, - SZArray, - } + Array, + ByRef, + Generic, + Named, + Pointer, + SZArray, } diff --git a/src/TypeNameInterpretation/WellKnownQualificationNames.cs b/src/TypeNameInterpretation/WellKnownQualificationNames.cs index 3f07018..f677b2d 100644 --- a/src/TypeNameInterpretation/WellKnownQualificationNames.cs +++ b/src/TypeNameInterpretation/WellKnownQualificationNames.cs @@ -1,12 +1,11 @@ // Copyright (c) Brian Reichle. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -namespace TypeNameInterpretation +namespace TypeNameInterpretation; + +public static class WellKnownQualificationNames { - public static class WellKnownQualificationNames - { - public const string Version = "Version"; - public const string PublicKey = "PublicKey"; - public const string PublicKeyToken = "PublicKeyToken"; - public const string Culture = "Culture"; - public const string ProcessorArchitecture = "ProcessorArchitecture"; - } + public const string Version = "Version"; + public const string PublicKey = "PublicKey"; + public const string PublicKeyToken = "PublicKeyToken"; + public const string Culture = "Culture"; + public const string ProcessorArchitecture = "ProcessorArchitecture"; }