From 42b9ef143d3f02403d19f7c3bbe1af0c59fe2031 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Thu, 29 Sep 2022 14:03:25 -0700 Subject: Add support for generic attributes (#871) * Add support for generic attributes * Compile test assembly against mscorlib To satisfy PEVerify --- Test/Mono.Cecil.Tests/CustomAttributesTests.cs | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'Test/Mono.Cecil.Tests') diff --git a/Test/Mono.Cecil.Tests/CustomAttributesTests.cs b/Test/Mono.Cecil.Tests/CustomAttributesTests.cs index a406c41..00f00ca 100644 --- a/Test/Mono.Cecil.Tests/CustomAttributesTests.cs +++ b/Test/Mono.Cecil.Tests/CustomAttributesTests.cs @@ -464,6 +464,80 @@ namespace Mono.Cecil.Tests { }, verify: !Platform.OnMono); } + [Test] + public void GenericAttributeString () + { + TestModule ("GenericAttributes.dll", module => { + var type = module.GetType ("WithGenericAttribute_OfString"); + Assert.IsTrue (type.HasCustomAttributes); + var attributes = type.CustomAttributes; + Assert.AreEqual (1, attributes.Count); + Assert.AreEqual ("GenericAttribute`1", attributes [0].AttributeType.FullName); + var attribute = attributes [0]; + // constructor arguments + Assert.AreEqual (true, attribute.HasConstructorArguments); + var argument = attribute.ConstructorArguments.Single (); + Assert.AreEqual ("System.String", argument.Type.FullName); + Assert.AreEqual ("t", argument.Value); + // named field argument + Assert.AreEqual (true, attribute.HasFields); + var field = attribute.Fields.Single (); + Assert.AreEqual ("F", field.Name); + Assert.AreEqual ("System.String", field.Argument.Type.FullName); + Assert.AreEqual ("f", field.Argument.Value); + // named property argument + Assert.AreEqual (true, attribute.HasProperties); + var property = attribute.Properties.Single (); + Assert.AreEqual ("P", property.Name); + Assert.AreEqual ("System.String", property.Argument.Type.FullName); + Assert.AreEqual ("p", property.Argument.Value); + + }, verify: !Platform.OnMono); + } + + [Test] + public void GenericAttributeInt () + { + TestModule ("GenericAttributes.dll", module => { + var type = module.GetType ("WithGenericAttribute_OfInt"); + Assert.IsTrue (type.HasCustomAttributes); + var attributes = type.CustomAttributes; + Assert.AreEqual (1, attributes.Count); + Assert.AreEqual ("GenericAttribute`1", attributes [0].AttributeType.FullName); + var attribute = attributes [0]; + // constructor arguments + Assert.AreEqual (true, attribute.HasConstructorArguments); + var argument = attribute.ConstructorArguments.Single (); + Assert.AreEqual ("System.Int32", argument.Type.FullName); + Assert.AreEqual (1, argument.Value); + // named field argument + Assert.AreEqual (true, attribute.HasFields); + var field = attribute.Fields.Single (); + Assert.AreEqual ("F", field.Name); + Assert.AreEqual ("System.Int32", field.Argument.Type.FullName); + Assert.AreEqual (2, field.Argument.Value); + // named property argument + Assert.AreEqual (true, attribute.HasProperties); + var property = attribute.Properties.Single (); + Assert.AreEqual ("P", property.Name); + Assert.AreEqual ("System.Int32", property.Argument.Type.FullName); + Assert.AreEqual (3, property.Argument.Value); + }, verify: !Platform.OnMono); + } + + [Test] + public void ConstrainedGenericAttribute () + { + TestModule ("GenericAttributes.dll", module => { + var type = module.GetType ("WithConstrainedGenericAttribute"); + Assert.IsTrue (type.HasCustomAttributes); + var attributes = type.CustomAttributes; + Assert.AreEqual (1, attributes.Count); + var attribute = attributes [0]; + Assert.AreEqual ("ConstrainedGenericAttribute`1", attribute.AttributeType.FullName); + }, verify: !Platform.OnMono); + } + [Test] public void NullCharInString () { -- cgit v1.2.3