Skip to content

Commit

Permalink
Added the dictionary to the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
florentmsl authored and amantinband committed Jan 1, 2024
1 parent 928f4df commit 026a1e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
17 changes: 9 additions & 8 deletions src/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static Error Failure(
string code = "General.Failure",
string description = "A failure has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.Failure);
new(code, description, ErrorType.Failure, dictionary);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Unexpected"/> from a code and description.
Expand All @@ -61,7 +61,7 @@ public static Error Unexpected(
string code = "General.Unexpected",
string description = "An unexpected error has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.Unexpected);
new(code, description, ErrorType.Unexpected, dictionary);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Validation"/> from a code and description.
Expand All @@ -71,8 +71,9 @@ public static Error Unexpected(
/// <param name="dictionary">A dictionary which provides optional space for information.</param>
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<string, object>? dictionary = null) =>
new(code, description, ErrorType.Validation, dictionary);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Conflict"/> from a code and description.
Expand All @@ -84,7 +85,7 @@ public static Error Conflict(
string code = "General.Conflict",
string description = "A conflict error has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.Conflict);
new(code, description, ErrorType.Conflict, dictionary);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.NotFound"/> from a code and description.
Expand All @@ -96,7 +97,7 @@ public static Error NotFound(
string code = "General.NotFound",
string description = "A 'Not Found' error has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.NotFound);
new(code, description, ErrorType.NotFound, dictionary);

/// <summary>
/// Creates an <see cref="Error"/> of type <see cref="ErrorType.Unauthorized"/> from a code and description.
Expand All @@ -108,7 +109,7 @@ public static Error Unauthorized(
string code = "General.Unauthorized",
string description = "An 'Unauthorized' error has occurred.",
Dictionary<string, object>? dictionary = null) =>
new(code, description, ErrorType.Unauthorized);
new(code, description, ErrorType.Unauthorized, dictionary);

/// <summary>
/// Creates an <see cref="Error"/> with the given numeric <paramref name="type"/>,
Expand All @@ -123,5 +124,5 @@ public static Error Custom(
string code,
string description,
Dictionary<string, object>? dictionary = null) =>
new(code, description, (ErrorType)type);
new(code, description, (ErrorType)type, dictionary);
}
20 changes: 13 additions & 7 deletions tests/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ public class ErrorTests
{
private const string ErrorCode = "ErrorCode";
private const string ErrorDescription = "ErrorDescription";
private static readonly Dictionary<string, object> 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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}

0 comments on commit 026a1e0

Please sign in to comment.