From 026a1e040168f7a5337cdb2c340acac158cc1330 Mon Sep 17 00:00:00 2001 From: Florent22 <42492541+Florent22@users.noreply.github.com> Date: Sat, 29 Oct 2022 13:38:18 +0200 Subject: [PATCH] Added the dictionary to the test suite --- src/Error.cs | 17 +++++++++-------- tests/ErrorTests.cs | 20 +++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/Error.cs b/src/Error.cs index 02227b9..000f0b3 100644 --- a/src/Error.cs +++ b/src/Error.cs @@ -49,7 +49,7 @@ public static Error Failure( string code = "General.Failure", string description = "A failure has occurred.", Dictionary? dictionary = null) => - new(code, description, ErrorType.Failure); + new(code, description, ErrorType.Failure, dictionary); /// /// Creates an of type from a code and description. @@ -61,7 +61,7 @@ public static Error Unexpected( string code = "General.Unexpected", string description = "An unexpected error has occurred.", Dictionary? dictionary = null) => - new(code, description, ErrorType.Unexpected); + new(code, description, ErrorType.Unexpected, dictionary); /// /// Creates an of type from a code and description. @@ -71,8 +71,9 @@ public static Error Unexpected( /// A dictionary which provides optional space for information. public static Error Validation( string code = "General.Validation", - string description = "A validation error has occurred.") => - new(code, description, ErrorType.Validation); + string description = "A validation error has occurred.", + Dictionary? dictionary = null) => + new(code, description, ErrorType.Validation, dictionary); /// /// Creates an of type from a code and description. @@ -84,7 +85,7 @@ public static Error Conflict( string code = "General.Conflict", string description = "A conflict error has occurred.", Dictionary? dictionary = null) => - new(code, description, ErrorType.Conflict); + new(code, description, ErrorType.Conflict, dictionary); /// /// Creates an of type from a code and description. @@ -96,7 +97,7 @@ public static Error NotFound( string code = "General.NotFound", string description = "A 'Not Found' error has occurred.", Dictionary? dictionary = null) => - new(code, description, ErrorType.NotFound); + new(code, description, ErrorType.NotFound, dictionary); /// /// Creates an of type from a code and description. @@ -108,7 +109,7 @@ public static Error Unauthorized( string code = "General.Unauthorized", string description = "An 'Unauthorized' error has occurred.", Dictionary? dictionary = null) => - new(code, description, ErrorType.Unauthorized); + new(code, description, ErrorType.Unauthorized, dictionary); /// /// Creates an with the given numeric , @@ -123,5 +124,5 @@ public static Error Custom( string code, string description, Dictionary? dictionary = null) => - new(code, description, (ErrorType)type); + new(code, description, (ErrorType)type, dictionary); } diff --git a/tests/ErrorTests.cs b/tests/ErrorTests.cs index 49d6766..81425c4 100644 --- a/tests/ErrorTests.cs +++ b/tests/ErrorTests.cs @@ -7,12 +7,17 @@ public class ErrorTests { private const string ErrorCode = "ErrorCode"; private const string ErrorDescription = "ErrorDescription"; + private static readonly Dictionary Dictionary = new() + { + { "key1", "value1" }, + { "key2", 21 }, + }; [Fact] public void CreateError_WhenFailureError_ShouldHaveErrorTypeFailure() { // Act - Error error = Error.Failure(ErrorCode, ErrorDescription); + Error error = Error.Failure(ErrorCode, ErrorDescription, Dictionary); // Assert ValidateError(error, expectedErrorType: ErrorType.Failure); @@ -22,7 +27,7 @@ public void CreateError_WhenFailureError_ShouldHaveErrorTypeFailure() public void CreateError_WhenUnexpectedError_ShouldHaveErrorTypeFailure() { // Act - Error error = Error.Unexpected(ErrorCode, ErrorDescription); + Error error = Error.Unexpected(ErrorCode, ErrorDescription, Dictionary); // Assert ValidateError(error, expectedErrorType: ErrorType.Unexpected); @@ -32,7 +37,7 @@ public void CreateError_WhenUnexpectedError_ShouldHaveErrorTypeFailure() public void CreateError_WhenValidationError_ShouldHaveErrorTypeValidation() { // Act - Error error = Error.Validation(ErrorCode, ErrorDescription); + Error error = Error.Validation(ErrorCode, ErrorDescription, Dictionary); // Assert ValidateError(error, expectedErrorType: ErrorType.Validation); @@ -42,7 +47,7 @@ public void CreateError_WhenValidationError_ShouldHaveErrorTypeValidation() public void CreateError_WhenConflictError_ShouldHaveErrorTypeConflict() { // Act - Error error = Error.Conflict(ErrorCode, ErrorDescription); + Error error = Error.Conflict(ErrorCode, ErrorDescription, Dictionary); // Assert ValidateError(error, expectedErrorType: ErrorType.Conflict); @@ -52,7 +57,7 @@ public void CreateError_WhenConflictError_ShouldHaveErrorTypeConflict() public void CreateError_WhenNotFoundError_ShouldHaveErrorTypeNotFound() { // Act - Error error = Error.NotFound(ErrorCode, ErrorDescription); + Error error = Error.NotFound(ErrorCode, ErrorDescription, Dictionary); // Assert ValidateError(error, expectedErrorType: ErrorType.NotFound); @@ -62,7 +67,7 @@ public void CreateError_WhenNotFoundError_ShouldHaveErrorTypeNotFound() public void CreateError_WhenNotAuthorizedError_ShouldHaveErrorTypeUnauthorized() { // Act - Error error = Error.Unauthorized(ErrorCode, ErrorDescription); + Error error = Error.Unauthorized(ErrorCode, ErrorDescription, Dictionary); // Assert ValidateError(error, expectedErrorType: ErrorType.Unauthorized); @@ -72,7 +77,7 @@ public void CreateError_WhenNotAuthorizedError_ShouldHaveErrorTypeUnauthorized() public void CreateError_WhenCustomType_ShouldHaveCustomErrorType() { // Act - Error error = Error.Custom(1232, ErrorCode, ErrorDescription); + Error error = Error.Custom(1232, ErrorCode, ErrorDescription, Dictionary); // Assert ValidateError(error, expectedErrorType: (ErrorType)1232); @@ -84,5 +89,6 @@ private static void ValidateError(Error error, ErrorType expectedErrorType) error.Description.Should().Be(ErrorDescription); error.Type.Should().Be(expectedErrorType); error.NumericType.Should().Be((int)expectedErrorType); + error.Dictionary.Should().BeEquivalentTo(Dictionary); } }