From 078d3cd0df9316759f1c9a73f0648c49ba45688a Mon Sep 17 00:00:00 2001 From: JPaja Date: Sat, 27 Nov 2021 21:23:50 +0100 Subject: [PATCH] Add: Test --- .../CustomAttributeTest.cs | 27 +++++++++++++++++++ .../CustomAttributesTestClass.cs | 12 ++++++--- .../TestCaseAttribute2.cs | 16 +++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 test/TestBinaries/DotNet/AsmResolver.DotNet.TestCases.CustomAttributes/TestCaseAttribute2.cs diff --git a/test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs b/test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs index 903b65c44..aece51dfe 100644 --- a/test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs +++ b/test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs @@ -86,6 +86,19 @@ private static CustomAttribute GetCustomAttributeTestCase(string methodName, boo return attribute; } + private static CustomAttribute GetCustomAttributeTestCase2(string methodName, bool rebuild = false) + { + var module = ModuleDefinition.FromFile(typeof(CustomAttributesTestClass).Assembly.Location); + var type = module.TopLevelTypes.First(t => t.Name == nameof(CustomAttributesTestClass)); + var method = type.Methods.First(m => m.Name == methodName); + var attribute = method.CustomAttributes + .First(c => c.Constructor.DeclaringType.Name == nameof(TestCase2Attribute)); + + if (rebuild) + attribute = RebuildAndLookup(attribute); + return attribute; + } + private static CustomAttribute RebuildAndLookup(CustomAttribute attribute) { var stream = new MemoryStream(); @@ -154,6 +167,20 @@ public void FixedTypeArgument(bool rebuild) argument.Element as TypeSignature, _comparer); } + [Theory] + [InlineData(false)] + [InlineData(true)] + public void FixedTypeArgumentNull(bool rebuild) + { + var attribute = GetCustomAttributeTestCase2(nameof(CustomAttributesTestClass.FixedTypeArgumentNull), rebuild); + Assert.Single(attribute.Signature.FixedArguments); + Assert.Empty(attribute.Signature.NamedArguments); + + var argument = attribute.Signature.FixedArguments[0]; + Assert.Null(argument.Element); + } + + [Theory] [InlineData(false)] [InlineData(true)] diff --git a/test/TestBinaries/DotNet/AsmResolver.DotNet.TestCases.CustomAttributes/CustomAttributesTestClass.cs b/test/TestBinaries/DotNet/AsmResolver.DotNet.TestCases.CustomAttributes/CustomAttributesTestClass.cs index e56c33afe..aab27eac8 100644 --- a/test/TestBinaries/DotNet/AsmResolver.DotNet.TestCases.CustomAttributes/CustomAttributesTestClass.cs +++ b/test/TestBinaries/DotNet/AsmResolver.DotNet.TestCases.CustomAttributes/CustomAttributesTestClass.cs @@ -47,8 +47,14 @@ public void FixedEnumArgument() [TestCase(typeof(string))] public void FixedTypeArgument() { - } - + } + + [TestCase2(null)] + public void FixedTypeArgumentNull() + { + } + + [TestCase(typeof(KeyValuePair))] public void FixedComplexTypeArgument() { @@ -130,4 +136,4 @@ public void FixedInt32ArrayAsObjectEmptyArgument() } } -} \ No newline at end of file +} diff --git a/test/TestBinaries/DotNet/AsmResolver.DotNet.TestCases.CustomAttributes/TestCaseAttribute2.cs b/test/TestBinaries/DotNet/AsmResolver.DotNet.TestCases.CustomAttributes/TestCaseAttribute2.cs new file mode 100644 index 000000000..8aa24f675 --- /dev/null +++ b/test/TestBinaries/DotNet/AsmResolver.DotNet.TestCases.CustomAttributes/TestCaseAttribute2.cs @@ -0,0 +1,16 @@ +using System; + +namespace AsmResolver.DotNet.TestCases.CustomAttributes +{ + [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] + public class TestCase2Attribute : Attribute + { + public TestCase2Attribute(Type type) + { + Type = type; + } + + public Type Type { get; set; } + + } +}