mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 14:11:10 +00:00
Hardens test assertions and adds missing test coverage.
This commit is contained in:
1 parent
6b86fd5805
commit
1189bbaf36
489 files changed
+39982
-512
No files matched your search
+97
@@ -0,0 +1,97 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Plugins;
|
||||
using Lumina.Plugins.Contracts.Common.Models.DTO.Settings;
|
||||
using Lumina.Plugins.Contracts.Fixtures.Common.Models.DTO.Settings;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.Contracts.UnitTests.Common.Models.DTO.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PluginSettingDescriptorDto"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PluginSettingDescriptorDtoTests
|
||||
{
|
||||
private readonly PluginSettingDescriptorDtoFixture _pluginSettingDescriptorDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenConstructedWithValues_ShouldPreserveAllValues()
|
||||
{
|
||||
// Arrange
|
||||
IReadOnlyList<string> allowedValues = ["Fiction", "Non-Fiction"];
|
||||
|
||||
// Act
|
||||
PluginSettingDescriptorDto result = _pluginSettingDescriptorDtoFixture.Create(
|
||||
key: "Theme",
|
||||
label: "Theme",
|
||||
type: PluginSettingType.Select,
|
||||
defaultValue: "Fiction",
|
||||
allowedValues: allowedValues);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Theme", result.Key);
|
||||
Assert.Equal("Theme", result.Label);
|
||||
Assert.Equal(PluginSettingType.Select, result.Type);
|
||||
Assert.Equal("Fiction", result.DefaultValue);
|
||||
Assert.Same(allowedValues, result.AllowedValues);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenDefaultValuesAreNotProvided_ShouldDefaultToNull()
|
||||
{
|
||||
// Act
|
||||
PluginSettingDescriptorDto result = _pluginSettingDescriptorDtoFixture.Create(defaultValue: null, allowedValues: null);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.DefaultValue);
|
||||
Assert.Null(result.AllowedValues);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equality_WhenTwoDescriptorsHaveTheSameValues_ShouldBeEqual()
|
||||
{
|
||||
// Arrange
|
||||
IReadOnlyList<string> allowedValues = ["A"];
|
||||
|
||||
// Act
|
||||
PluginSettingDescriptorDto first = _pluginSettingDescriptorDtoFixture.Create(key: "Key", label: "Label", type: PluginSettingType.Text, defaultValue: "value", allowedValues: allowedValues);
|
||||
PluginSettingDescriptorDto second = _pluginSettingDescriptorDtoFixture.Create(key: "Key", label: "Label", type: PluginSettingType.Text, defaultValue: "value", allowedValues: allowedValues);
|
||||
bool result = first == second;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
Assert.Equal(first, second);
|
||||
Assert.Equal(first.GetHashCode(), second.GetHashCode());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equality_WhenDescriptorsHaveDifferentAllowedValueInstances_ShouldNotBeEqual()
|
||||
{
|
||||
// Arrange
|
||||
PluginSettingDescriptorDto first = _pluginSettingDescriptorDtoFixture.Create(key: "Key", label: "Label", type: PluginSettingType.Text, defaultValue: "value", allowedValues: ["A"]);
|
||||
PluginSettingDescriptorDto second = _pluginSettingDescriptorDtoFixture.Create(key: "Key", label: "Label", type: PluginSettingType.Text, defaultValue: "value", allowedValues: ["A"]);
|
||||
|
||||
// Act
|
||||
bool result = first == second;
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equality_WhenTwoDescriptorsHaveDifferentKeys_ShouldNotBeEqual()
|
||||
{
|
||||
// Arrange
|
||||
PluginSettingDescriptorDto first = _pluginSettingDescriptorDtoFixture.Create(key: "KeyOne", label: "Label", type: PluginSettingType.Text);
|
||||
PluginSettingDescriptorDto second = _pluginSettingDescriptorDtoFixture.Create(key: "KeyTwo", label: "Label", type: PluginSettingType.Text);
|
||||
|
||||
// Act
|
||||
bool result = first == second;
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
Assert.NotEqual(first, second);
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Contracts.DTO.Common;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Plugins.Contracts.Core.Metadata;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.Contracts.UnitTests.Core.Metadata;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="IRemoteMetadataProvider{TLookup, TMetadata}"/> interface.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class IRemoteMetadataProviderGenericTests
|
||||
{
|
||||
private readonly BookMetadataLookupDtoFixture _bookMetadataLookupDtoFixture = new();
|
||||
private readonly BookMetadataDtoFixture _bookMetadataDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public async Task GetSearchResultsAsync_WhenCalledThroughBaseInterface_ShouldForwardToTypedImplementation()
|
||||
{
|
||||
// Arrange
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create();
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookMetadataDto expectedMetadata = _bookMetadataDtoFixture.Create(title: "Search Result", includeOptionalProperties: false);
|
||||
TestBookMetadataProvider provider = new(expectedMetadata);
|
||||
IRemoteMetadataProvider baseProvider = provider;
|
||||
|
||||
// Act
|
||||
IReadOnlyList<MetadataDto> result = await baseProvider.GetSearchResultsAsync(lookup, cancellationToken);
|
||||
|
||||
// Assert
|
||||
BookMetadataDto metadata = Assert.IsType<BookMetadataDto>(Assert.Single(result));
|
||||
Assert.Equal(expectedMetadata, metadata);
|
||||
Assert.Same(lookup, provider.ReceivedLookup);
|
||||
Assert.Equal(cancellationToken, provider.ReceivedCancellationToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenCalledThroughBaseInterface_ShouldForwardToTypedImplementation()
|
||||
{
|
||||
// Arrange
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create();
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookMetadataDto expectedMetadata = _bookMetadataDtoFixture.Create(title: "Exact Result", includeOptionalProperties: false);
|
||||
TestBookMetadataProvider provider = new(expectedMetadata);
|
||||
IRemoteMetadataProvider baseProvider = provider;
|
||||
|
||||
// Act
|
||||
MetadataDto? result = await baseProvider.GetMetadataAsync(lookup, cancellationToken);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedMetadata, Assert.IsType<BookMetadataDto>(result));
|
||||
Assert.Same(lookup, provider.ReceivedLookup);
|
||||
Assert.Equal(cancellationToken, provider.ReceivedCancellationToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSearchResultsAsync_WhenLookupIsOfAnotherRuntimeType_ShouldThrowInvalidCastException()
|
||||
{
|
||||
// Arrange
|
||||
MetadataLookupDto otherLookup = new OtherMetadataLookupDto();
|
||||
TestBookMetadataProvider provider = new(_bookMetadataDtoFixture.Create(title: "Search Result", includeOptionalProperties: false));
|
||||
IRemoteMetadataProvider baseProvider = provider;
|
||||
|
||||
// Act
|
||||
async Task Act()
|
||||
{
|
||||
await baseProvider.GetSearchResultsAsync(otherLookup, CancellationToken.None);
|
||||
}
|
||||
|
||||
// Assert
|
||||
await Assert.ThrowsAsync<InvalidCastException>(Act);
|
||||
Assert.Null(provider.ReceivedLookup);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenLookupIsOfAnotherRuntimeType_ShouldThrowInvalidCastException()
|
||||
{
|
||||
// Arrange
|
||||
MetadataLookupDto otherLookup = new OtherMetadataLookupDto();
|
||||
TestBookMetadataProvider provider = new(_bookMetadataDtoFixture.Create(title: "Exact Result", includeOptionalProperties: false));
|
||||
IRemoteMetadataProvider baseProvider = provider;
|
||||
|
||||
// Act
|
||||
async Task Act()
|
||||
{
|
||||
await baseProvider.GetMetadataAsync(otherLookup, CancellationToken.None);
|
||||
}
|
||||
|
||||
// Assert
|
||||
await Assert.ThrowsAsync<InvalidCastException>(Act);
|
||||
Assert.Null(provider.ReceivedLookup);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test double for the typed metadata provider interface that records the forwarded arguments.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
private sealed class TestBookMetadataProvider : IRemoteMetadataProvider<BookMetadataLookupDto, BookMetadataDto>
|
||||
{
|
||||
private readonly BookMetadataDto _metadata;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the lookup that was forwarded to the typed implementation.
|
||||
/// </summary>
|
||||
public BookMetadataLookupDto? ReceivedLookup { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the cancellation token that was forwarded to the typed implementation.
|
||||
/// </summary>
|
||||
public CancellationToken ReceivedCancellationToken { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TestBookMetadataProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="metadata">The metadata returned by the typed implementation.</param>
|
||||
public TestBookMetadataProvider(BookMetadataDto metadata)
|
||||
{
|
||||
_metadata = metadata;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string Name => "Test Provider";
|
||||
|
||||
/// <inheritdoc/>
|
||||
public LibraryType SupportedLibraryType => LibraryType.Book;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool RequiresWebAccess => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<IReadOnlyList<BookMetadataDto>> GetSearchResultsAsync(BookMetadataLookupDto lookup, CancellationToken cancellationToken)
|
||||
{
|
||||
ReceivedLookup = lookup;
|
||||
ReceivedCancellationToken = cancellationToken;
|
||||
return Task.FromResult<IReadOnlyList<BookMetadataDto>>([_metadata]);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<BookMetadataDto?> GetMetadataAsync(BookMetadataLookupDto lookup, CancellationToken cancellationToken)
|
||||
{
|
||||
ReceivedLookup = lookup;
|
||||
ReceivedCancellationToken = cancellationToken;
|
||||
return Task.FromResult<BookMetadataDto?>(_metadata);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test double deriving from <see cref="MetadataLookupDto"/> for a runtime type other than <see cref="BookMetadataLookupDto"/>.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
private sealed record OtherMetadataLookupDto : MetadataLookupDto;
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="Xunit.CollectionBehaviorAttribute">
|
||||
<_Parameter1>DisableTestParallelization = true</_Parameter1>
|
||||
<_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="5.0.0-preview0011" />
|
||||
<PackageReference Include="AutoFixture.Xunit2" Version="5.0.0-preview0011" />
|
||||
<PackageReference Include="Bogus" Version="35.6.5" />
|
||||
<PackageReference Include="coverlet.collector" Version="10.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.11" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
|
||||
<PackageReference Include="NSubstitute" Version="6.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Lumina.Plugins.Contracts\Lumina.Plugins.Contracts.csproj" />
|
||||
<ProjectReference Include="..\..\Common\Lumina.Plugins.Contracts.Fixtures\Lumina.Plugins.Contracts.Fixtures.csproj" />
|
||||
<ProjectReference Include="..\..\Common\Lumina.Contracts.Fixtures\Lumina.Contracts.Fixtures.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in new issue
Block a user