Skip to content

Commit

Permalink
Add: Test
Browse files Browse the repository at this point in the history
  • Loading branch information
JPaja committed Nov 27, 2021
1 parent 42d6727 commit 078d3cd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
27 changes: 27 additions & 0 deletions test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ public void FixedEnumArgument()
[TestCase(typeof(string))]
public void FixedTypeArgument()
{
}

}

[TestCase2(null)]
public void FixedTypeArgumentNull()
{
}


[TestCase(typeof(KeyValuePair<string[], int[]>))]
public void FixedComplexTypeArgument()
{
Expand Down Expand Up @@ -130,4 +136,4 @@ public void FixedInt32ArrayAsObjectEmptyArgument()
}

}
}
}
Original file line number Diff line number Diff line change
@@ -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; }

}
}

0 comments on commit 078d3cd

Please sign in to comment.