Hardens test assertions and adds missing test coverage.

This commit is contained in:
BinaryAlley committed 2026-08-18 08:28:16 -07:00
1 parent 6b86fd5805
commit 1189bbaf36
489 files changed
+39982 -512

No files matched your search

@@ -0,0 +1,197 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Primitives;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Domain.UnitTests.Common.Primitives;
/// <summary>
/// Contains unit tests for the <see cref="Error"/> class and its derived error records.
/// </summary>
[ExcludeFromCodeCoverage]
public class ErrorTests
{
[Fact]
public void NotFound_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.NotFound();
// Assert
AssertError(error, ErrorType.NotFound, "General.NotFound", "A 'Not Found' error has occurred.");
}
[Fact]
public void NotFound_WhenCalledWithCodeAndDescription_ShouldUseProvidedValues()
{
// Act
Error error = Error.NotFound("Library.NotFound", "The library was not found.");
// Assert
AssertError(error, ErrorType.NotFound, "Library.NotFound", "The library was not found.");
}
[Fact]
public void InvalidOperation_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.InvalidOperation();
// Assert
AssertError(error, ErrorType.Unexpected, "General.InvalidOperation", "An invalid operation error has occurred.");
}
[Fact]
public void Forbidden_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.Forbidden();
// Assert
AssertError(error, ErrorType.Forbidden, "General.Forbidden", "A 'Forbidden' error has occurred.");
}
[Fact]
public void Failure_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.Failure();
// Assert
AssertError(error, ErrorType.Failure, "General.Failure", "A failure has occurred.");
}
[Fact]
public void Validation_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.Validation();
// Assert
AssertError(error, ErrorType.Validation, "General.Validation", "A validation error has occurred.");
}
[Fact]
public void Validation_WhenCalledWithCodeAndDescription_ShouldUseProvidedValues()
{
// Act
Error error = Error.Validation("Title.CannotBeEmpty", "The title cannot be empty.");
// Assert
AssertError(error, ErrorType.Validation, "Title.CannotBeEmpty", "The title cannot be empty.");
}
[Fact]
public void Conflict_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.Conflict();
// Assert
AssertError(error, ErrorType.Conflict, "General.Conflict", "A conflict error has occurred.");
}
[Fact]
public void Timeout_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.Timeout();
// Assert
AssertError(error, ErrorType.Unexpected, "General.Timeout", "The operation timed out.");
}
[Fact]
public void Unauthorized_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.Unauthorized();
// Assert
AssertError(error, ErrorType.Unauthorized, "General.Unauthorized", "An 'Unauthorized' error has occurred.");
}
[Fact]
public void Unexpected_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.Unexpected();
// Assert
AssertError(error, ErrorType.Unexpected, "General.Unexpected", "An unexpected error has occurred.");
}
[Fact]
public void Internal_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.Internal();
// Assert
AssertError(error, ErrorType.Unexpected, "General.Internal", "An internal error has occurred.");
}
[Fact]
public void ResourceUnavailable_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.ResourceUnavailable();
// Assert
AssertError(error, ErrorType.Unexpected, "General.ResourceUnavailable", "The required resource is currently unavailable.");
}
[Fact]
public void NotImplemented_WhenCalledWithoutArguments_ShouldUseDefaultCodeAndDescription()
{
// Act
Error error = Error.NotImplemented();
// Assert
AssertError(error, ErrorType.Unexpected, "General.NotImplemented", "The requested functionality is not implemented.");
}
[Fact]
public void ToString_WhenCalled_ShouldReturnRecordRepresentation()
{
// Arrange
Error error = Error.NotFound("Library.NotFound", "The library was not found.");
// Act
string result = error.ToString();
// Assert
Assert.Equal("NotFoundError { Type = NotFound, Code = Library.NotFound, Description = The library was not found. }", result);
}
[Fact]
public void Equals_WhenSameCodeAndDescription_ShouldReturnTrue()
{
// Act
Error firstError = Error.Validation("Code", "Description");
Error secondError = Error.Validation("Code", "Description");
// Assert
Assert.Equal(firstError, secondError);
Assert.Equal(firstError.GetHashCode(), secondError.GetHashCode());
}
[Fact]
public void Equals_WhenDifferentCode_ShouldReturnFalse()
{
// Act
Error firstError = Error.Validation("Code1", "Description");
Error secondError = Error.Validation("Code2", "Description");
// Assert
Assert.NotEqual(firstError, secondError);
}
private static void AssertError(Error error, ErrorType expectedType, string expectedCode, string expectedDescription)
{
Assert.Equal(expectedType, error.Type);
Assert.Equal(expectedCode, error.Code);
Assert.Equal(expectedDescription, error.Description);
}
}
@@ -0,0 +1,377 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Domain.UnitTests.Common.Primitives;
/// <summary>
/// Contains unit tests for the <see cref="Result{TValue}"/> structure.
/// </summary>
[ExcludeFromCodeCoverage]
public class ResultTests
{
[Fact]
public void Success_WhenCalledWithValue_ShouldCreateSuccessfulResult()
{
// Act
Result<string> result = Result<string>.Success("test");
// Assert
Assert.True(result.IsSuccess);
Assert.False(result.IsFailure);
Assert.Equal("test", result.Value);
}
[Fact]
public void Failure_WhenCalledWithSingleError_ShouldCreateFailedResult()
{
// Arrange
Error error = Error.Validation("Code", "Description");
// Act
Result<string> result = Result<string>.Failure(error);
// Assert
Assert.False(result.IsSuccess);
Assert.True(result.IsFailure);
Assert.Single(result.Errors);
Assert.Equal(error, result.FirstError);
}
[Fact]
public void Failure_WhenCalledWithMultipleErrors_ShouldCreateFailedResultWithAllErrors()
{
// Arrange
Error firstError = Error.Validation("Code1", "Description1");
Error secondError = Error.NotFound("Code2", "Description2");
// Act
Result<string> result = Result<string>.Failure(new List<Error>([firstError, secondError]));
// Assert
Assert.True(result.IsFailure);
Assert.Equal(2, result.Errors.Count);
Assert.Equal(firstError, result.Errors[0]);
Assert.Equal(secondError, result.Errors[1]);
Assert.Equal(firstError, result.FirstError);
}
[Fact]
public void ImplicitConversion_WhenConvertingValue_ShouldCreateSuccessfulResult()
{
// Act
Result<string> result = "test";
// Assert
Assert.True(result.IsSuccess);
Assert.Equal("test", result.Value);
}
[Fact]
public void ImplicitConversion_WhenConvertingError_ShouldCreateFailedResult()
{
// Arrange
Error error = Error.Conflict("Code", "Description");
// Act
Result<string> result = error;
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
}
[Fact]
public void ImplicitConversion_WhenConvertingErrorList_ShouldCreateFailedResult()
{
// Arrange
List<Error> errors = [Error.Validation("Code", "Description")];
// Act
Result<string> result = errors;
// Assert
Assert.True(result.IsFailure);
Assert.Equal(errors, result.Errors);
}
[Fact]
public void Map_WhenResultIsSuccessful_ShouldMapValue()
{
// Arrange
Result<int> result = Result<int>.Success(5);
// Act
Result<string> mappedResult = result.Map(value => value.ToString());
// Assert
Assert.True(mappedResult.IsSuccess);
Assert.Equal("5", mappedResult.Value);
}
[Fact]
public void Map_WhenResultIsFailed_ShouldPreserveErrors()
{
// Arrange
Error error = Error.Validation("Code", "Description");
Result<int> result = Result<int>.Failure(error);
// Act
Result<string> mappedResult = result.Map(value => value.ToString());
// Assert
Assert.True(mappedResult.IsFailure);
Assert.Equal(error, mappedResult.FirstError);
}
[Fact]
public void Bind_WhenResultIsSuccessful_ShouldReturnBinderResult()
{
// Arrange
Result<int> result = Result<int>.Success(5);
// Act
Result<string> boundResult = result.Bind(value => Result<string>.Success(value.ToString()));
// Assert
Assert.True(boundResult.IsSuccess);
Assert.Equal("5", boundResult.Value);
}
[Fact]
public void Bind_WhenResultIsFailed_ShouldPreserveErrors()
{
// Arrange
Error error = Error.Validation("Code", "Description");
Result<int> result = Result<int>.Failure(error);
// Act
Result<string> boundResult = result.Bind(value => Result<string>.Success(value.ToString()));
// Assert
Assert.True(boundResult.IsFailure);
Assert.Equal(error, boundResult.FirstError);
}
[Fact]
public void OnSuccess_WhenResultIsSuccessful_ShouldExecuteAction()
{
// Arrange
Result<int> result = Result<int>.Success(5);
bool actionExecuted = false;
// Act
result.OnSuccess(value => actionExecuted = true);
// Assert
Assert.True(actionExecuted);
}
[Fact]
public void OnSuccess_WhenResultIsFailed_ShouldNotExecuteAction()
{
// Arrange
Result<int> result = Result<int>.Failure(Error.Validation("Code", "Description"));
bool actionExecuted = false;
// Act
result.OnSuccess(value => actionExecuted = true);
// Assert
Assert.False(actionExecuted);
}
[Fact]
public void OnFailure_WhenResultIsFailed_ShouldExecuteAction()
{
// Arrange
Error error = Error.Validation("Code", "Description");
Result<int> result = Result<int>.Failure(error);
List<Error>? receivedErrors = null;
// Act
result.OnFailure(errors => receivedErrors = [.. errors]);
// Assert
Assert.NotNull(receivedErrors);
Assert.Single(receivedErrors);
Assert.Equal(error, receivedErrors[0]);
}
[Fact]
public void OnFailure_WhenResultIsSuccessful_ShouldNotExecuteAction()
{
// Arrange
Result<int> result = Result<int>.Success(5);
bool actionExecuted = false;
// Act
result.OnFailure(errors => actionExecuted = true);
// Assert
Assert.False(actionExecuted);
}
[Fact]
public void Match_WhenResultIsSuccessful_ShouldExecuteOnSuccessFunction()
{
// Arrange
Result<int> result = Result<int>.Success(5);
// Act
string matchedResult = result.Match(value => $"Success: {value}", errors => "Failure");
// Assert
Assert.Equal("Success: 5", matchedResult);
}
[Fact]
public void Match_WhenResultIsFailed_ShouldExecuteOnFailureFunction()
{
// Arrange
Result<int> result = Result<int>.Failure(Error.Validation("Code", "Description"));
// Act
string matchedResult = result.Match(value => "Success", errors => $"Failure: {errors.Count}");
// Assert
Assert.Equal("Failure: 1", matchedResult);
}
[Fact]
public void Value_WhenResultIsFailed_ShouldThrowInvalidOperationException()
{
// Arrange
Result<string> result = Result<string>.Failure(Error.Validation("Code", "Description"));
// Act & Assert
Assert.Throws<InvalidOperationException>(() => result.Value);
}
[Fact]
public void Errors_WhenResultIsSuccessful_ShouldThrowInvalidOperationException()
{
// Arrange
Result<string> result = Result<string>.Success("test");
// Act & Assert
Assert.Throws<InvalidOperationException>(() => result.Errors);
}
[Fact]
public void Equals_WhenBothResultsAreSuccessfulWithSameValue_ShouldReturnTrue()
{
// Act
Result<string> firstResult = Result<string>.Success("test");
Result<string> secondResult = Result<string>.Success("test");
// Assert
Assert.True(firstResult.Equals(secondResult));
}
[Fact]
public void Equals_WhenResultsHaveDifferentSuccessValues_ShouldReturnFalse()
{
// Act
Result<string> firstResult = Result<string>.Success("first");
Result<string> secondResult = Result<string>.Success("second");
// Assert
Assert.False(firstResult.Equals(secondResult));
}
[Fact]
public void Equals_WhenBothResultsAreFailedWithSameErrors_ShouldReturnTrue()
{
// Act
Result<string> firstResult = Result<string>.Failure(Error.Validation("Code", "Description"));
Result<string> secondResult = Result<string>.Failure(Error.Validation("Code", "Description"));
// Assert
Assert.True(firstResult.Equals(secondResult));
}
[Fact]
public void EqualityOperator_WhenResultsAreEqual_ShouldReturnTrue()
{
// Act
Result<int> firstResult = Result<int>.Success(5);
Result<int> secondResult = Result<int>.Success(5);
// Assert
Assert.True(firstResult == secondResult);
}
[Fact]
public void InequalityOperator_WhenResultsAreNotEqual_ShouldReturnTrue()
{
// Act
Result<int> firstResult = Result<int>.Success(5);
Result<int> secondResult = Result<int>.Success(6);
// Assert
Assert.True(firstResult != secondResult);
}
[Fact]
public void GetHashCode_WhenResultsAreEqual_ShouldReturnSameHashCode()
{
// Act
Result<int> firstResult = Result<int>.Success(5);
Result<int> secondResult = Result<int>.Success(5);
// Assert
Assert.Equal(firstResult.GetHashCode(), secondResult.GetHashCode());
}
[Fact]
public void ToString_WhenResultIsSuccessful_ShouldReturnSuccessString()
{
// Arrange
Result<string> result = Result<string>.Success("test");
// Act
string resultString = result.ToString();
// Assert
Assert.Equal("Success: test", resultString);
}
[Fact]
public void ToString_WhenResultIsFailed_ShouldReturnFailureString()
{
// Arrange
Result<string> result = Result<string>.Failure(Error.Validation("Code", "Description"));
// Act
string resultString = result.ToString();
// Assert
Assert.Equal("Failure: [ValidationError { Type = Validation, Code = Code, Description = Description }]", resultString);
}
[Fact]
public void From_WhenCalledWithValue_ShouldCreateSuccessfulResult()
{
// Act
Result<int> result = Result.From(5);
// Assert
Assert.True(result.IsSuccess);
Assert.Equal(5, result.Value);
}
[Fact]
public void Marker_WhenAccessed_ShouldExposePredefinedSuccessResults()
{
// Act & Assert
Assert.Equal(new Success(), Result.Success);
Assert.Equal(new Created(), Result.Created);
Assert.Equal(new Updated(), Result.Updated);
Assert.Equal(new Deleted(), Result.Deleted);
}
}