Skip to content

Commit

Permalink
Add tests for all similar cases of ThrowIfFail
Browse files Browse the repository at this point in the history
  • Loading branch information
ShliakhestkyiAndrii committed Sep 12, 2024
1 parent 4edeba2 commit 2d0fe2e
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 27 deletions.

This file was deleted.

12 changes: 0 additions & 12 deletions ManagedCode.Communication.Tests/Properties/launchSettings.json

This file was deleted.

32 changes: 32 additions & 0 deletions ManagedCode.Communication.Tests/Results/ResultFailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void Fail()
fail.IsSuccess.Should().BeFalse();
fail.IsFailed.Should().BeTrue();
Assert.Throws<Exception>(() => fail.ThrowIfFail());
Assert.Throws<Exception>(() => fail.ThrowIfFailWithStackPreserved());
Assert.True(fail == false);
Assert.False(fail);
}
Expand Down Expand Up @@ -74,6 +75,7 @@ public void FailThrow()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(Exception), () => ok.ThrowIfFail());
Assert.Throws(typeof(Exception), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -87,6 +89,7 @@ public void FailExceptionThrow()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(ArithmeticException), () => ok.ThrowIfFail());
Assert.Throws(typeof(ArithmeticException), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -100,6 +103,7 @@ public void FailFrom()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFail());
Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -114,6 +118,7 @@ public void FailT()
fail.IsSuccess.Should().BeFalse();
fail.IsFailed.Should().BeTrue();
Assert.Throws<Exception>(() => fail.ThrowIfFail());
Assert.Throws<Exception>(() => fail.ThrowIfFailWithStackPreserved());
Assert.True(fail == false);
Assert.False(fail);
}
Expand Down Expand Up @@ -176,6 +181,7 @@ public void FailTThrow()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(Exception), () => ok.ThrowIfFail());
Assert.Throws(typeof(Exception), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -189,6 +195,7 @@ public void FailTExceptionThrow()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(ArithmeticException), () => ok.ThrowIfFail());
Assert.Throws(typeof(ArithmeticException), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -206,6 +213,7 @@ public void SucceedTFrom()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFail());
Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -223,6 +231,7 @@ public async Task SucceedFromTask()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFail());
Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand Down Expand Up @@ -297,6 +306,7 @@ public void FailGenericThrow()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(Exception), () => ok.ThrowIfFail());
Assert.Throws(typeof(Exception), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -310,6 +320,7 @@ public void FailGenericExceptionThrow()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(ArithmeticException), () => ok.ThrowIfFail());
Assert.Throws(typeof(ArithmeticException), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -327,6 +338,7 @@ public void SucceedGenericFrom()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFail());
Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -344,6 +356,7 @@ public async Task SucceedFromGenericTask()
ok.IsFailed.Should().BeTrue();

Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFail());
Assert.Throws(typeof(ArgumentException), () => ok.ThrowIfFailWithStackPreserved());

Assert.True(ok == false);
Assert.False(ok);
Expand All @@ -368,6 +381,25 @@ public async Task SeveralErrors()
}
}

[Fact]
public async Task SeveralErrorsWithStackPreserved()
{
var result = Result.Fail();
result.AddError(Error.Create("oops1"));
result.AddError(Error.Create("oops2"));
result.AddError(Error.Create("oops3"));

try
{
result.ThrowIfFailWithStackPreserved();
}
catch (AggregateException e)
{
e.Message.Should().Be("One or more errors occurred. (oops1) (oops2) (oops3)");
e.InnerExceptions.Count.Should().Be(3);
}
}

[Fact]
public void ExceptionParse()
{
Expand Down
3 changes: 3 additions & 0 deletions ManagedCode.Communication.Tests/Results/ResultSucceedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void Succeed()
ok.IsFailed.Should().BeFalse();
ok.GetError().Should().BeNull();
ok.ThrowIfFail();
ok.ThrowIfFailWithStackPreserved();
Assert.True(ok == true);
Assert.True(ok);
ok.AsTask().Result.IsSuccess.Should().BeTrue();
Expand All @@ -31,6 +32,7 @@ public void SucceedT()
ok.IsFailed.Should().BeFalse();
ok.GetError().Should().BeNull();
ok.ThrowIfFail();
ok.ThrowIfFailWithStackPreserved();
ok.Value.Message.Should().Be("msg");

Assert.True(ok == true);
Expand Down Expand Up @@ -62,6 +64,7 @@ public void SucceedTEnum()
ok.IsFailed.Should().BeFalse();
ok.GetError().Should().BeNull();
ok.ThrowIfFail();
ok.ThrowIfFailWithStackPreserved();

Assert.True(ok == true);
Assert.True(ok);
Expand Down
14 changes: 14 additions & 0 deletions ManagedCode.Communication.Tests/Results/ResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,27 @@ public void ThrowIfFail_ShouldThrowException_WhenErrorsExist()
Assert.Throws<Exception>(() => result.ThrowIfFail());
}

[Fact]
public void ThrowIfFailWithStackPreserved_ShouldThrowException_WhenErrorsExist()
{
var result = Result<int>.Fail("TestError");
Assert.Throws<Exception>(() => result.ThrowIfFailWithStackPreserved());
}

[Fact]
public void ThrowIfFail_ShouldNotThrowException_WhenNoErrorsExist()
{
var result = Result<int>.Succeed(5);
result.Invoking(r => r.ThrowIfFail()).Should().NotThrow();
}

[Fact]
public void ThrowIfFailWithStackPreserved_ShouldNotThrowException_WhenNoErrorsExist()
{
var result = Result<int>.Succeed(5);
result.Invoking(r => r.ThrowIfFailWithStackPreserved()).Should().NotThrow();
}

[Fact]
public void IsErrorCode_ShouldReturnTrue_WhenErrorCodeMatches()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void ThrowIfFailWithStackPreserved()
return;
}

var exceptions = Errors.Select(s => s.ExceptionInfo() ?? ExceptionDispatchInfo.Capture(new Exception(string.Join(';', s.ErrorCode, s.Message))));
var exceptions = Errors.Select(s => s.ExceptionInfo() ?? ExceptionDispatchInfo.Capture(new Exception(StringExtension.JoinFilter(';', s.ErrorCode, s.Message))));

if (Errors.Length == 1)
{
Expand Down
2 changes: 1 addition & 1 deletion ManagedCode.Communication/Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void ThrowIfFailWithStackPreserved()
return;
}

var exceptions = Errors.Select(s => s.ExceptionInfo() ?? ExceptionDispatchInfo.Capture(new Exception(string.Join(';', s.ErrorCode, s.Message))));
var exceptions = Errors.Select(s => s.ExceptionInfo() ?? ExceptionDispatchInfo.Capture(new Exception(StringExtension.JoinFilter(';', s.ErrorCode, s.Message))));

if (Errors.Length == 1)
{
Expand Down
2 changes: 1 addition & 1 deletion ManagedCode.Communication/ResultT/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void ThrowIfFailWithStackPreserved()
return;
}

var exceptions = Errors.Select(s => s.ExceptionInfo() ?? ExceptionDispatchInfo.Capture(new Exception(string.Join(';', s.ErrorCode, s.Message))));
var exceptions = Errors.Select(s => s.ExceptionInfo() ?? ExceptionDispatchInfo.Capture(new Exception(StringExtension.JoinFilter(';', s.ErrorCode, s.Message))));

if (Errors.Length == 1)
{
Expand Down

0 comments on commit 2d0fe2e

Please sign in to comment.