-
-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathAssemblyExtensionsTest.cs
More file actions
87 lines (65 loc) · 2.92 KB
/
AssemblyExtensionsTest.cs
File metadata and controls
87 lines (65 loc) · 2.92 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
using System.Reflection;
using System.Runtime.Loader;
namespace UnitTest.Extensions;
public class AssemblyExtensionsTest
{
[Fact]
public void GetUniqueName_Ok()
{
var sc = new ServiceCollection();
sc.AddBootstrapBlazor();
var provider = sc.BuildServiceProvider();
var cacheManager = provider.GetRequiredService<ICacheManager>();
cacheManager.GetStartTime();
var type = Type.GetType("BootstrapBlazor.Components.AssemblyExtensions, BootstrapBlazor");
Assert.NotNull(type);
var methodInfo = type.GetMethod("GetUniqueName", BindingFlags.Static | BindingFlags.Public);
Assert.NotNull(methodInfo);
var actual = methodInfo.Invoke(null, [GetType().Assembly]);
Assert.Equal(GetType().Assembly.GetName().Name, actual);
actual = methodInfo.Invoke(null, [new MockAssembly()]);
Assert.Equal("", actual);
var loader = new AssemblyLoadContext("Test", true);
var assemblyFile = Path.Combine(AppContext.BaseDirectory, "Plugins", "Test.dll");
Assert.True(File.Exists(assemblyFile));
var assembly = loader.LoadFromAssemblyPath(assemblyFile);
actual = methodInfo.Invoke(null, [assembly]);
Assert.Contains("Test-", actual?.ToString());
loader.Unload();
}
[Fact]
public void GetUniqueTypeName_Ok()
{
var type = Type.GetType("BootstrapBlazor.Components.TypeExtensions, BootstrapBlazor");
Assert.NotNull(type);
var methodInfo = type.GetMethod("GetUniqueTypeName", BindingFlags.Static | BindingFlags.Public);
Assert.NotNull(methodInfo);
var actual = methodInfo.Invoke(null, [GetType()]);
Assert.Equal(GetType().FullName, actual);
actual = methodInfo.Invoke(null, [new MockTypeInfo()]);
Assert.Equal("", actual);
var loader = new AssemblyLoadContext("Test", true);
var assemblyFile = Path.Combine(AppContext.BaseDirectory, "Plugins", "Test.dll");
Assert.True(File.Exists(assemblyFile));
var assembly = loader.LoadFromAssemblyPath(assemblyFile);
type = assembly.GetType("ConsoleApp3.TestClass");
actual = methodInfo.Invoke(null, [type]);
Assert.Contains("ConsoleApp3.TestClass-", actual?.ToString());
loader.Unload();
}
class MockTypeInfo : TypeDelegator
{
public override string? FullName => null;
public override bool IsCollectible => false;
public override RuntimeTypeHandle TypeHandle => new();
}
class MockAssembly : Assembly
{
public override AssemblyName GetName() => new();
public override bool IsCollectible => false;
}
}