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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Tests;
using System.Runtime.CompilerServices;
using Xunit;
[assembly:
Attr(77, name = "AttrSimple"),
Int32Attr(77, name = "Int32AttrSimple"),
Int64Attr(77, name = "Int64AttrSimple"),
StringAttr("hello", name = "StringAttrSimple"),
EnumAttr(PublicEnum.Case1, name = "EnumAttrSimple"),
TypeAttr(typeof(object), name = "TypeAttrSimple")]
[assembly: CompilationRelaxations(8)]
[assembly: Debuggable((DebuggableAttribute.DebuggingModes)263)]
[assembly: CLSCompliant(false)]
namespace System.Reflection.Tests
{
public class AssemblyTests
{
[Theory]
[InlineData(typeof(Int32Attr))]
[InlineData(typeof(Int64Attr))]
[InlineData(typeof(StringAttr))]
[InlineData(typeof(EnumAttr))]
[InlineData(typeof(TypeAttr))]
[InlineData(typeof(CompilationRelaxationsAttribute))]
[InlineData(typeof(AssemblyTitleAttribute))]
[InlineData(typeof(AssemblyDescriptionAttribute))]
[InlineData(typeof(AssemblyCompanyAttribute))]
[InlineData(typeof(CLSCompliantAttribute))]
[InlineData(typeof(DebuggableAttribute))]
[InlineData(typeof(Attr))]
public void CustomAttributes(Type type)
{
Assembly assembly = Helpers.ExecutingAssembly;
IEnumerable<CustomAttributeData> attributesData = assembly.CustomAttributes;
bool result = attributesData.Any(customAttribute => customAttribute.AttributeType.Equals(type));
Assert.True(result, $"Did not find custom attribute of type {type}.");
ICustomAttributeProvider attributeProvider = assembly;
Assert.Equal(1, attributeProvider.GetCustomAttributes(type, false).Length);
Assert.True(attributeProvider.IsDefined(type, false));
object[] customAttributes = attributeProvider.GetCustomAttributes(false);
result = customAttributes.Any(attribute => attribute.GetType().Equals(type));
Assert.True(result, $"Did not find custom attribute of type {type}.");
}
[Theory]
[InlineData(typeof(int), false)]
[InlineData(typeof(Attr), true)]
[InlineData(typeof(Int32Attr), true)]
[InlineData(typeof(Int64Attr), true)]
[InlineData(typeof(StringAttr), true)]
[InlineData(typeof(EnumAttr), true)]
[InlineData(typeof(TypeAttr), true)]
[InlineData(typeof(ObjectAttr), true)]
[InlineData(typeof(NullAttr), true)]
public void DefinedTypes(Type type, bool expected)
{
IEnumerable<TypeInfo> customAttrs = Helpers.ExecutingAssembly.DefinedTypes;
bool result = customAttrs.Any(typeInfo => typeInfo.AsType().Equals(type));
Assert.Equal(expected, result);
}
[Theory]
[InlineData("EmbeddedImage.png", true)]
[InlineData("NoSuchFile", false)]
public void EmbeddedFiles(string resource, bool exists)
{
string[] resources = Helpers.ExecutingAssembly.GetManifestResourceNames();
Assert.True(exists == resources.Contains(resource), $"{resource} resource expected existence: '{exists}', but got '{!exists}'");
Stream resourceStream = Helpers.ExecutingAssembly.GetManifestResourceStream(resource);
Assert.True(exists == (resourceStream != null), $"{resource} resource expected existence: '{exists}', but got '{!exists}'");
}
[Fact]
public void EntryPoint_ExecutingAssembly_IsNull()
{
Assert.Null(Helpers.ExecutingAssembly.EntryPoint);
}
public static IEnumerable<object[]> Equals_TestData()
{
yield return new object[] { Assembly.Load(new AssemblyName(typeof(int).GetTypeInfo().Assembly.FullName)), Assembly.Load(new AssemblyName(typeof(int).GetTypeInfo().Assembly.FullName)), true };
yield return new object[] { Assembly.Load(new AssemblyName(typeof(List<int>).GetTypeInfo().Assembly.FullName)), Assembly.Load(new AssemblyName(typeof(List<int>).GetTypeInfo().Assembly.FullName)), true };
yield return new object[] { Assembly.Load(new AssemblyName(typeof(List<int>).GetTypeInfo().Assembly.FullName)), Helpers.ExecutingAssembly, false };
}
[Theory]
[MemberData(nameof(Equals_TestData))]
public void Equals(Assembly assembly1, Assembly assembly2, bool expected)
{
Assert.Equal(expected, assembly1.Equals(assembly2));
}
[Theory]
[InlineData(typeof(AssemblyPublicClass), true)]
[InlineData(typeof(AssemblyTests), true)]
[InlineData(typeof(AssemblyPublicClass.PublicNestedClass), true)]
[InlineData(typeof(PublicEnum), true)]
[InlineData(typeof(BaseClass), true)]
[InlineData(typeof(AssemblyGenericPublicClass<>), true)]
[InlineData(typeof(AssemblyInternalClass), false)]
public void ExportedTypes(Type type, bool expected)
{
Assembly assembly = Helpers.ExecutingAssembly;
Assert.Equal(assembly.GetExportedTypes(), assembly.ExportedTypes);
Assert.Equal(expected, assembly.ExportedTypes.Contains(type));
}
[Fact]
public void GetEntryAssembly()
{
Assert.NotNull(Assembly.GetEntryAssembly());
Assert.True(Assembly.GetEntryAssembly().ToString().StartsWith("xunit.console.netcore", StringComparison.OrdinalIgnoreCase));
}
public static IEnumerable<object[]> GetHashCode_TestData()
{
yield return new object[] { LoadSystemRuntimeAssembly() };
yield return new object[] { LoadSystemCollectionsAssembly() };
yield return new object[] { LoadSystemReflectionAssembly() };
yield return new object[] { typeof(AssemblyTests).GetTypeInfo().Assembly };
}
[Theory]
[MemberData(nameof(GetHashCode_TestData))]
public void GetHashCode(Assembly assembly)
{
int hashCode = assembly.GetHashCode();
Assert.False((hashCode == -1) || (hashCode == 0));
}
[Theory]
[InlineData("System.Reflection.Tests.AssemblyPublicClass", true)]
[InlineData("System.Reflection.Tests.AssemblyInternalClass", true)]
[InlineData("System.Reflection.Tests.PublicEnum", true)]
[InlineData("System.Reflection.Tests.PublicStruct", true)]
[InlineData("AssemblyPublicClass", false)]
[InlineData("NoSuchType", false)]
public void GetType(string name, bool exists)
{
Type type = Helpers.ExecutingAssembly.GetType(name);
if (exists)
{
Assert.Equal(name, type.FullName);
}
else
{
Assert.Null(type);
}
}
public static IEnumerable<object[]> IsDynamic_TestData()
{
yield return new object[] { Helpers.ExecutingAssembly, false };
yield return new object[] { LoadSystemCollectionsAssembly(), false };
}
[Theory]
[MemberData(nameof(IsDynamic_TestData))]
public void IsDynamic(Assembly assembly, bool expected)
{
Assert.Equal(expected, assembly.IsDynamic);
}
public static IEnumerable<object[]> Load_TestData()
{
yield return new object[] { new AssemblyName(typeof(int).GetTypeInfo().Assembly.FullName) };
yield return new object[] { new AssemblyName(typeof(List<int>).GetTypeInfo().Assembly.FullName) };
yield return new object[] { new AssemblyName(typeof(AssemblyName).GetTypeInfo().Assembly.FullName) };
}
[Theory]
[MemberData(nameof(Load_TestData))]
public void Load(AssemblyName assemblyRef)
{
Assert.NotNull(Assembly.Load(assemblyRef));
}
[Fact]
public void Load_Invalid()
{
Assert.Throws<ArgumentNullException>("assemblyRef", () => Assembly.Load(null)); // AssemblyRef is null
Assert.Throws<FileNotFoundException>(() => Assembly.Load(new AssemblyName("no such assembly"))); // No such assembly
}
[Fact]
public void Location_ExecutingAssembly_IsNotNull()
{
// This test applies on all platforms including .NET Native. Location must at least be non-null (it can be empty).
// System.Reflection.CoreCLR.Tests adds tests that expect more than that.
Assert.NotNull(Helpers.ExecutingAssembly.Location);
}
[Fact]
public void CodeBase()
{
Assert.NotEmpty(Helpers.ExecutingAssembly.CodeBase);
}
[Fact]
public void ImageRuntimeVersion()
{
Assert.NotEmpty(Helpers.ExecutingAssembly.ImageRuntimeVersion);
}
public static IEnumerable<object[]> CreateInstance_TestData()
{
yield return new object[] { Helpers.ExecutingAssembly, typeof(AssemblyPublicClass).FullName, typeof(AssemblyPublicClass) };
yield return new object[] { typeof(int).GetTypeInfo().Assembly, typeof(int).FullName, typeof(int) };
yield return new object[] { typeof(int).GetTypeInfo().Assembly, typeof(Dictionary<int, string>).FullName, typeof(Dictionary<int, string>) };
}
[Theory]
[MemberData(nameof(CreateInstance_TestData))]
public void CreateInstance(Assembly assembly, string typeName, Type expectedType)
{
Assert.IsType(expectedType, assembly.CreateInstance(typeName));
Assert.IsType(expectedType, assembly.CreateInstance(typeName, false));
Assert.IsType(expectedType, assembly.CreateInstance(typeName, true));
Assert.IsType(expectedType, assembly.CreateInstance(typeName.ToUpper(), true));
Assert.IsType(expectedType, assembly.CreateInstance(typeName.ToLower(), true));
}
public static IEnumerable<object[]> CreateInstance_Invalid_TestData()
{
yield return new object[] { "", typeof(ArgumentException) };
yield return new object[] { null, typeof(ArgumentNullException) };
yield return new object[] { typeof(AssemblyClassWithPrivateCtor).FullName, typeof(MissingMethodException) };
yield return new object[] { typeof(AssemblyClassWithNoDefaultCtor).FullName, typeof(MissingMethodException) };
}
[Theory]
[MemberData(nameof(CreateInstance_Invalid_TestData))]
public void CreateInstance_Invalid(string typeName, Type exceptionType)
{
Assembly assembly = Helpers.ExecutingAssembly;
Assert.Throws(exceptionType, () => Helpers.ExecutingAssembly.CreateInstance(typeName));
Assert.Throws(exceptionType, () => Helpers.ExecutingAssembly.CreateInstance(typeName, true));
Assert.Throws(exceptionType, () => Helpers.ExecutingAssembly.CreateInstance(typeName, false));
}
[Fact]
public void CreateQualifiedName()
{
string assemblyName = Helpers.ExecutingAssembly.ToString();
Assert.Equal(typeof(AssemblyTests).FullName + ", " + assemblyName, Assembly.CreateQualifiedName(assemblyName, typeof(AssemblyTests).FullName));
}
[Fact]
public void GetReferencedAssemblies()
{
// It is too brittle to depend on the assembly references so we just call the method and check that it does not throw.
AssemblyName[] assemblies = Helpers.ExecutingAssembly.GetReferencedAssemblies();
Assert.NotEmpty(assemblies);
}
public static IEnumerable<object[]> Modules_TestData()
{
yield return new object[] { LoadSystemCollectionsAssembly() };
yield return new object[] { LoadSystemReflectionAssembly() };
}
[Theory]
[MemberData(nameof(Modules_TestData))]
public void Modules(Assembly assembly)
{
Assert.NotEmpty(assembly.Modules);
foreach (Module module in assembly.Modules)
{
Assert.NotNull(module);
}
}
public IEnumerable<object[]> ToString_TestData()
{
yield return new object[] { Helpers.ExecutingAssembly, "System.Reflection.Tests" };
yield return new object[] { Assembly.Load(new AssemblyName(typeof(List<int>).GetTypeInfo().Assembly.FullName)), "PublicKeyToken=" };
}
[Theory]
public void ToString(Assembly assembly, string expected)
{
Assert.True(assembly.ToString().Contains(expected));
Assert.Equal(assembly.ToString(), assembly.FullName);
}
private static Assembly LoadSystemCollectionsAssembly()
{
// Force System.collections to be linked statically
List<int> li = new List<int>();
li.Add(1);
return Assembly.Load(new AssemblyName(typeof(List<int>).GetTypeInfo().Assembly.FullName));
}
private static Assembly LoadSystemReflectionAssembly()
{
// Force System.Reflection to be linked statically
return Assembly.Load(new AssemblyName(typeof(AssemblyName).GetTypeInfo().Assembly.FullName));
}
private static Assembly LoadSystemRuntimeAssembly()
{
// Load System.Runtime
return Assembly.Load(new AssemblyName(typeof(int).GetTypeInfo().Assembly.FullName)); ;
}
}
public struct PublicStruct { }
public class AssemblyPublicClass
{
public class PublicNestedClass { }
}
public class AssemblyGenericPublicClass<T> { }
internal class AssemblyInternalClass { }
public class AssemblyClassWithPrivateCtor
{
private AssemblyClassWithPrivateCtor() { }
}
public class AssemblyClassWithNoDefaultCtor
{
public AssemblyClassWithNoDefaultCtor(int x) { }
}
}
|