-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
339 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
using BMTLab.OneOf.Reduced; | ||
|
||
namespace BMTLab.StateResults.Tests.Units; | ||
|
||
public class ResultTests | ||
{ | ||
[Fact] | ||
public void Result_Should_InitializeWithSuccessValue() | ||
{ | ||
//// Arrange & Act | ||
var result = new Result<string>("success"); | ||
|
||
//// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
result.IsError.Should().BeFalse(); | ||
result.Value.Should().Be("success"); | ||
} | ||
|
||
|
||
[Fact] | ||
public void Result_Should_InitializeWithErrorValue() | ||
{ | ||
//// Arrange & Act | ||
var result = new Result<string>("error", false); | ||
|
||
//// Assert | ||
result.IsSuccess.Should().BeFalse(); | ||
result.IsError.Should().BeTrue(); | ||
result.Value.Should().Be("error"); | ||
} | ||
|
||
|
||
[Fact] | ||
public void Result_ShouldBeEqual_WhenInitializedWithSameValue() | ||
{ | ||
//// Arrange | ||
var result1 = new Result<string>("testValue"); | ||
var result2 = new Result<string>("testValue"); | ||
|
||
//// Act && Assert | ||
result1.Should().BeEquivalentTo(result2); | ||
} | ||
|
||
|
||
[Fact] | ||
public void IOneOfValue_ShouldReturnCorrectValue() | ||
{ | ||
//// Arrange | ||
var testValue = new object(); | ||
var result = new Result<object>(testValue); | ||
|
||
//// Act | ||
var oneOfValue = ((IOneOf) result).Value; | ||
|
||
//// Assert | ||
oneOfValue.Should().Be(testValue); | ||
} | ||
|
||
|
||
[Fact] | ||
public void Index_ShouldAlwaysReturnZero() | ||
{ | ||
//// Arrange | ||
var result = new Result<string>("testValue"); | ||
|
||
//// Act && Assert | ||
result.Index.Should().Be(0); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ImplicitOperator_Should_SetIsSuccessToDefault() | ||
{ | ||
//// Arrange & Act | ||
Result<string> result = "some value"; | ||
|
||
//// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
result.Value.Should().Be("some value"); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ExplicitOperator_Should_ReturnValue() | ||
{ | ||
//// Arrange | ||
var result = new Result<string>("some value"); | ||
|
||
//// Act | ||
var value = (string) result; | ||
|
||
//// Assert | ||
value.Should().Be("some value"); | ||
} | ||
|
||
|
||
[Fact] | ||
public void TrueOperator_Should_ReturnTrueIfSuccess() | ||
{ | ||
//// Arrange | ||
var result = new Result<string>("some value"); | ||
|
||
if (result) | ||
// Assertion inside condition to ensure it's executed. | ||
result.IsSuccess.Should().BeTrue(); | ||
else | ||
Assert.Fail("Result should be successful"); | ||
} | ||
|
||
|
||
[Fact] | ||
public void FalseOperator_Should_ReturnTrueIfError() | ||
{ | ||
//// Arrange | ||
var result = new Result<string>("some value", false); | ||
|
||
if (result) | ||
Assert.Fail("Result should be an error"); | ||
else | ||
// Assertion inside condition to ensure it's executed. | ||
result.IsError.Should().BeTrue(); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ToString_Should_ReturnCorrectFormatForSuccess() | ||
{ | ||
//// Arrange | ||
var result = new Result<string>("success"); | ||
|
||
//// Act | ||
var toString = result.ToString(); | ||
|
||
//// Assert | ||
toString.Should() | ||
.Contain($"{nameof(Result<string>.IsSuccess)} = True").And | ||
.Contain($"{nameof(Result<string>.Value)} = success"); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ToString_Should_ReturnCorrectFormatForError() | ||
{ | ||
//// Arrange | ||
var result = new Result<string>("error", false); | ||
|
||
//// Act | ||
var toString = result.ToString(); | ||
|
||
//// Assert | ||
toString.Should() | ||
.Contain($"{nameof(Result<string>.IsSuccess)} = False").And | ||
.Contain($"{nameof(Result<string>.Value)} = error"); | ||
} | ||
|
||
|
||
[Fact] | ||
public void GetHashCode_Should_ReturnDifferentHashCodesForDifferentValues() | ||
{ | ||
//// Arrange | ||
var result1 = new Result<string>("value1"); | ||
var result2 = new Result<string>("value2"); | ||
|
||
//// Act | ||
var hashCode1 = result1.ToString(); | ||
var hashCode2 = result2.ToString(); | ||
|
||
//// Assert | ||
hashCode1.Should().NotBe(hashCode2); | ||
} | ||
|
||
|
||
#pragma warning disable CA1806 | ||
[Fact] | ||
public void Result_ShouldThrow_ArgumentNullException_WhenValueIsValue() | ||
{ | ||
//// Arrange | ||
Action act = () => new Result<string>(null!); | ||
|
||
//// Act && Assert | ||
act.Should().ThrowExactly<ArgumentNullException>(); | ||
} | ||
#pragma warning restore CA1806 | ||
} |
155 changes: 155 additions & 0 deletions
155
tests/UnitTests/StateResults.Tests.Unit/Results2Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
namespace BMTLab.StateResults.Tests.Units; | ||
|
||
public class ResultsTests | ||
{ | ||
[Fact] | ||
public void Results_InitializedWithSuccess_ShouldIndicateSuccess() | ||
{ | ||
//// Arrange & Act | ||
var result = new Results<string, Exception>("success"); | ||
|
||
//// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
result.IsError.Should().BeFalse(); | ||
result.Success.Should().Be("success"); | ||
result.Error.Should().BeNull(); | ||
result.Index.Should().Be(0); | ||
} | ||
|
||
|
||
[Fact] | ||
public void Results_InitializedWithError_ShouldIndicateError() | ||
{ | ||
//// Arrange & Act | ||
var exception = new ArgumentException("error"); | ||
var result = new Results<string, Exception>(exception); | ||
|
||
//// Assert | ||
result.IsSuccess.Should().BeFalse(); | ||
result.IsError.Should().BeTrue(); | ||
result.Error.Should().Be(exception); | ||
result.Success.Should().BeNull(); | ||
} | ||
|
||
|
||
[Fact] | ||
public void IOneOfValue_ShouldReturnCorrectValue_ForSuccess() | ||
{ | ||
//// Arrange | ||
const int successValue = 42; | ||
var result = new Results<int, string>(successValue); | ||
|
||
//// Act && Assert | ||
result.Value.Should().Be(successValue); | ||
result.Index.Should().Be(0); | ||
} | ||
|
||
|
||
[Fact] | ||
public void IOneOfValue_ShouldReturnCorrectValue_ForError() | ||
{ | ||
//// Arrange | ||
const string errorValue = "Error!"; | ||
var result = new Results<int, string>(errorValue); | ||
|
||
//// Act && Assert | ||
result.Value.Should().Be(errorValue); | ||
result.Index.Should().Be(1); | ||
} | ||
|
||
|
||
[Fact] | ||
public void Match_ShouldExecuteSuccessFunction_WhenSuccess() | ||
{ | ||
//// Arrange | ||
var result = new Results<string, object>("Success"); | ||
|
||
//// Act & Assert | ||
result.Match( | ||
success => | ||
{ | ||
success.Should().Be("Success"); | ||
return true; | ||
}, | ||
error => | ||
{ | ||
Assert.Fail("Expected success, not error."); | ||
return false; | ||
}); | ||
} | ||
|
||
|
||
[Fact] | ||
public void Match_ShouldExecuteErrorFunction_WhenError() | ||
{ | ||
//// Arrange | ||
var result = new Results<object, string>("Error"); | ||
|
||
//// Act & Assert | ||
result.Match( | ||
success => | ||
{ | ||
Assert.Fail("Expected error, not success."); | ||
return true; | ||
}, | ||
error => | ||
{ | ||
error.Should().Be("Error"); | ||
return false; | ||
}); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ImplicitConversion_FromSuccessValue_ShouldResultInSuccess() | ||
{ | ||
//// Arrange & Act | ||
Results<string, byte> result = "Success"; | ||
|
||
//// Assert | ||
result.IsSuccess.Should().BeTrue(); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ImplicitConversion_FromErrorValue_ShouldResultInError() | ||
{ | ||
//// Arrange & Act | ||
Results<byte, string> result = "Error"; | ||
|
||
//// Assert | ||
result.IsError.Should().BeTrue(); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ExplicitConversion_ToSuccessValue_WhenSuccess_ShouldReturnCorrectValue() | ||
{ | ||
//// Arrange | ||
var result = new Results<string, byte>("Success"); | ||
|
||
//// Act | ||
var success = (string) result; | ||
|
||
//// Assert | ||
success.Should().Be("Success"); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ExplicitConversion_ToErrorValue_WhenError_ShouldReturnCorrectValue() | ||
{ | ||
//// Arrange | ||
var result = new Results<byte, string>(5); | ||
|
||
//// Act | ||
var error = (byte) result; | ||
|
||
//// Assert | ||
error.Should().Be(5); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.