提交 3d29852c 编写于 作者: A AlekseyTs

Add a test for assembly level attributes in NetModules in absence of...

Add a test for assembly level attributes in NetModules in absence of AssemblyAttributesGoHere types.

Related to #10550.
上级 7b1c17c6
......@@ -995,6 +995,8 @@ class Program
references: new[] { netModuleWithAssemblyAttributes.GetReference() },
options: TestOptions.ReleaseExe);
Assert.NotNull(consoleappCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHere"));
Assert.NotNull(consoleappCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHereM"));
var diagnostics = consoleappCompilation.GetDiagnostics();
var attrs = consoleappCompilation.Assembly.GetAttributes();
......@@ -1055,6 +1057,118 @@ class Program
Assert.True(token.IsNil); //could the type ref be located? If not then the attribute's not there.
}
[Fact]
[WorkItem(10550, "https://github.com/dotnet/roslyn/issues/10550")]
public void AssemblyAttributesFromNetModule_WithoutAssemblyAttributesGoHereTypes()
{
string netModuleSource =
@"using System;
using System.Reflection;
[assembly: UserDefinedAssemblyAttrNoAllowMultiple(""UserDefinedAssemblyAttrNoAllowMultiple"")]
[assembly: UserDefinedAssemblyAttrAllowMultiple(""UserDefinedAssemblyAttrAllowMultiple"")]
public class NetModuleClass { }
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public class UserDefinedAssemblyAttrNoAllowMultipleAttribute : Attribute
{
public UserDefinedAssemblyAttrNoAllowMultipleAttribute(string text) {}
}
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public class UserDefinedAssemblyAttrAllowMultipleAttribute : Attribute
{
public UserDefinedAssemblyAttrAllowMultipleAttribute(string text) {}
}
";
string consoleappSource =
@"
class Program
{
static void Main(string[] args) { }
}
";
var netmoduleCompilation = CreateCompilation(netModuleSource,
options: TestOptions.ReleaseModule,
references: new[] { MinCorlibRef });
Assert.Null(netmoduleCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHere"));
Assert.Null(netmoduleCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHereM"));
var netModuleWithAssemblyAttributes = ModuleMetadata.CreateFromImage(netmoduleCompilation.EmitToArray());
PEModule peModule = netModuleWithAssemblyAttributes.Module;
var metadataReader = peModule.GetMetadataReader();
Assert.Equal(0, metadataReader.GetTableRowCount(TableIndex.ExportedType));
Assert.Equal(4, metadataReader.CustomAttributes.Count);
Assert.Equal(0, metadataReader.DeclarativeSecurityAttributes.Count);
EntityHandle token = peModule.GetTypeRef(peModule.GetAssemblyRef("mincorlib"), "System.Runtime.CompilerServices", "AssemblyAttributesGoHereM");
Assert.False(token.IsNil); //could the type ref be located? If not then the attribute's not there.
var consoleappCompilation = CreateCompilation(
consoleappSource,
references: new[] { MinCorlibRef, netModuleWithAssemblyAttributes.GetReference() },
options: TestOptions.ReleaseExe);
Assert.Null(consoleappCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHere"));
Assert.Null(consoleappCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHereM"));
consoleappCompilation.GetDiagnostics().Verify();
var attrs = consoleappCompilation.Assembly.GetAttributes();
Assert.Equal(2, attrs.Length);
foreach (var a in attrs)
{
switch (a.AttributeClass.Name)
{
case "UserDefinedAssemblyAttrNoAllowMultipleAttribute":
Assert.Equal(@"UserDefinedAssemblyAttrNoAllowMultipleAttribute(""UserDefinedAssemblyAttrNoAllowMultiple"")", a.ToString());
break;
case "UserDefinedAssemblyAttrAllowMultipleAttribute":
Assert.Equal(@"UserDefinedAssemblyAttrAllowMultipleAttribute(""UserDefinedAssemblyAttrAllowMultiple"")", a.ToString());
break;
default:
Assert.Equal("Unexpected Attr", a.AttributeClass.Name);
break;
}
}
var exeMetadata = AssemblyMetadata.CreateFromImage(consoleappCompilation.EmitToArray());
peModule = exeMetadata.GetAssembly().ManifestModule;
metadataReader = peModule.GetMetadataReader();
Assert.Equal(1, metadataReader.GetTableRowCount(TableIndex.ModuleRef));
Assert.Equal(3, metadataReader.GetTableRowCount(TableIndex.ExportedType));
Assert.Equal(2, metadataReader.CustomAttributes.Count);
Assert.Equal(0, metadataReader.DeclarativeSecurityAttributes.Count);
token = peModule.GetTypeRef(peModule.GetAssemblyRef("mincorlib"), "System.Runtime.CompilerServices", "AssemblyAttributesGoHereM");
Assert.True(token.IsNil); //could the type ref be located? If not then the attribute's not there.
consoleappCompilation = CreateCompilation(
consoleappSource,
references: new[] { MinCorlibRef, netModuleWithAssemblyAttributes.GetReference() },
options: TestOptions.ReleaseModule);
Assert.Equal(0, consoleappCompilation.Assembly.GetAttributes().Length);
var modMetadata = ModuleMetadata.CreateFromImage(consoleappCompilation.EmitToArray());
peModule = modMetadata.Module;
metadataReader = peModule.GetMetadataReader();
Assert.Equal(0, metadataReader.GetTableRowCount(TableIndex.ModuleRef));
Assert.Equal(0, metadataReader.GetTableRowCount(TableIndex.ExportedType));
Assert.Equal(0, metadataReader.CustomAttributes.Count);
Assert.Equal(0, metadataReader.DeclarativeSecurityAttributes.Count);
token = peModule.GetTypeRef(peModule.GetAssemblyRef("mincorlib"), "System.Runtime.CompilerServices", "AssemblyAttributesGoHereM");
Assert.True(token.IsNil); //could the type ref be located? If not then the attribute's not there.
}
[Fact]
public void AssemblyAttributesFromNetModuleDropIdentical()
{
......
......@@ -1261,6 +1261,8 @@ End Class
Assert.False(token.IsNil) 'could the type ref be located? If not then the attribute's not there.
Dim consoleappCompilation = CreateCompilationWithMscorlibAndReferences(consoleappSource, {netModuleWithAssemblyAttributes.GetReference()})
Assert.NotNull(consoleappCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHere"))
Assert.NotNull(consoleappCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHereM"))
Dim diagnostics = consoleappCompilation.GetDiagnostics()
Dim attrs = consoleappCompilation.Assembly.GetAttributes()
......@@ -1313,6 +1315,117 @@ End Class
Assert.True(token.IsNil) 'could the type ref be located? If not then the attribute's not there.
End Sub
<Fact()>
<WorkItem(10550, "https://github.com/dotnet/roslyn/issues/10550")>
Public Sub AssemblyAttributesFromNetModule_WithoutAssemblyAttributesGoHereTypes()
Dim netmoduleSource =
<compilation>
<file name="a.vb">
<![CDATA[
Imports System
<Assembly: UserDefinedAssemblyAttrNoAllowMultiple("UserDefinedAssemblyAttrNoAllowMultiple")>
<Assembly: UserDefinedAssemblyAttrAllowMultiple("UserDefinedAssemblyAttrAllowMultiple")>
Public Class NetModuleClass
End Class
<AttributeUsage(AttributeTargets.Assembly, AllowMultiple := False)>
Public Class UserDefinedAssemblyAttrNoAllowMultipleAttribute
Inherits Attribute
Public Sub New(text1 As String)
End Sub
End Class
<AttributeUsage(AttributeTargets.Assembly, AllowMultiple := True)>
Public Class UserDefinedAssemblyAttrAllowMultipleAttribute
Inherits Attribute
Public Sub New(text1 As String)
End Sub
End Class
]]>
</file>
</compilation>
Dim consoleappSource =
<compilation>
<file name="a.vb">
<![CDATA[
Class Program
Private Shared Sub Main(args As String())
End Sub
End Class
]]>
</file>
</compilation>
Dim netmoduleCompilation = CreateCompilationWithReferences(netmoduleSource, references:={MinCorlibRef}, options:=TestOptions.ReleaseModule)
Assert.Null(netmoduleCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHere"))
Assert.Null(netmoduleCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHereM"))
Dim bytes = netmoduleCompilation.EmitToArray()
Dim netModuleWithAssemblyAttributes = ModuleMetadata.CreateFromImage(bytes)
Dim metadata As PEModule = netModuleWithAssemblyAttributes.Module
Dim metadataReader = metadata.GetMetadataReader()
Assert.Equal(0, metadataReader.GetTableRowCount(TableIndex.ExportedType))
Assert.Equal(4, metadataReader.CustomAttributes.Count)
Assert.Equal(0, metadataReader.DeclarativeSecurityAttributes.Count)
Dim token As EntityHandle = metadata.GetTypeRef(metadata.GetAssemblyRef("mincorlib"), "System.Runtime.CompilerServices", "AssemblyAttributesGoHereM")
Assert.False(token.IsNil) 'could the type ref be located? If not then the attribute's not there.
Dim consoleappCompilation = CreateCompilationWithReferences(consoleappSource, {MinCorlibRef, netModuleWithAssemblyAttributes.GetReference()})
Assert.Null(consoleappCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHere"))
Assert.Null(consoleappCompilation.GetTypeByMetadataName("System.Runtime.CompilerServices.AssemblyAttributesGoHereM"))
consoleappCompilation.GetDiagnostics().Verify()
Dim attrs = consoleappCompilation.Assembly.GetAttributes()
Assert.Equal(2, attrs.Length)
For Each a In attrs
Select Case a.AttributeClass.Name
Case "UserDefinedAssemblyAttrNoAllowMultipleAttribute"
Assert.Equal("UserDefinedAssemblyAttrNoAllowMultipleAttribute(""UserDefinedAssemblyAttrNoAllowMultiple"")", a.ToString())
Exit Select
Case "UserDefinedAssemblyAttrAllowMultipleAttribute"
Assert.Equal("UserDefinedAssemblyAttrAllowMultipleAttribute(""UserDefinedAssemblyAttrAllowMultiple"")", a.ToString())
Exit Select
Case Else
Assert.Equal("Unexpected Attr", a.AttributeClass.Name)
Exit Select
End Select
Next
metadata = AssemblyMetadata.CreateFromImage(consoleappCompilation.EmitToArray()).GetAssembly.ManifestModule
metadataReader = metadata.GetMetadataReader()
Assert.Equal(1, metadataReader.GetTableRowCount(TableIndex.ModuleRef))
Assert.Equal(3, metadataReader.GetTableRowCount(TableIndex.ExportedType))
Assert.Equal(2, metadataReader.CustomAttributes.Count)
Assert.Equal(0, metadataReader.DeclarativeSecurityAttributes.Count)
token = metadata.GetTypeRef(metadata.GetAssemblyRef("mincorlib"), "System.Runtime.CompilerServices", "AssemblyAttributesGoHereM")
Assert.True(token.IsNil) 'could the type ref be located? If not then the attribute's not there.
consoleappCompilation = CreateCompilationWithReferences(consoleappSource, {MinCorlibRef, netModuleWithAssemblyAttributes.GetReference()}, TestOptions.ReleaseModule)
Assert.Equal(0, consoleappCompilation.Assembly.GetAttributes().Length)
Dim modRef = DirectCast(consoleappCompilation.EmitToImageReference(), MetadataImageReference)
metadata = ModuleMetadata.CreateFromImage(consoleappCompilation.EmitToArray()).Module
metadataReader = metadata.GetMetadataReader()
Assert.Equal(0, metadataReader.GetTableRowCount(TableIndex.ModuleRef))
Assert.Equal(0, metadataReader.GetTableRowCount(TableIndex.ExportedType))
Assert.Equal(0, metadataReader.CustomAttributes.Count)
Assert.Equal(0, metadataReader.DeclarativeSecurityAttributes.Count)
token = metadata.GetTypeRef(metadata.GetAssemblyRef("mincorlib"), "System.Runtime.CompilerServices", "AssemblyAttributesGoHereM")
Assert.True(token.IsNil) 'could the type ref be located? If not then the attribute's not there.
End Sub
<Fact()>
Public Sub AssemblyAttributesFromNetModuleDropIdentical()
Dim consoleappSource =
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册