-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
CustomAssemblyTests.cs
160 lines (138 loc) · 6.08 KB
/
CustomAssemblyTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using Xunit;
namespace System.Reflection.Context.Tests
{
public class CustomAssemblyTests
{
private readonly Assembly _customAssembly;
public CustomAssemblyTests()
{
var customReflectionContext = new TestCustomReflectionContext();
Assembly assembly = typeof(CustomAssemblyTests).Assembly;
_customAssembly = customReflectionContext.MapAssembly(assembly);
}
[Fact]
public void ProjectAssemblyTest()
{
Assert.Equal(ProjectionConstants.CustomAssembly, _customAssembly.GetType().FullName);
}
[Fact]
public void ManifestModuleTest()
{
Module customModule = _customAssembly.ManifestModule;
Assert.Equal(ProjectionConstants.CustomModule, customModule.GetType().FullName);
}
[Fact]
public void GetCustomAttributesTest()
{
object[] attributes = _customAssembly.GetCustomAttributes(typeof(TestAssemblyAttribute), true);
Assert.Single(attributes);
Assert.IsType<TestAssemblyAttribute>(attributes[0]);
}
[Fact]
public void GetCustomAttributesDataTest()
{
IList<CustomAttributeData> customAttributesData = _customAssembly.GetCustomAttributesData();
Assert.NotEmpty(customAttributesData);
Assert.All(customAttributesData,
cad => Assert.Equal(ProjectionConstants.ProjectingCustomAttributeData, cad.GetType().FullName));
}
[Fact]
public void IsDefinedTest()
{
Assert.True(_customAssembly.IsDefined(typeof(TestAssemblyAttribute), true));
Assert.True(_customAssembly.IsDefined(typeof(TestAssemblyAttribute), false));
Assert.False(_customAssembly.IsDefined(typeof(DataContractAttribute), true));
Assert.False(_customAssembly.IsDefined(typeof(DataContractAttribute), false));
}
[Fact]
public void GetExportedTypesTest()
{
Type[] exportedTypes = _customAssembly.GetExportedTypes();
Assert.NotEmpty(exportedTypes);
Assert.All(exportedTypes,
(et) => Assert.Equal(ProjectionConstants.CustomType, et.GetType().FullName));
}
[Fact]
public void GetLoadedModulesTest()
{
Module[] loadedModules = _customAssembly.GetLoadedModules(true);
Assert.Single(loadedModules);
if (PlatformDetection.HasAssemblyFiles)
Assert.Equal(typeof(CustomAssemblyTests).Assembly.GetName().Name + ".dll", loadedModules[0].Name);
Assert.All(loadedModules,
(mod) => Assert.Equal(ProjectionConstants.CustomModule, mod.GetType().FullName));
}
[Fact]
public void GetManifestResourceInfoTest()
{
IEnumerable<ManifestResourceInfo> manifestResourceInfos = _customAssembly.GetManifestResourceNames()
.Select(mrn => _customAssembly.GetManifestResourceInfo(mrn));
Assert.NotEmpty(manifestResourceInfos);
Assert.All(manifestResourceInfos,
(mri) => Assert.Equal(ProjectionConstants.ProjectingManifestResourceInfo, mri.GetType().FullName));
}
[Fact]
public void GetModulesTest()
{
Module[] modules = _customAssembly.GetModules(true);
Assert.Single(modules);
if (PlatformDetection.HasAssemblyFiles)
Assert.Equal(typeof(CustomAssemblyTests).Assembly.GetName().Name + ".dll", modules[0].Name);
Assert.All(modules,
(module) => Assert.Equal(ProjectionConstants.CustomModule, module.GetType().FullName));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.HasAssemblyFiles))]
public void GetModuleTest()
{
Module[] modules = _customAssembly.GetModules(true);
string moduleName = modules[0].Name;
Module module = _customAssembly.GetModule(moduleName);
Assert.NotNull(module);
Assert.Equal(ProjectionConstants.CustomModule, module.GetType().FullName);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtimelab/issues/155", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
public void GetSatelliteAssemblyTest()
{
Assert.Throws<FileNotFoundException>(() => _customAssembly.GetSatelliteAssembly(CultureInfo.InvariantCulture));
Assert.Throws<FileNotFoundException>(() => _customAssembly.GetSatelliteAssembly(CultureInfo.InvariantCulture, Version.Parse("1.0.0.0")));
}
[Fact]
public void GetTypeTest()
{
Type type = _customAssembly.GetType(typeof(TestObject).FullName, throwOnError: false, ignoreCase: false);
Assert.NotNull(type);
Assert.Equal(ProjectionConstants.CustomType, type.GetType().FullName);
}
[Fact]
public void GetTypesTest()
{
Type[] types = _customAssembly.GetTypes();
Assert.NotEmpty(types);
Assert.All(types,
(type) => Assert.Equal(ProjectionConstants.CustomType, type.GetType().FullName));
}
[Fact]
public void EqualsTest()
{
var customReflectionContext = new TestCustomReflectionContext();
Assembly assembly = typeof(CustomAssemblyTests).Assembly;
Assembly customAssembly = customReflectionContext.MapAssembly(assembly);
// Projector is not the same.
Assert.NotEqual(customAssembly, _customAssembly);
}
[Fact]
public void GetHashCodeTest()
{
Assembly assembly = typeof(CustomAssemblyTests).Assembly;
Assert.NotEqual(assembly.GetHashCode(), _customAssembly.GetHashCode());
}
}
}