Skip to content

Commit

Permalink
test(GetUniqueName): add uni test improve code coverage (#3288)
Browse files Browse the repository at this point in the history
* chore: bump version 8.0.1

* chore: 移除注释

* chore: 更新依赖版本

* revert: 撤销打包工具误删除

* refactor: 重构代码

* test: 增加单元测试

* test: 更新单元测试

* chore: bump version 8.4.6
  • Loading branch information
ArgoZhang authored Apr 14, 2024
1 parent 3276c05 commit 70616a7
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.4.5</Version>
<Version>8.4.6</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
6 changes: 3 additions & 3 deletions src/BootstrapBlazor/Extensions/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static class AssemblyExtensions
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
public static string GetUniqueName(this Assembly assembly) => (assembly.IsCollectible
? $"{assembly.GetHashCode()}"
: $"{assembly.GetName().Name}");
public static string GetUniqueName(this Assembly assembly) => assembly.IsCollectible
? $"{assembly.GetName().Name}-{assembly.GetHashCode()}"
: $"{assembly.GetName().Name}";
}
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void EnsureNoAuthenticationSchemeSpecified()
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string GetUniqueTypeName(this Type type) => (type.IsCollectible
public static string GetUniqueTypeName(this Type type) => type.IsCollectible
? $"{type.FullName}-{type.TypeHandle.Value}"
: $"{type.FullName}");
: $"{type.FullName}";
}
6 changes: 0 additions & 6 deletions src/BootstrapBlazor/wwwroot/src/css/motronic.css
Original file line number Diff line number Diff line change
Expand Up @@ -357,21 +357,15 @@
border-color: var(--bs-secondary);
}

/*dropdonw*/
.dropdown-menu {
--bs-dropdown-item-padding-y: 6px;
--bs-dropdown-item-padding-x: 20px;
}
/*end dropdown*/

/*tree*/
.tree-view {
--bb-tree-item-hover-color: var(--bs-primary);
}
/*end tree*/

/*btn*/
.btn-xs, .btn-group-xs > .btn {
--bs-btn-border-radius: .375rem;
}
/*end btn*/
1 change: 1 addition & 0 deletions src/Bundle.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<ItemGroup>
<PackageReference Include="BuildBundlerMinifierPlus" Version="5.3.0" Condition="'$(Configuration)'=='Debug' and '$(TargetFramework)' == 'net8.0'" />
<PackageReference Include="AspNetCore.SassCompiler" Version="1.75.0" Condition="'$(Configuration)'=='Debug' and '$(TargetFramework)' == 'net8.0'" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.0.1-beta01</Version>
<Version>8.0.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="8.4.4-beta01" />
<PackageReference Include="BootstrapBlazor" Version="8.4.5" />
</ItemGroup>

<ItemGroup>
Expand Down
82 changes: 82 additions & 0 deletions test/UnitTest/Extensions/AssemblyExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using System.Reflection;
using System.Runtime.Loader;

namespace UnitTest.Extensions;

public class AssemblyExtensionsTest
{
[Fact]
public void GetUniqueName_Ok()
{
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;
}

//[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "GetUniqueName")]
//static extern int CallMethod(Assembly assembly);
}
Binary file added test/UnitTest/Plugins/Test.dll
Binary file not shown.
7 changes: 7 additions & 0 deletions test/UnitTest/UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
<ProjectReference Include="..\..\src\BootstrapBlazor\BootstrapBlazor.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="Plugins\Test.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Remove="Plugins\Test.dll" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down

0 comments on commit 70616a7

Please sign in to comment.