diff --git a/src/LightResults/Common/IActionableResult.cs b/src/LightResults/Common/IActionableResult.cs index df717bd..f4d454a 100644 --- a/src/LightResults/Common/IActionableResult.cs +++ b/src/LightResults/Common/IActionableResult.cs @@ -28,7 +28,7 @@ public interface IActionableResult : IResult /// The error message associated with the failure. /// The metadata associated with the failure. /// A new instance of representing a failed result with the specified error message and metadata. - static abstract TResult Fail(string errorMessage, IDictionary metadata); + static abstract TResult Fail(string errorMessage, IReadOnlyDictionary metadata); /// Creates a failed result with the given error. /// The error associated with the failure. @@ -39,6 +39,11 @@ public interface IActionableResult : IResult /// A collection of errors associated with the failure. /// A new instance of representing a failed result with the specified errors. static abstract TResult Fail(IEnumerable errors); + + /// Creates a failed result with the given errors. + /// A collection of errors associated with the failure. + /// A new instance of representing a failed result with the specified errors. + static abstract TResult Fail(IReadOnlyList errors); } /// Defines an actionable result. @@ -69,7 +74,7 @@ public interface IActionableResult : IResult /// The error message associated with the failure. /// The metadata associated with the failure. /// A new instance of representing a failed result with the specified error message and metadata. - static abstract TResult Fail(string errorMessage, IDictionary metadata); + static abstract TResult Fail(string errorMessage, IReadOnlyDictionary metadata); /// Creates a failed result with the given error. /// The error associated with the failure. @@ -80,5 +85,10 @@ public interface IActionableResult : IResult /// A collection of errors associated with the failure. /// A new instance of representing a failed result with the specified errors. static abstract TResult Fail(IEnumerable errors); + + /// Creates a failed result with the given errors. + /// A collection of errors associated with the failure. + /// A new instance of representing a failed result with the specified errors. + static abstract TResult Fail(IReadOnlyList errors); } #endif diff --git a/src/LightResults/Error.cs b/src/LightResults/Error.cs index a113440..a6d797b 100644 --- a/src/LightResults/Error.cs +++ b/src/LightResults/Error.cs @@ -1,31 +1,11 @@ -using System.Collections.Immutable; -using LightResults.Common; -#if NET8_0_OR_GREATER -using System.Collections.Frozen; -#endif +using LightResults.Common; namespace LightResults; /// Represents an error with a message and associated metadata. public class Error : IError { - /// Gets an empty error. - public static IError Empty { get; } = new Error(); - - internal static IReadOnlyCollection EmptyCollection { get; } = ImmutableArray.Empty; - internal static IReadOnlyCollection DefaultCollection { get; } = ImmutableArray.Create(Empty); - - /// - public string Message { get; } - - /// - public IReadOnlyDictionary Metadata => _metadata; - -#if NET8_0_OR_GREATER - private readonly FrozenDictionary _metadata; -#else - private readonly ImmutableDictionary _metadata; -#endif + private static readonly IReadOnlyDictionary EmptyMetaData = new Dictionary(); /// Initializes a new instance of the class. public Error() @@ -38,11 +18,7 @@ public Error() public Error(string message) { Message = message; -#if NET8_0_OR_GREATER - _metadata = FrozenDictionary.Empty; -#else - _metadata = ImmutableDictionary.Empty; -#endif + Metadata = EmptyMetaData; } /// Initializes a new instance of the class with the specified metadata. @@ -58,19 +34,15 @@ public Error((string Key, object Value) metadata) public Error(string message, (string Key, object Value) metadata) { Message = message; -#if NET8_0_OR_GREATER - var dictionary = new Dictionary { { metadata.Key, metadata.Value } }; - _metadata = dictionary.ToFrozenDictionary(); -#else - var builder = ImmutableDictionary.CreateBuilder(); - builder.Add(metadata.Key, metadata.Value); - _metadata = builder.ToImmutable(); -#endif + Metadata = new Dictionary(1) + { + { metadata.Key, metadata.Value }, + }; } /// Initializes a new instance of the class with the specified metadata. /// The metadata associated with the error. - public Error(IDictionary metadata) + public Error(IReadOnlyDictionary metadata) : this("", metadata) { } @@ -78,20 +50,29 @@ public Error(IDictionary metadata) /// Initializes a new instance of the class with the specified error message and metadata. /// The error message. /// The metadata associated with the error. - public Error(string message, IDictionary metadata) + public Error(string message, IReadOnlyDictionary metadata) { Message = message; -#if NET8_0_OR_GREATER - _metadata = metadata.ToFrozenDictionary(); -#else - _metadata = metadata.ToImmutableDictionary(); -#endif + Metadata = metadata; } + /// Gets an empty error. + public static IError Empty { get; } = new Error(); + + internal static IReadOnlyList EmptyErrorList { get; } = []; + internal static IReadOnlyList DefaultErrorList { get; } = [Empty]; + + /// + public string Message { get; } + + /// + public IReadOnlyDictionary Metadata { get; } + /// public override string ToString() { - var errorType = GetType().Name; + var errorType = GetType() + .Name; if (Message.Length == 0) return errorType; diff --git a/src/LightResults/IResult.cs b/src/LightResults/IResult.cs index 1ff97d2..073187f 100644 --- a/src/LightResults/IResult.cs +++ b/src/LightResults/IResult.cs @@ -7,7 +7,7 @@ public interface IResult { /// Gets a collection of errors associated with the result. /// An of representing the errors. - IReadOnlyCollection Errors { get; } + IReadOnlyList Errors { get; } /// Gets whether the result was successful or not. /// true if the result was successful; otherwise, false. diff --git a/src/LightResults/LightResults.csproj b/src/LightResults/LightResults.csproj index 2b910f3..d5606e1 100644 --- a/src/LightResults/LightResults.csproj +++ b/src/LightResults/LightResults.csproj @@ -27,7 +27,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -37,7 +37,7 @@ LightResults - 9.0.0-preview.11 + 9.0.0-preview.12 9.0.0.0 9.0.0.0 en-US @@ -120,7 +120,7 @@ Result`1.cs - + Result`1.cs diff --git a/src/LightResults/MigrationNotes.md b/src/LightResults/MigrationNotes.md index bf7f3f3..8647b61 100644 --- a/src/LightResults/MigrationNotes.md +++ b/src/LightResults/MigrationNotes.md @@ -4,3 +4,5 @@ IsFailed property => IsFailed method Error property => gone Result.Ok() and Result.Fail() methods => gone IsIActionableResult still worth it? +Changes to Errors Interface +Changes to Metadata method overloads \ No newline at end of file diff --git a/src/LightResults/Result.Equality.cs b/src/LightResults/Result.Equality.cs index a8da529..858a161 100644 --- a/src/LightResults/Result.Equality.cs +++ b/src/LightResults/Result.Equality.cs @@ -7,7 +7,7 @@ partial struct Result /// true if the specified is equal to this instance; otherwise, false. public bool Equals(Result other) { - return Nullable.Equals(_errors, other._errors); + return Equals(_errors, other._errors); } /// Determines whether the specified object is equal to this instance. @@ -22,7 +22,7 @@ public override bool Equals(object? obj) /// A 32-bit signed integer hash code. public override int GetHashCode() { - return _errors.GetHashCode(); + return _errors?.GetHashCode() ?? 0; } /// Determines whether two instances are equal. diff --git a/src/LightResults/Result.Errors.cs b/src/LightResults/Result.Errors.cs index 345f65b..5aa8afe 100644 --- a/src/LightResults/Result.Errors.cs +++ b/src/LightResults/Result.Errors.cs @@ -5,17 +5,17 @@ namespace LightResults; partial struct Result { /// - public IReadOnlyCollection Errors + public IReadOnlyList Errors { get { - if (_errors.HasValue) + if (_errors is not null) return _errors; if (_isSuccess) - return Error.EmptyCollection; + return Error.EmptyErrorList; - return Error.DefaultCollection; + return Error.DefaultErrorList; } } @@ -26,18 +26,16 @@ public bool HasError() if (_isSuccess) return false; - if (_errors.HasValue) - { + if (_errors is not null) // Do not convert to LINQ, this creates unnecessary heap allocations. // For is the most efficient way to loop. It is the fastest and does not allocate. // ReSharper disable once ForCanBeConvertedToForeach // ReSharper disable once LoopCanBeConvertedToQuery - for (var index = 0; index < _errors.Value.Length; index++) + for (var index = 0; index < _errors.Count; index++) { - if (_errors.Value[index] is TError) + if (_errors[index] is TError) return true; } - } return typeof(TError) == typeof(Error); } @@ -52,21 +50,19 @@ public bool HasError([MaybeNullWhen(false)] out TError error) return false; } - if (_errors.HasValue) - { + if (_errors is not null) // Do not convert to LINQ, this creates unnecessary heap allocations. // For is the most efficient way to loop. It is the fastest and does not allocate. // ReSharper disable once ForCanBeConvertedToForeach // ReSharper disable once LoopCanBeConvertedToQuery - for (var index = 0; index < _errors.Value.Length; index++) + for (var index = 0; index < _errors.Count; index++) { - if (_errors.Value[index] is not TError) + if (_errors[index] is not TError tError) continue; - error = (TError)_errors.Value[index]; + error = tError; return true; } - } if (typeof(TError) == typeof(Error)) { diff --git a/src/LightResults/Result.Fail.cs b/src/LightResults/Result.Fail.cs index e1cfd43..5eb1980 100644 --- a/src/LightResults/Result.Fail.cs +++ b/src/LightResults/Result.Fail.cs @@ -34,7 +34,7 @@ public static Result Fail(string errorMessage, (string Key, object Value) metada /// The metadata associated with the failure. /// The error message associated with the failure. /// A new instance of representing a failed result with the specified error message and metadata. - public static Result Fail(string errorMessage, IDictionary metadata) + public static Result Fail(string errorMessage, IReadOnlyDictionary metadata) { var error = new Error(errorMessage, metadata); return new Result(error); @@ -55,4 +55,12 @@ public static Result Fail(IEnumerable errors) { return new Result(errors); } + + /// Creates a failed result with the given errors. + /// A collection of errors associated with the failure. + /// A new instance of representing a failed result with the specified errors. + public static Result Fail(IReadOnlyList errors) + { + return new Result(errors); + } } diff --git a/src/LightResults/Result.Fail`1.cs b/src/LightResults/Result.Fail`1.cs index b5563f1..6d883ee 100644 --- a/src/LightResults/Result.Fail`1.cs +++ b/src/LightResults/Result.Fail`1.cs @@ -36,7 +36,7 @@ public static Result Fail(string errorMessage, (string Key, obje /// The metadata associated with the failure. /// The type of the value of the result. /// A new instance of representing a failed result with the specified error message and metadata. - public static Result Fail(string errorMessage, IDictionary metadata) + public static Result Fail(string errorMessage, IReadOnlyDictionary metadata) { var error = new Error(errorMessage, metadata); return new Result(error); @@ -59,4 +59,13 @@ public static Result Fail(IEnumerable errors) { return new Result(errors); } + + /// Creates a failed result with the given errors. + /// A collection of errors associated with the failure. + /// The type of the value of the result. + /// A new instance of representing a failed result with the specified errors. + public static Result Fail(IReadOnlyList errors) + { + return new Result(errors); + } } diff --git a/src/LightResults/Result.IsFailed.cs b/src/LightResults/Result.IsFailed.cs index 50ee051..39b3139 100644 --- a/src/LightResults/Result.IsFailed.cs +++ b/src/LightResults/Result.IsFailed.cs @@ -15,8 +15,8 @@ public bool IsFailed([MaybeNullWhen(false)] out IError error) { if (_isSuccess) error = default; - else if (_errors.HasValue) - error = _errors.Value[0]; + else if (_errors is not null) + error = _errors[0]; else error = Error.Empty; diff --git a/src/LightResults/Result.ToString.cs b/src/LightResults/Result.ToString.cs index 8bcf302..36304a0 100644 --- a/src/LightResults/Result.ToString.cs +++ b/src/LightResults/Result.ToString.cs @@ -10,8 +10,8 @@ public override string ToString() if (_isSuccess) return $"{nameof(Result)} {{ IsSuccess = True }}"; - if (_errors.HasValue && _errors.Value[0].Message.Length > 0) - return StringHelper.GetResultErrorString(_errors.Value[0].Message); + if (_errors is not null && _errors[0].Message.Length > 0) + return StringHelper.GetResultErrorString(_errors[0].Message); return $"{nameof(Result)} {{ IsSuccess = False }}"; } diff --git a/src/LightResults/Result.cs b/src/LightResults/Result.cs index c89b205..62a87c9 100644 --- a/src/LightResults/Result.cs +++ b/src/LightResults/Result.cs @@ -1,4 +1,3 @@ -using System.Collections.Immutable; #if NET7_0_OR_GREATER using LightResults.Common; #endif @@ -14,7 +13,7 @@ namespace LightResults; #endif { private readonly bool _isSuccess = false; - private readonly ImmutableArray? _errors; + private readonly IReadOnlyList? _errors; private Result(bool isSuccess) { @@ -23,16 +22,16 @@ private Result(bool isSuccess) private Result(IError error) { - _errors = ImmutableArray.Create(error); + _errors = [error]; } - internal Result(ImmutableArray errors) + private Result(IEnumerable errors) { - _errors = errors; + _errors = errors.ToArray(); } - private Result(IEnumerable errors) + internal Result(IReadOnlyList errors) { - _errors = errors.ToImmutableArray(); + _errors = errors; } } diff --git a/src/LightResults/Result`1.Errors.cs b/src/LightResults/Result`1.Errors.cs index e12b746..443ad34 100644 --- a/src/LightResults/Result`1.Errors.cs +++ b/src/LightResults/Result`1.Errors.cs @@ -5,17 +5,17 @@ namespace LightResults; partial struct Result { /// - public IReadOnlyCollection Errors + public IReadOnlyList Errors { get { - if (_errors.HasValue) + if (_errors is not null) return _errors; if (_isSuccess) - return Error.EmptyCollection; + return Error.EmptyErrorList; - return Error.DefaultCollection; + return Error.DefaultErrorList; } } @@ -26,14 +26,14 @@ public bool HasError() if (_isSuccess) return false; - if (_errors.HasValue) + if (_errors is not null) // Do not convert to LINQ, this creates unnecessary heap allocations. // For is the most efficient way to loop. It is the fastest and does not allocate. // ReSharper disable once ForCanBeConvertedToForeach // ReSharper disable once LoopCanBeConvertedToQuery - for (var index = 0; index < _errors.Value.Length; index++) + for (var index = 0; index < _errors.Count; index++) { - if (_errors.Value[index] is TError) + if (_errors[index] is TError) return true; } @@ -50,21 +50,19 @@ public bool HasError([MaybeNullWhen(false)] out TError error) return false; } - if (_errors.HasValue) - { + if (_errors is not null) // Do not convert to LINQ, this creates unnecessary heap allocations. // For is the most efficient way to loop. It is the fastest and does not allocate. // ReSharper disable once ForCanBeConvertedToForeach // ReSharper disable once LoopCanBeConvertedToQuery - for (var index = 0; index < _errors.Value.Length; index++) + for (var index = 0; index < _errors.Count; index++) { - if (_errors.Value[index] is not TError) + if (_errors[index] is not TError tError) continue; - error = (TError)_errors.Value[index]; + error = tError; return true; } - } if (typeof(TError) == typeof(Error)) { diff --git a/src/LightResults/Result`1.Fail.cs b/src/LightResults/Result`1.Fail.cs index 1f74d32..b559067 100644 --- a/src/LightResults/Result`1.Fail.cs +++ b/src/LightResults/Result`1.Fail.cs @@ -39,7 +39,7 @@ static Result IActionableResult>.Fail(string erro /// The error message associated with the failure. /// The metadata associated with the failure. /// A new instance of representing a failed result with the specified error message. - static Result IActionableResult>.Fail(string errorMessage, IDictionary metadata) + static Result IActionableResult>.Fail(string errorMessage, IReadOnlyDictionary metadata) { var error = new Error(errorMessage, metadata); return new Result(error); @@ -60,5 +60,13 @@ static Result IActionableResult>.Fail(IEnumerable { return new Result(errors); } + + /// Creates a failed result with the given errors. + /// A collection of errors associated with the failure. + /// A new instance of representing a failed result with the specified errors. + static Result IActionableResult>.Fail(IReadOnlyList errors) + { + return new Result(errors); + } #endif } diff --git a/src/LightResults/Result`1.IsFailed.cs b/src/LightResults/Result`1.IsFailed.cs index 593b5fb..ed262e1 100644 --- a/src/LightResults/Result`1.IsFailed.cs +++ b/src/LightResults/Result`1.IsFailed.cs @@ -15,8 +15,8 @@ public bool IsFailed([MaybeNullWhen(false)] out IError error) { if (_isSuccess) error = default; - else if (_errors.HasValue) - error = _errors.Value[0]; + else if (_errors is not null) + error = _errors[0]; else error = Error.Empty; @@ -34,8 +34,8 @@ public bool IsFailed([MaybeNullWhen(false)] out IError error, [MaybeNullWhen(tru else { value = default; - if (_errors.HasValue) - error = _errors.Value[0]; + if (_errors is not null) + error = _errors[0]; else error = Error.Empty; } diff --git a/src/LightResults/Result`1.IsSuccess.cs b/src/LightResults/Result`1.IsSuccess.cs index a109125..8839b93 100644 --- a/src/LightResults/Result`1.IsSuccess.cs +++ b/src/LightResults/Result`1.IsSuccess.cs @@ -28,8 +28,8 @@ public bool IsSuccess([MaybeNullWhen(false)] out TValue value, [MaybeNullWhen(tr else { value = default; - if (_errors.HasValue) - error = _errors.Value[0]; + if (_errors is not null) + error = _errors[0]; else error = Error.Empty; } diff --git a/src/LightResults/Result`1.AsFailed.cs b/src/LightResults/Result`1.ToFailed.cs similarity index 75% rename from src/LightResults/Result`1.AsFailed.cs rename to src/LightResults/Result`1.ToFailed.cs index 5c7b911..f9b4f3b 100644 --- a/src/LightResults/Result`1.AsFailed.cs +++ b/src/LightResults/Result`1.ToFailed.cs @@ -4,10 +4,10 @@ partial struct Result { /// Converts the current to a failed . /// A new instance of containing the same error as the , if any. - public Result AsFailed() + public Result ToFailed() { - if (_errors.HasValue) - return new Result(_errors.Value); + if (_errors is not null) + return new Result(_errors); return Result.FailedResult; } @@ -15,10 +15,10 @@ public Result AsFailed() /// Converts the current to a failed . /// A new instance of containing the same error as the , if any. /// The type of the value of the failed result. - public Result AsFailed() + public Result ToFailed() { - if (_errors.HasValue) - return new Result(_errors.Value); + if (_errors is not null) + return new Result(_errors); return Result.FailedResult; } diff --git a/src/LightResults/Result`1.ToString.cs b/src/LightResults/Result`1.ToString.cs index 9cd8ddd..ca0b436 100644 --- a/src/LightResults/Result`1.ToString.cs +++ b/src/LightResults/Result`1.ToString.cs @@ -10,8 +10,8 @@ public override string ToString() if (_isSuccess) return StringHelper.GetResultValueString(_valueOrDefault); - if (_errors.HasValue && _errors.Value[0].Message.Length > 0) - return StringHelper.GetResultErrorString(_errors.Value[0].Message); + if (_errors is not null && _errors[0].Message.Length > 0) + return StringHelper.GetResultErrorString(_errors[0].Message); return $"{nameof(Result)} {{ IsSuccess = False }}"; } diff --git a/src/LightResults/Result`1.cs b/src/LightResults/Result`1.cs index 598615d..da94c04 100644 --- a/src/LightResults/Result`1.cs +++ b/src/LightResults/Result`1.cs @@ -1,5 +1,4 @@ -using System.Collections.Immutable; -#if NET7_0_OR_GREATER +#if NET7_0_OR_GREATER using LightResults.Common; #endif @@ -16,7 +15,7 @@ namespace LightResults; #endif { private readonly bool _isSuccess = false; - private readonly ImmutableArray? _errors; + private readonly IReadOnlyList? _errors; private readonly TValue? _valueOrDefault; internal Result(TValue value) @@ -27,16 +26,16 @@ internal Result(TValue value) internal Result(IError error) { - _errors = ImmutableArray.Create(error); + _errors = [error]; } - private Result(ImmutableArray errors) + internal Result(IEnumerable errors) { - _errors = errors; + _errors = errors.ToArray(); } - internal Result(IEnumerable errors) + private Result(IReadOnlyList errors) { - _errors = errors.ToImmutableArray(); + _errors = errors; } } diff --git a/tests/LightResults.Tests/CustomErrorTests.cs b/tests/LightResults.Tests/CustomErrorTests.cs index 076fee0..1a089f1 100644 --- a/tests/LightResults.Tests/CustomErrorTests.cs +++ b/tests/LightResults.Tests/CustomErrorTests.cs @@ -12,8 +12,12 @@ public void DefaultConstructor_ShouldCreateEmptyCustomError() var error = new CustomError(); // Assert - error.Message.Should().BeEmpty(); - error.Metadata.Should().BeEmpty(); + error.Message + .Should() + .BeEmpty(); + error.Metadata + .Should() + .BeEmpty(); } [Fact] @@ -26,8 +30,12 @@ public void ConstructorWithMessage_ShouldCreateErrorWithMessage() var error = new CustomError(errorMessage); // Assert - error.Message.Should().Be(errorMessage); - error.Metadata.Should().BeEmpty(); + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .BeEmpty(); } [Fact] @@ -40,24 +48,46 @@ public void ConstructorWithMetadataTuple_ShouldCreateErrorWithSingleMetadata() var error = new CustomError(metadata); // Assert - error.Message.Should().BeEmpty(); - error.Metadata.Should().ContainSingle(); - error.Metadata.First().Key.Should().BeEquivalentTo(metadata.Key); - error.Metadata.First().Value.Should().BeEquivalentTo(metadata.Value); + error.Message + .Should() + .BeEmpty(); + error.Metadata + .Should() + .ContainSingle(); + error.Metadata + .First() + .Key + .Should() + .BeEquivalentTo(metadata.Key); + error.Metadata + .First() + .Value + .Should() + .BeEquivalentTo(metadata.Value); } [Fact] public void ConstructorWithMetadataDictionary_ShouldCreateErrorWithMultipleMetadata() { // Arrange - var metadata = new Dictionary { { "Key1", "Value1" }, { "Key2", 42 } }; + var metadata = new Dictionary + { + { "Key1", "Value1" }, + { "Key2", 42 }, + }; // Act var error = new CustomError(metadata); // Assert - error.Message.Should().BeEmpty(); - error.Metadata.Should().HaveCount(2).And.BeEquivalentTo(metadata); + error.Message + .Should() + .BeEmpty(); + error.Metadata + .Should() + .HaveCount(2) + .And + .BeEquivalentTo(metadata); } [Fact] @@ -71,10 +101,22 @@ public void ConstructorWithMessageAndMetadataTuple_ShouldCreateErrorWithMessageA var error = new CustomError(errorMessage, metadata); // Assert - error.Message.Should().Be(errorMessage); - error.Metadata.Should().ContainSingle(); - error.Metadata.First().Key.Should().BeEquivalentTo(metadata.Key); - error.Metadata.First().Value.Should().BeEquivalentTo(metadata.Value); + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .ContainSingle(); + error.Metadata + .First() + .Key + .Should() + .BeEquivalentTo(metadata.Key); + error.Metadata + .First() + .Value + .Should() + .BeEquivalentTo(metadata.Value); } [Fact] @@ -82,14 +124,24 @@ public void ConstructorWithMessageAndMetadataDictionary_ShouldCreateErrorWithMes { // Arrange const string errorMessage = "Sample error message"; - var metadata = new Dictionary { { "Key1", "Value1" }, { "Key2", 42 } }; + var metadata = new Dictionary + { + { "Key1", "Value1" }, + { "Key2", 42 }, + }; // Act var error = new CustomError(errorMessage, metadata); // Assert - error.Message.Should().Be(errorMessage); - error.Metadata.Should().HaveCount(2).And.BeEquivalentTo(metadata); + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .HaveCount(2) + .And + .BeEquivalentTo(metadata); } [Theory] @@ -101,7 +153,9 @@ public void ToString_ShouldReturnStringRepresentation(string errorMessage) var error = new CustomError(errorMessage); // Assert - error.ToString().Should().Be(errorMessage.Length > 0 ? $"CustomError {{ Message = \"{errorMessage}\" }}" : "CustomError"); + error.ToString() + .Should() + .Be(errorMessage.Length > 0 ? $"CustomError {{ Message = \"{errorMessage}\" }}" : "CustomError"); } private sealed class CustomError : Error @@ -125,12 +179,12 @@ public CustomError((string Key, object Value) metadata) { } - public CustomError(string errorMessage, IDictionary metadata) + public CustomError(string errorMessage, IReadOnlyDictionary metadata) : base(errorMessage, metadata) { } - public CustomError(IDictionary metadata) + public CustomError(IReadOnlyDictionary metadata) : base("", metadata) { } diff --git a/tests/LightResults.Tests/ResultTValueTests.cs b/tests/LightResults.Tests/ResultTValueTests.cs index ca487fb..897ecbd 100644 --- a/tests/LightResults.Tests/ResultTValueTests.cs +++ b/tests/LightResults.Tests/ResultTValueTests.cs @@ -20,19 +20,44 @@ public void DefaultStruct_ShouldBeFailedResultWithDefaultValue() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out var resultValue).Should().BeFalse(); - resultValue.Should().Be(default); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out var resultError).Should().BeTrue(); - resultError.Should().Be(Error.Empty); - result.Errors.Should().ContainSingle().Which.Should().BeOfType(); - result.HasError().Should().BeTrue(); - result.HasError(out var error).Should().BeTrue(); - error.Should().Be(Error.Empty); - result.HasError().Should().BeFalse(); - result.HasError(out var validationError).Should().BeFalse(); - validationError.Should().Be(default); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out var resultValue) + .Should() + .BeFalse(); + resultValue.Should() + .Be(default); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out var resultError) + .Should() + .BeTrue(); + resultError.Should() + .Be(Error.Empty); + result.Errors + .Should() + .ContainSingle() + .Which + .Should() + .BeOfType(); + result.HasError() + .Should() + .BeTrue(); + result.HasError(out var error) + .Should() + .BeTrue(); + error.Should() + .Be(Error.Empty); + result.HasError() + .Should() + .BeFalse(); + result.HasError(out var validationError) + .Should() + .BeFalse(); + validationError.Should() + .Be(default); } } @@ -45,19 +70,44 @@ public void DefaultStruct_ShouldBeFailedResultWithNullValue() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out var resultValue).Should().BeFalse(); - resultValue.Should().Be(null); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out var resultError).Should().BeTrue(); - resultError.Should().Be(Error.Empty); - result.Errors.Should().ContainSingle().Which.Should().BeOfType(); - result.HasError().Should().BeTrue(); - result.HasError(out var error).Should().BeTrue(); - error.Should().Be(Error.Empty); - result.HasError().Should().BeFalse(); - result.HasError(out var validationError).Should().BeFalse(); - validationError.Should().Be(default); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out var resultValue) + .Should() + .BeFalse(); + resultValue.Should() + .Be(null); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out var resultError) + .Should() + .BeTrue(); + resultError.Should() + .Be(Error.Empty); + result.Errors + .Should() + .ContainSingle() + .Which + .Should() + .BeOfType(); + result.HasError() + .Should() + .BeTrue(); + result.HasError(out var error) + .Should() + .BeTrue(); + error.Should() + .Be(Error.Empty); + result.HasError() + .Should() + .BeFalse(); + result.HasError(out var validationError) + .Should() + .BeFalse(); + validationError.Should() + .Be(default); } } @@ -68,7 +118,9 @@ public void IsSuccess_WhenResultIsSuccess() var result = Result.Ok(42); // Assert - result.IsSuccess().Should().BeTrue(); + result.IsSuccess() + .Should() + .BeTrue(); } [Fact] @@ -83,8 +135,10 @@ public void IsSuccess_WhenResultIsSuccess_ShouldReturnAssignedValue() // Assert using (new AssertionScope()) { - isSuccess.Should().BeTrue(); - resultValue.Should().Be(42); + isSuccess.Should() + .BeTrue(); + resultValue.Should() + .Be(42); } } @@ -100,9 +154,12 @@ public void IsSuccess_WhenResultIsSuccess_ShouldReturnAssignedValueAndNullError( // Assert using (new AssertionScope()) { - isSuccess.Should().BeTrue(); - resultValue.Should().Be(42); - resultError.Should().Be(null); + isSuccess.Should() + .BeTrue(); + resultValue.Should() + .Be(42); + resultError.Should() + .Be(null); } } @@ -118,8 +175,10 @@ public void IsSuccess_WhenResultIsFailed_ShouldReturnDefaultValue() // Assert using (new AssertionScope()) { - isSuccess.Should().BeFalse(); - resultValue.Should().Be(default); + isSuccess.Should() + .BeFalse(); + resultValue.Should() + .Be(default); } } @@ -135,8 +194,10 @@ public void IsSuccess_WhenResultIsFailed_ShouldReturnNullValue() // Assert using (new AssertionScope()) { - isSuccess.Should().BeFalse(); - resultValue.Should().Be(null); + isSuccess.Should() + .BeFalse(); + resultValue.Should() + .Be(null); } } @@ -145,7 +206,11 @@ public void IsSuccess_WhenResultIsFailed_ShouldReturnDefaultValueAndFirstError() { // Arrange var firstError = new Error("Error 1"); - var errors = new List { firstError, new Error("Error 2") }; + var errors = new List + { + firstError, + new Error("Error 2"), + }; var result = Result.Fail(errors); // Act @@ -154,9 +219,12 @@ public void IsSuccess_WhenResultIsFailed_ShouldReturnDefaultValueAndFirstError() // Assert using (new AssertionScope()) { - isSuccess.Should().BeFalse(); - resultValue.Should().Be(default); - resultError.Should().Be(firstError); + isSuccess.Should() + .BeFalse(); + resultValue.Should() + .Be(default); + resultError.Should() + .Be(firstError); } } @@ -165,7 +233,11 @@ public void IsSuccess_WhenResultIsFailed_ShouldReturnNullValueAndFirstError() { // Arrange var firstError = new Error("Error 1"); - var errors = new List { firstError, new Error("Error 2") }; + var errors = new List + { + firstError, + new Error("Error 2"), + }; var result = Result.Fail(errors); // Act @@ -174,9 +246,12 @@ public void IsSuccess_WhenResultIsFailed_ShouldReturnNullValueAndFirstError() // Assert using (new AssertionScope()) { - isSuccess.Should().BeFalse(); - resultValue.Should().Be(null); - resultError.Should().Be(firstError); + isSuccess.Should() + .BeFalse(); + resultValue.Should() + .Be(null); + resultError.Should() + .Be(firstError); } } @@ -192,9 +267,12 @@ public void IsSuccess_WhenResultIsFailed_ShouldReturnDefaultValueAndDefaultError // Assert using (new AssertionScope()) { - isSuccess.Should().BeFalse(); - resultValue.Should().Be(null); - resultError.Should().Be(Error.Empty); + isSuccess.Should() + .BeFalse(); + resultValue.Should() + .Be(null); + resultError.Should() + .Be(Error.Empty); } } @@ -205,7 +283,9 @@ public void IsFailed_WhenResultIsFailed() var result = Result.Fail(); // Assert - result.IsFailed().Should().BeTrue(); + result.IsFailed() + .Should() + .BeTrue(); } [Fact] @@ -213,7 +293,11 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnFirstError() { // Arrange var firstError = new Error("Error 1"); - var errors = new List { firstError, new Error("Error 2") }; + var errors = new List + { + firstError, + new Error("Error 2"), + }; var result = Result.Fail(errors); // Act @@ -222,8 +306,10 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnFirstError() // Assert using (new AssertionScope()) { - isFailed.Should().BeTrue(); - resultError.Should().Be(firstError); + isFailed.Should() + .BeTrue(); + resultError.Should() + .Be(firstError); } } @@ -239,8 +325,10 @@ public void IsFailed_WhenResultIsSuccess_ShouldReturnNullError() // Assert using (new AssertionScope()) { - isFailed.Should().BeFalse(); - resultError.Should().Be(null); + isFailed.Should() + .BeFalse(); + resultError.Should() + .Be(null); } } @@ -249,7 +337,11 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnFirstErrorAndDefaultValue() { // Arrange var firstError = new Error("Error 1"); - var errors = new List { firstError, new Error("Error 2") }; + var errors = new List + { + firstError, + new Error("Error 2"), + }; var result = Result.Fail(errors); // Act @@ -258,9 +350,12 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnFirstErrorAndDefaultValue() // Assert using (new AssertionScope()) { - isFailed.Should().BeTrue(); - resultError.Should().Be(firstError); - resultValue.Should().Be(default); + isFailed.Should() + .BeTrue(); + resultError.Should() + .Be(firstError); + resultValue.Should() + .Be(default); } } @@ -269,7 +364,11 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnFirstErrorAndNullValue() { // Arrange var firstError = new Error("Error 1"); - var errors = new List { firstError, new Error("Error 2") }; + var errors = new List + { + firstError, + new Error("Error 2"), + }; var result = Result.Fail(errors); // Act @@ -278,9 +377,12 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnFirstErrorAndNullValue() // Assert using (new AssertionScope()) { - isFailed.Should().BeTrue(); - resultError.Should().Be(firstError); - resultValue.Should().Be(null); + isFailed.Should() + .BeTrue(); + resultError.Should() + .Be(firstError); + resultValue.Should() + .Be(null); } } @@ -296,9 +398,12 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnDefaultErrorAndNullValue() // Assert using (new AssertionScope()) { - isFailed.Should().BeTrue(); - resultError.Should().Be(Error.Empty); - resultValue.Should().Be(null); + isFailed.Should() + .BeTrue(); + resultError.Should() + .Be(Error.Empty); + resultValue.Should() + .Be(null); } } @@ -314,9 +419,12 @@ public void IsFailed_WhenResultIsSuccess_ShouldReturnNullErrorAndAssignedValue() // Assert using (new AssertionScope()) { - isFailed.Should().BeFalse(); - resultError.Should().Be(null); - resultValue.Should().Be(42); + isFailed.Should() + .BeFalse(); + resultError.Should() + .Be(null); + resultValue.Should() + .Be(42); } } @@ -332,13 +440,25 @@ public void Ok_WithValue_ShouldCreateSuccessResultWithValue() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeTrue(); - result.IsSuccess(out var resultValue).Should().BeTrue(); - resultValue.Should().Be(value); - result.IsFailed().Should().BeFalse(); - result.IsFailed(out var resultError).Should().BeFalse(); - resultError.Should().Be(null); - result.Errors.Should().BeEmpty(); + result.IsSuccess() + .Should() + .BeTrue(); + result.IsSuccess(out var resultValue) + .Should() + .BeTrue(); + resultValue.Should() + .Be(value); + result.IsFailed() + .Should() + .BeFalse(); + result.IsFailed(out var resultError) + .Should() + .BeFalse(); + resultError.Should() + .Be(null); + result.Errors + .Should() + .BeEmpty(); } } @@ -351,11 +471,25 @@ public void Fail_ShouldCreateFailedResultWithSingleError() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Message.Should().Be(""); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Message + .Should() + .Be(""); } } @@ -371,11 +505,25 @@ public void Fail_WithErrorMessage_ShouldCreateFailedResultWithSingleError() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Message.Should().Be(errorMessage); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Message + .Should() + .Be(errorMessage); } } @@ -392,13 +540,31 @@ public void Fail_WithErrorMessageAndTupleMetadata_ShouldCreateFailedResultWithSi // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - var error = result.Errors.Should().ContainSingle().Which; - error.Message.Should().Be(errorMessage); - error.Metadata.Should().ContainSingle().Which.Should().BeEquivalentTo(new KeyValuePair("Key", 0)); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + var error = result.Errors + .Should() + .ContainSingle() + .Which; + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(new KeyValuePair("Key", 0)); } } @@ -407,7 +573,10 @@ public void Fail_WithErrorMessageAndDictionaryMetadata_ShouldCreateFailedResultW { // Arrange const string errorMessage = "Sample error message"; - IDictionary metadata = new Dictionary { { "Key", 0 } }; + IReadOnlyDictionary metadata = new Dictionary + { + { "Key", 0 }, + }; // Act var result = Result.Fail(errorMessage, metadata); @@ -415,13 +584,31 @@ public void Fail_WithErrorMessageAndDictionaryMetadata_ShouldCreateFailedResultW // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - var error = result.Errors.Should().ContainSingle().Which; - error.Message.Should().Be(errorMessage); - error.Metadata.Should().ContainSingle().Which.Should().BeEquivalentTo(new KeyValuePair("Key", 0)); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + var error = result.Errors + .Should() + .ContainSingle() + .Which; + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(new KeyValuePair("Key", 0)); } } @@ -437,11 +624,24 @@ public void Fail_WithErrorObject_ShouldCreateFailedResultWithSingleError() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Should().BeEquivalentTo(error); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(error); } } @@ -449,7 +649,11 @@ public void Fail_WithErrorObject_ShouldCreateFailedResultWithSingleError() public void Fail_WithErrorsEnumerable_ShouldCreateFailedResultWithMultipleErrors() { // Arrange - var errors = new List { new Error("Error 1"), new Error("Error 2") }; + var errors = new List + { + new Error("Error 1"), + new Error("Error 2"), + }; // Act var result = Result.Fail(errors); @@ -457,11 +661,23 @@ public void Fail_WithErrorsEnumerable_ShouldCreateFailedResultWithMultipleErrors // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().HaveCount(2).And.BeEquivalentTo(errors); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .HaveCount(2) + .And + .BeEquivalentTo(errors); } } @@ -472,7 +688,9 @@ public void HasError_WithMatchingErrorType_ShouldReturnTrue() var result = Result.Fail(new ValidationError("Validation error")); // Assert - result.HasError().Should().BeTrue(); + result.HasError() + .Should() + .BeTrue(); } [Fact] @@ -480,7 +698,11 @@ public void HasError_WithMatchingErrorType_ShouldOutFirstMatch() { // Arrange var firstError = new ValidationError("Validation error"); - var errors = new List { firstError, new ValidationError("Error 2") }; + var errors = new List + { + firstError, + new ValidationError("Error 2"), + }; var result = Result.Fail(errors); // Act @@ -489,8 +711,10 @@ public void HasError_WithMatchingErrorType_ShouldOutFirstMatch() // Assert using (new AssertionScope()) { - hasError.Should().BeTrue(); - error.Should().Be(firstError); + hasError.Should() + .BeTrue(); + error.Should() + .Be(firstError); } } @@ -501,7 +725,9 @@ public void HasError_WithNonMatchingErrorType_ShouldReturnFalse() var result = Result.Fail(new Error("Generic error")); // Assert - result.HasError().Should().BeFalse(); + result.HasError() + .Should() + .BeFalse(); } [Fact] @@ -516,8 +742,10 @@ public void HasError_WithNonMatchingErrorType_ShouldOutDefaultError() // Assert using (new AssertionScope()) { - hasError.Should().BeFalse(); - error.Should().Be(default); + hasError.Should() + .BeFalse(); + error.Should() + .Be(default); } } @@ -528,7 +756,9 @@ public void HasError_WhenIsSuccess_ShouldReturnFalse() var result = Result.Ok(42); // Assert - result.HasError().Should().BeFalse(); + result.HasError() + .Should() + .BeFalse(); } [Fact] @@ -543,8 +773,10 @@ public void HasError_WhenIsSuccess_ShouldOutDefaultError() // Assert using (new AssertionScope()) { - hasError.Should().BeFalse(); - error.Should().Be(default); + hasError.Should() + .BeFalse(); + error.Should() + .Be(default); } } @@ -560,12 +792,23 @@ public void ImplicitOperator_ShouldCreateSuccessResultWithValue() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeTrue(); - result.IsSuccess(out var resultValue).Should().BeTrue(); - resultValue.Should().Be(value); - result.IsFailed().Should().BeFalse(); - result.IsFailed(out _).Should().BeFalse(); - result.Errors.Should().BeEmpty(); + result.IsSuccess() + .Should() + .BeTrue(); + result.IsSuccess(out var resultValue) + .Should() + .BeTrue(); + resultValue.Should() + .Be(value); + result.IsFailed() + .Should() + .BeFalse(); + result.IsFailed(out _) + .Should() + .BeFalse(); + result.Errors + .Should() + .BeEmpty(); } } @@ -573,18 +816,30 @@ public void ImplicitOperator_ShouldCreateSuccessResultWithValue() public void AsFailed_ShouldConvertResultToNonGenericResultWithSameErrors() { // Arrange - var errors = new List { new Error("Error 1"), new Error("Error 2") }; + var errors = new List + { + new Error("Error 1"), + new Error("Error 2"), + }; var result = Result.Fail(errors); // Act - var nonGenericResult = result.AsFailed(); + var nonGenericResult = result.ToFailed(); // Assert using (new AssertionScope()) { - nonGenericResult.IsSuccess().Should().BeFalse(); - nonGenericResult.IsFailed().Should().BeTrue(); - nonGenericResult.Errors.Should().HaveCount(2).And.BeEquivalentTo(errors); + nonGenericResult.IsSuccess() + .Should() + .BeFalse(); + nonGenericResult.IsFailed() + .Should() + .BeTrue(); + nonGenericResult.Errors + .Should() + .HaveCount(2) + .And + .BeEquivalentTo(errors); } } @@ -595,14 +850,22 @@ public void AsFailed_ShouldConvertDefaultResultToNonGenericResult() Result result = default; // Act - var nonGenericResult = result.AsFailed(); + var nonGenericResult = result.ToFailed(); // Assert using (new AssertionScope()) { - nonGenericResult.IsSuccess().Should().BeFalse(); - nonGenericResult.IsFailed().Should().BeTrue(); - nonGenericResult.Errors.Should().HaveCount(1).And.HaveElementAt(0, Error.Empty); + nonGenericResult.IsSuccess() + .Should() + .BeFalse(); + nonGenericResult.IsFailed() + .Should() + .BeTrue(); + nonGenericResult.Errors + .Should() + .HaveCount(1) + .And + .HaveElementAt(0, Error.Empty); } } @@ -610,18 +873,30 @@ public void AsFailed_ShouldConvertDefaultResultToNonGenericResult() public void AsFailed_ShouldConvertResultToGenericResultWithSameErrors() { // Arrange - var errors = new List { new Error("Error 1"), new Error("Error 2") }; + var errors = new List + { + new Error("Error 1"), + new Error("Error 2"), + }; var result = Result.Fail(errors); // Act - var genericResult = result.AsFailed(); + var genericResult = result.ToFailed(); // Assert using (new AssertionScope()) { - genericResult.IsSuccess().Should().BeFalse(); - genericResult.IsFailed().Should().BeTrue(); - genericResult.Errors.Should().HaveCount(2).And.BeEquivalentTo(errors); + genericResult.IsSuccess() + .Should() + .BeFalse(); + genericResult.IsFailed() + .Should() + .BeTrue(); + genericResult.Errors + .Should() + .HaveCount(2) + .And + .BeEquivalentTo(errors); } } @@ -632,14 +907,22 @@ public void AsFailed_ShouldConvertDefaultResultToGenericResult() Result result = default; // Act - var genericResult = result.AsFailed(); + var genericResult = result.ToFailed(); // Assert using (new AssertionScope()) { - genericResult.IsSuccess().Should().BeFalse(); - genericResult.IsFailed().Should().BeTrue(); - genericResult.Errors.Should().HaveCount(1).And.HaveElementAt(0, Error.Empty); + genericResult.IsSuccess() + .Should() + .BeFalse(); + genericResult.IsFailed() + .Should() + .BeTrue(); + genericResult.Errors + .Should() + .HaveCount(1) + .And + .HaveElementAt(0, Error.Empty); } } @@ -651,7 +934,9 @@ public void Equals_ResultInt_ShouldReturnTrueForEqualResults() var result2 = Result.Ok(42); // Assert - result1.Equals(result2).Should().BeTrue(); + result1.Equals(result2) + .Should() + .BeTrue(); } [Fact] @@ -662,7 +947,9 @@ public void Equals_ResultInt_ShouldReturnFalseForDifferentResults() var result2 = Result.Ok(43); // Assert - result1.Equals(result2).Should().BeFalse(); + result1.Equals(result2) + .Should() + .BeFalse(); } [Fact] @@ -673,7 +960,9 @@ public void Equals_ResultObject_ShouldReturnTrueForEqualResults() var result2 = Result.Ok("test"); // Assert - result1.Equals(result2).Should().BeTrue(); + result1.Equals(result2) + .Should() + .BeTrue(); } [Fact] @@ -684,7 +973,9 @@ public void Equals_ResultObject_ShouldReturnFalseForDifferentResults() var result2 = Result.Ok("test2"); // Assert - result1.Equals(result2).Should().BeFalse(); + result1.Equals(result2) + .Should() + .BeFalse(); } [Fact] @@ -695,7 +986,9 @@ public void Equals_ResultIntToObject_ShouldReturnFalseForDifferentResults() var result2 = Result.Ok(42); // Assert - result1.Equals(result2).Should().BeFalse(); + result1.Equals(result2) + .Should() + .BeFalse(); } [Fact] @@ -706,7 +999,9 @@ public void GetHashCode_ResultInt_ShouldReturnSameHashCodeForEqualResults() var result2 = Result.Ok(42); // Assert - result1.GetHashCode().Should().Be(result2.GetHashCode()); + result1.GetHashCode() + .Should() + .Be(result2.GetHashCode()); } [Fact] @@ -717,7 +1012,9 @@ public void GetHashCode_ResultInt_ShouldReturnDifferentHashCodeForDifferentResul var result2 = Result.Ok(43); // Assert - result1.GetHashCode().Should().NotBe(result2.GetHashCode()); + result1.GetHashCode() + .Should() + .NotBe(result2.GetHashCode()); } [Fact] @@ -728,7 +1025,8 @@ public void op_Equality_ResultInt_ShouldReturnTrueForEqualResults() var result2 = Result.Ok(42); // Assert - (result1 == result2).Should().BeTrue(); + (result1 == result2).Should() + .BeTrue(); } [Fact] @@ -739,7 +1037,8 @@ public void op_Equality_ResultInt_ShouldReturnFalseForDifferentResults() var result2 = Result.Ok(43); // Assert - (result1 == result2).Should().BeFalse(); + (result1 == result2).Should() + .BeFalse(); } [Fact] @@ -750,7 +1049,8 @@ public void op_Inequality_ResultInt_ShouldReturnFalseForEqualResults() var result2 = Result.Ok(42); // Assert - (result1 != result2).Should().BeFalse(); + (result1 != result2).Should() + .BeFalse(); } [Fact] @@ -761,7 +1061,8 @@ public void op_Inequality_ResultInt_ShouldReturnTrueForDifferentResults() var result2 = Result.Ok(43); // Assert - (result1 != result2).Should().BeTrue(); + (result1 != result2).Should() + .BeTrue(); } [Fact] @@ -772,7 +1073,8 @@ public void op_Equality_ResultObject_ShouldReturnTrueForEqualResults() var result2 = Result.Ok("test"); // Assert - (result1 == result2).Should().BeTrue(); + (result1 == result2).Should() + .BeTrue(); } [Fact] @@ -783,7 +1085,8 @@ public void op_Equality_ResultObject_ShouldReturnFalseForDifferentResults() var result2 = Result.Ok("test2"); // Assert - (result1 == result2).Should().BeFalse(); + (result1 == result2).Should() + .BeFalse(); } [Fact] @@ -794,7 +1097,8 @@ public void op_Inequality_ResultObject_ShouldReturnFalseForEqualResults() var result2 = Result.Ok("test"); // Assert - (result1 != result2).Should().BeFalse(); + (result1 != result2).Should() + .BeFalse(); } [Fact] @@ -805,7 +1109,8 @@ public void op_Inequality_ResultObject_ShouldReturnTrueForDifferentResults() var result2 = Result.Ok("test2"); // Assert - (result1 != result2).Should().BeTrue(); + (result1 != result2).Should() + .BeTrue(); } [Fact] @@ -816,7 +1121,8 @@ public void op_Equality_ResultIntToObject_ShouldReturnFalseForDifferentResults() var result2 = Result.Ok(42); // Assert - (result1 == result2).Should().BeFalse(); + (result1 == result2).Should() + .BeFalse(); } [Fact] @@ -827,7 +1133,8 @@ public void op_Inequality_ResultIntToObject_ShouldReturnTrueForDifferentResults( var result2 = Result.Ok(42); // Assert - (result1 != result2).Should().BeTrue(); + (result1 != result2).Should() + .BeTrue(); } [Theory] @@ -840,7 +1147,9 @@ public void ToString_ShouldReturnProperRepresentationForBoolean(bool success, st var result = success ? Result.Ok(true) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -853,7 +1162,9 @@ public void ToString_ShouldReturnProperRepresentationForSByte(bool success, stri var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -866,7 +1177,9 @@ public void ToString_ShouldReturnProperRepresentationForByte(bool success, strin var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -879,7 +1192,9 @@ public void ToString_ShouldReturnProperRepresentationForInt16(bool success, stri var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -892,7 +1207,9 @@ public void ToString_ShouldReturnProperRepresentationForUInt16(bool success, str var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -905,7 +1222,9 @@ public void ToString_ShouldReturnProperRepresentationForInt32(bool success, stri var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -918,7 +1237,9 @@ public void ToString_ShouldReturnProperRepresentationForUInt32(bool success, str var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -931,7 +1252,9 @@ public void ToString_ShouldReturnProperRepresentationForInt64(bool success, stri var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -944,7 +1267,9 @@ public void ToString_ShouldReturnProperRepresentationForUInt64(bool success, str var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -957,7 +1282,9 @@ public void ToString_ShouldReturnProperRepresentationForDecimal(bool success, st var result = success ? Result.Ok(1.1m) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -970,7 +1297,9 @@ public void ToString_ShouldReturnProperRepresentationForFloat(bool success, stri var result = success ? Result.Ok(1.1f) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -983,7 +1312,9 @@ public void ToString_ShouldReturnProperRepresentationForDouble(bool success, str var result = success ? Result.Ok(1.1d) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -996,7 +1327,9 @@ public void ToString_ShouldReturnProperRepresentationForDateTime(bool success, s var result = success ? Result.Ok(new DateTime(2024, 04, 05, 12, 30, 00, DateTimeKind.Utc)) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -1009,7 +1342,9 @@ public void ToString_ShouldReturnProperRepresentationForDateTimeOffset(bool succ var result = success ? Result.Ok(new DateTimeOffset(2024, 04, 05, 12, 30, 00, TimeSpan.Zero)) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -1022,7 +1357,9 @@ public void ToString_ShouldReturnProperRepresentationForChar(bool success, strin var result = success ? Result.Ok('c') : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -1035,7 +1372,9 @@ public void ToString_ShouldReturnProperRepresentationForString(bool success, str var result = success ? Result.Ok("StringValue") : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -1048,7 +1387,9 @@ public void ToString_ShouldReturnProperRepresentationForObject(bool success, str var result = success ? Result.Ok(new object()) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } [Theory] @@ -1061,7 +1402,9 @@ public void ToString_ShouldReturnProperRepresentationForNullableValueTypes(bool var result = success ? Result.Ok(1) : Result.Fail(errorMessage); // Assert - result.ToString().Should().Be($"Result {{ {expected} }}"); + result.ToString() + .Should() + .Be($"Result {{ {expected} }}"); } private class ValidationError(string errorMessage) : Error(errorMessage); @@ -1179,7 +1522,7 @@ static Result Fail() where TResult : IActionableResult> { const string errorMessage = "Sample error message"; - IDictionary metadata = new Dictionary { { "Key", 0 } }; + IReadOnlyDictionary metadata = new Dictionary { { "Key", 0 } }; return TResult.Fail(errorMessage, metadata); } diff --git a/tests/LightResults.Tests/ResultTests.cs b/tests/LightResults.Tests/ResultTests.cs index 1f82871..699941a 100644 --- a/tests/LightResults.Tests/ResultTests.cs +++ b/tests/LightResults.Tests/ResultTests.cs @@ -15,18 +15,40 @@ public void DefaultStruct_ShouldBeFailedResult() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out var resultError).Should().BeTrue(); - resultError.Should().Be(Error.Empty); - result.Errors.Should().ContainSingle(); - result.Errors.First().Should().BeOfType(); - result.HasError().Should().BeTrue(); - result.HasError(out var error).Should().BeTrue(); - error.Should().Be(Error.Empty); - result.HasError().Should().BeFalse(); - result.HasError(out var validationError).Should().BeFalse(); - validationError.Should().Be(default); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out var resultError) + .Should() + .BeTrue(); + resultError.Should() + .Be(Error.Empty); + result.Errors + .Should() + .ContainSingle(); + result.Errors + .First() + .Should() + .BeOfType(); + result.HasError() + .Should() + .BeTrue(); + result.HasError(out var error) + .Should() + .BeTrue(); + error.Should() + .Be(Error.Empty); + result.HasError() + .Should() + .BeFalse(); + result.HasError(out var validationError) + .Should() + .BeFalse(); + validationError.Should() + .Be(default); } } @@ -37,7 +59,9 @@ public void IsSuccess_WhenResultIsSuccess() var result = Result.Ok(); // Assert - result.IsSuccess().Should().BeTrue(); + result.IsSuccess() + .Should() + .BeTrue(); } [Fact] @@ -47,7 +71,9 @@ public void IsFailed_WhenResultIsFailed() var result = Result.Fail(); // Assert - result.IsFailed().Should().BeTrue(); + result.IsFailed() + .Should() + .BeTrue(); } [Fact] @@ -55,7 +81,11 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnFirstError() { // Arrange var firstError = new Error("Error 1"); - var errors = new List { firstError, new Error("Error 2") }; + var errors = new List + { + firstError, + new Error("Error 2"), + }; var result = Result.Fail(errors); // Act @@ -64,8 +94,10 @@ public void IsFailed_WhenResultIsFailed_ShouldReturnFirstError() // Assert using (new AssertionScope()) { - isFailed.Should().BeTrue(); - resultError.Should().Be(firstError); + isFailed.Should() + .BeTrue(); + resultError.Should() + .Be(firstError); } } @@ -81,8 +113,10 @@ public void IsFailed_WhenResultIsSuccess_ShouldReturnDefaultValue() // Assert using (new AssertionScope()) { - isFailed.Should().BeFalse(); - resultError.Should().Be(null); + isFailed.Should() + .BeFalse(); + resultError.Should() + .Be(null); } } @@ -98,8 +132,10 @@ public void IsFailed_WhenResultIsSuccess_ShouldReturnNullValue() // Assert using (new AssertionScope()) { - isFailed.Should().BeFalse(); - resultError.Should().Be(null); + isFailed.Should() + .BeFalse(); + resultError.Should() + .Be(null); } } @@ -112,11 +148,20 @@ public void Ok_ShouldCreateSuccessResult() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeTrue(); - result.IsFailed().Should().BeFalse(); - result.IsFailed(out var resultError).Should().BeFalse(); - resultError.Should().Be(null); - result.Errors.Should().BeEmpty(); + result.IsSuccess() + .Should() + .BeTrue(); + result.IsFailed() + .Should() + .BeFalse(); + result.IsFailed(out var resultError) + .Should() + .BeFalse(); + resultError.Should() + .Be(null); + result.Errors + .Should() + .BeEmpty(); } } @@ -132,13 +177,25 @@ public void OkTValue_WithValue_ShouldCreateSuccessResultWithValue() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeTrue(); - result.IsSuccess(out var resultValue).Should().BeTrue(); - resultValue.Should().Be(value); - result.IsFailed().Should().BeFalse(); - result.IsFailed(out var resultError).Should().BeFalse(); - resultError.Should().Be(null); - result.Errors.Should().BeEmpty(); + result.IsSuccess() + .Should() + .BeTrue(); + result.IsSuccess(out var resultValue) + .Should() + .BeTrue(); + resultValue.Should() + .Be(value); + result.IsFailed() + .Should() + .BeFalse(); + result.IsFailed(out var resultError) + .Should() + .BeFalse(); + resultError.Should() + .Be(null); + result.Errors + .Should() + .BeEmpty(); } } @@ -151,10 +208,22 @@ public void Fail_ShouldCreateFailedResultWithSingleError() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Message.Should().Be(""); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Message + .Should() + .Be(""); } } @@ -170,10 +239,22 @@ public void Fail_WithErrorMessage_ShouldCreateFailedResultWithSingleError() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Message.Should().Be(errorMessage); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Message + .Should() + .Be(errorMessage); } } @@ -190,12 +271,28 @@ public void Fail_WithErrorMessageAndTupleMetadata_ShouldCreateFailedResultWithSi // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - var error = result.Errors.Should().ContainSingle().Which; - error.Message.Should().Be(errorMessage); - error.Metadata.Should().ContainSingle().Which.Should().BeEquivalentTo(new KeyValuePair("Key", 0)); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + var error = result.Errors + .Should() + .ContainSingle() + .Which; + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(new KeyValuePair("Key", 0)); } } @@ -204,7 +301,10 @@ public void Fail_WithErrorMessageAndDictionaryMetadata_ShouldCreateFailedResultW { // Arrange const string errorMessage = "Sample error message"; - IDictionary metadata = new Dictionary { { "Key", 0 } }; + IReadOnlyDictionary metadata = new Dictionary + { + { "Key", 0 }, + }; // Act var result = Result.Fail(errorMessage, metadata); @@ -212,12 +312,28 @@ public void Fail_WithErrorMessageAndDictionaryMetadata_ShouldCreateFailedResultW // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - var error = result.Errors.Should().ContainSingle().Which; - error.Message.Should().Be(errorMessage); - error.Metadata.Should().ContainSingle().Which.Should().BeEquivalentTo(new KeyValuePair("Key", 0)); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + var error = result.Errors + .Should() + .ContainSingle() + .Which; + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(new KeyValuePair("Key", 0)); } } @@ -233,10 +349,21 @@ public void Fail_WithErrorObject_ShouldCreateFailedResultWithSingleError() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Should().BeEquivalentTo(error); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(error); } } @@ -244,7 +371,11 @@ public void Fail_WithErrorObject_ShouldCreateFailedResultWithSingleError() public void Fail_WithErrorsEnumerable_ShouldCreateFailedResultWithMultipleErrors() { // Arrange - var errors = new List { new Error("Error 1"), new Error("Error 2") }; + var errors = new List + { + new Error("Error 1"), + new Error("Error 2"), + }; // Act var result = Result.Fail(errors); @@ -252,10 +383,20 @@ public void Fail_WithErrorsEnumerable_ShouldCreateFailedResultWithMultipleErrors // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().HaveCount(2).And.BeEquivalentTo(errors); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .HaveCount(2) + .And + .BeEquivalentTo(errors); } } @@ -268,11 +409,25 @@ public void FailTValue_ShouldCreateFailedResultWithSingleError() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Message.Should().Be(""); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Message + .Should() + .Be(""); } } @@ -288,11 +443,25 @@ public void FailTValue_WithErrorMessage_ShouldCreateFailedResultWithSingleError( // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Message.Should().Be(errorMessage); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Message + .Should() + .Be(errorMessage); } } @@ -309,13 +478,31 @@ public void FailTValue_WithErrorMessageAndTupleMetadata_ShouldCreateFailedResult // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - var error = result.Errors.Should().ContainSingle().Which; - error.Message.Should().Be(errorMessage); - error.Metadata.Should().ContainSingle().Which.Should().BeEquivalentTo(new KeyValuePair("Key", 0)); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + var error = result.Errors + .Should() + .ContainSingle() + .Which; + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(new KeyValuePair("Key", 0)); } } @@ -324,7 +511,10 @@ public void FailTValue_WithErrorMessageAndDictionaryMetadata_ShouldCreateFailedR { // Arrange const string errorMessage = "Sample error message"; - IDictionary metadata = new Dictionary { { "Key", 0 } }; + IReadOnlyDictionary metadata = new Dictionary + { + { "Key", 0 }, + }; // Act var result = Result.Fail(errorMessage, metadata); @@ -332,13 +522,31 @@ public void FailTValue_WithErrorMessageAndDictionaryMetadata_ShouldCreateFailedR // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - var error = result.Errors.Should().ContainSingle().Which; - error.Message.Should().Be(errorMessage); - error.Metadata.Should().ContainSingle().Which.Should().BeEquivalentTo(new KeyValuePair("Key", 0)); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + var error = result.Errors + .Should() + .ContainSingle() + .Which; + error.Message + .Should() + .Be(errorMessage); + error.Metadata + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(new KeyValuePair("Key", 0)); } } @@ -354,11 +562,24 @@ public void FailTValue_WithErrorObject_ShouldCreateFailedResultWithSingleError() // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().ContainSingle().Which.Should().BeEquivalentTo(error); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .ContainSingle() + .Which + .Should() + .BeEquivalentTo(error); } } @@ -366,7 +587,11 @@ public void FailTValue_WithErrorObject_ShouldCreateFailedResultWithSingleError() public void FailTValue_WithErrorsEnumerable_ShouldCreateFailedResultWithMultipleErrors() { // Arrange - var errors = new List { new Error("Error 1"), new Error("Error 2") }; + var errors = new List + { + new Error("Error 1"), + new Error("Error 2"), + }; // Act var result = Result.Fail(errors); @@ -374,11 +599,23 @@ public void FailTValue_WithErrorsEnumerable_ShouldCreateFailedResultWithMultiple // Assert using (new AssertionScope()) { - result.IsSuccess().Should().BeFalse(); - result.IsSuccess(out _).Should().BeFalse(); - result.IsFailed().Should().BeTrue(); - result.IsFailed(out _).Should().BeTrue(); - result.Errors.Should().HaveCount(2).And.BeEquivalentTo(errors); + result.IsSuccess() + .Should() + .BeFalse(); + result.IsSuccess(out _) + .Should() + .BeFalse(); + result.IsFailed() + .Should() + .BeTrue(); + result.IsFailed(out _) + .Should() + .BeTrue(); + result.Errors + .Should() + .HaveCount(2) + .And + .BeEquivalentTo(errors); } } @@ -389,7 +626,9 @@ public void HasError_WithMatchingErrorType_ShouldReturnTrue() var result = Result.Fail(new ValidationError("Validation error")); // Assert - result.HasError().Should().BeTrue(); + result.HasError() + .Should() + .BeTrue(); } [Fact] @@ -397,7 +636,11 @@ public void HasError_WithMatchingErrorType_ShouldOutFirstMatch() { // Arrange var firstError = new ValidationError("Validation error"); - var errors = new List { firstError, new ValidationError("Validation error 2") }; + var errors = new List + { + firstError, + new ValidationError("Validation error 2"), + }; var result = Result.Fail(errors); // Act @@ -406,8 +649,10 @@ public void HasError_WithMatchingErrorType_ShouldOutFirstMatch() // Assert using (new AssertionScope()) { - hasError.Should().BeTrue(); - error.Should().Be(firstError); + hasError.Should() + .BeTrue(); + error.Should() + .Be(firstError); } } @@ -418,7 +663,9 @@ public void HasError_WithNonMatchingErrorType_ShouldReturnFalse() var result = Result.Fail(new Error("Generic error")); // Assert - result.HasError().Should().BeFalse(); + result.HasError() + .Should() + .BeFalse(); } [Fact] @@ -433,8 +680,10 @@ public void HasError_WithNonMatchingErrorType_ShouldOutDefaultError() // Assert using (new AssertionScope()) { - hasError.Should().BeFalse(); - error.Should().Be(default); + hasError.Should() + .BeFalse(); + error.Should() + .Be(default); } } @@ -445,7 +694,9 @@ public void HasError_WhenIsSuccess_ShouldReturnFalse() var result = Result.Ok(); // Assert - result.HasError().Should().BeFalse(); + result.HasError() + .Should() + .BeFalse(); } [Fact] @@ -460,8 +711,10 @@ public void HasError_WhenIsSuccess_ShouldOutDefaultError() // Assert using (new AssertionScope()) { - hasError.Should().BeFalse(); - error.Should().Be(default); + hasError.Should() + .BeFalse(); + error.Should() + .Be(default); } } @@ -473,7 +726,9 @@ public void Equals_Result_ShouldReturnTrueForEqualResults() var result2 = Result.Ok(); // Assert - result1.Equals(result2).Should().BeTrue(); + result1.Equals(result2) + .Should() + .BeTrue(); } [Fact] @@ -484,7 +739,9 @@ public void Equals_Result_ShouldReturnFalseForUnequalResults() var result2 = Result.Fail("Error"); // Assert - result1.Equals(result2).Should().BeFalse(); + result1.Equals(result2) + .Should() + .BeFalse(); } [Fact] @@ -495,7 +752,9 @@ public void Equals_Object_ShouldReturnTrueForEqualResults() var result2 = Result.Ok(); // Assert - result1.Equals((object)result2).Should().BeTrue(); + result1.Equals((object)result2) + .Should() + .BeTrue(); } [Fact] @@ -506,7 +765,9 @@ public void Equals_Object_ShouldReturnFalseForUnequalResults() var result2 = Result.Fail("Error"); // Assert - result1.Equals((object)result2).Should().BeFalse(); + result1.Equals((object)result2) + .Should() + .BeFalse(); } [Fact] @@ -517,7 +778,9 @@ public void GetHashCode_ShouldReturnSameHashCodeForEqualResults() var result2 = Result.Ok(); // Assert - result1.GetHashCode().Should().Be(result2.GetHashCode()); + result1.GetHashCode() + .Should() + .Be(result2.GetHashCode()); } [Fact] @@ -528,7 +791,8 @@ public void op_Equality_Result_ShouldReturnTrueForEqualResults() var result2 = Result.Ok(); // Assert - (result1 == result2).Should().BeTrue(); + (result1 == result2).Should() + .BeTrue(); } [Fact] @@ -539,7 +803,8 @@ public void op_Equality_Result_ShouldReturnFalseForUnequalResults() var result2 = Result.Fail("Error"); // Assert - (result1 == result2).Should().BeFalse(); + (result1 == result2).Should() + .BeFalse(); } [Fact] @@ -550,7 +815,8 @@ public void op_Inequality_Result_ShouldReturnFalseForEqualResults() var result2 = Result.Ok(); // Assert - (result1 != result2).Should().BeFalse(); + (result1 != result2).Should() + .BeFalse(); } [Fact] @@ -561,7 +827,8 @@ public void op_Inequality_Result_ShouldReturnTrueForUnequalResults() var result2 = Result.Fail("Error"); // Assert - (result1 != result2).Should().BeTrue(); + (result1 != result2).Should() + .BeTrue(); } [Fact] @@ -571,7 +838,9 @@ public void ToString_WhenSuccess_ShouldReturnStringRepresentation() var result = Result.Ok(); // Assert - result.ToString().Should().Be("Result { IsSuccess = True }"); + result.ToString() + .Should() + .Be("Result { IsSuccess = True }"); } [Theory] @@ -583,7 +852,9 @@ public void ToString_WhenFailed_ShouldReturnStringRepresentation(string errorMes var result = Result.Fail(errorMessage); // Assert - result.ToString().Should().Be(errorMessage.Length > 0 ? $"Result {{ IsSuccess = False, Error = \"{errorMessage}\" }}" : "Result { IsSuccess = False }"); + result.ToString() + .Should() + .Be(errorMessage.Length > 0 ? $"Result {{ IsSuccess = False, Error = \"{errorMessage}\" }}" : "Result { IsSuccess = False }"); } private class ValidationError(string errorMessage) : Error(errorMessage); diff --git a/tools/LightResults.ComparisonBenchmarks/LightResults.ComparisonBenchmarks.csproj b/tools/LightResults.ComparisonBenchmarks/LightResults.ComparisonBenchmarks.csproj index df6a561..e2c33f7 100644 --- a/tools/LightResults.ComparisonBenchmarks/LightResults.ComparisonBenchmarks.csproj +++ b/tools/LightResults.ComparisonBenchmarks/LightResults.ComparisonBenchmarks.csproj @@ -11,9 +11,9 @@ - + - + diff --git a/tools/LightResults.CurrentBenchmarks/LightResults.CurrentBenchmarks.csproj b/tools/LightResults.CurrentBenchmarks/LightResults.CurrentBenchmarks.csproj index 828508d..03b656c 100644 --- a/tools/LightResults.CurrentBenchmarks/LightResults.CurrentBenchmarks.csproj +++ b/tools/LightResults.CurrentBenchmarks/LightResults.CurrentBenchmarks.csproj @@ -11,7 +11,7 @@ - + diff --git a/tools/LightResults.DevelopBenchmarks/Benchmarks.cs b/tools/LightResults.DevelopBenchmarks/Benchmarks.cs index 73ccf47..33b1078 100644 --- a/tools/LightResults.DevelopBenchmarks/Benchmarks.cs +++ b/tools/LightResults.DevelopBenchmarks/Benchmarks.cs @@ -11,9 +11,6 @@ namespace LightResults.DevelopBenchmarks; [HideColumns(Column.Job, Column.Iterations, Column.Error, Column.StdDev, Column.Median, Column.RatioSD, Column.Gen0, Column.Gen1, Column.Gen2)] public class Benchmarks { - [Params(10)] - public int Iterations { get; set; } - private const int ResultValue = 0; private const string ErrorMessage = "An unknown error occured."; private static readonly Error EmptyError = new(); @@ -25,6 +22,9 @@ public class Benchmarks private static readonly Result ResultTValueFail = Result.Fail(); private static readonly Result ResultTValueFailWithErrorMessage = Result.Fail(ErrorWithErrorMessage); + [Params(10)] + public int Iterations { get; set; } + [Benchmark] public void Develop_Result_Ok() {