Skip to content

Commit 7672d4a

Browse files
authored
Merge pull request #8 from NitroDevs/feat/result-assertions
Add ResultExtensions, tests, and update messaging
2 parents 1ee1e77 + 53d8c1b commit 7672d4a

File tree

5 files changed

+147
-20
lines changed

5 files changed

+147
-20
lines changed

src/CSharpFunctionalExtensions.FluentAssertions/MaybeExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ public MaybeAssertions(Maybe<T> instance) : base(instance) { }
1515

1616
protected override string Identifier => "Maybe{T}";
1717

18+
/// <summary>
19+
/// Asserts that the current <see cref="Maybe{T}"/> has a value.
20+
/// </summary>
21+
/// <param name="value"></param>
22+
/// <param name="because"></param>
23+
/// <param name="becauseArgs"></param>
24+
/// <returns></returns>
1825
public AndConstraint<MaybeAssertions<T>> HaveValue(T value, string because = "", params object[] becauseArgs)
1926
{
2027
Execute.Assertion
@@ -35,6 +42,12 @@ public AndConstraint<MaybeAssertions<T>> HaveValue(T value, string because = "",
3542
return new AndConstraint<MaybeAssertions<T>>(this);
3643
}
3744

45+
/// <summary>
46+
/// Asserts that the current <see cref="Maybe{T}"/> has no value.
47+
/// </summary>
48+
/// <param name="because"></param>
49+
/// <param name="becauseArgs"></param>
50+
/// <returns></returns>
3851
public AndConstraint<MaybeAssertions<T>> HaveNoValue(string because = "", params object[] becauseArgs)
3952
{
4053
Execute.Assertion
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using FluentAssertions;
2+
using FluentAssertions.Execution;
3+
using FluentAssertions.Primitives;
4+
5+
namespace CSharpFunctionalExtensions.FluentAssertions;
6+
7+
public static class ResultExtensions
8+
{
9+
public static ResultAssertions Should(this Result instance) => new(instance);
10+
}
11+
12+
public class ResultAssertions : ReferenceTypeAssertions<Result, ResultAssertions>
13+
{
14+
public ResultAssertions(Result instance) : base(instance) { }
15+
16+
protected override string Identifier => "Result";
17+
18+
/// <summary>
19+
/// Asserts a result is a success.
20+
/// </summary>
21+
/// <param name="because"></param>
22+
/// <param name="becauseArgs"></param>
23+
/// <returns></returns>
24+
public AndConstraint<ResultAssertions> Succeed(string because = "", params object[] becauseArgs)
25+
{
26+
Execute.Assertion
27+
.BecauseOf(because, becauseArgs)
28+
.Given(() => Subject.IsSuccess)
29+
.ForCondition(isSuccess => isSuccess)
30+
.FailWith("Expected Result to be successful but it failed");
31+
32+
return new AndConstraint<ResultAssertions>(this);
33+
}
34+
35+
/// <summary>
36+
/// Asserts a result is a failure.
37+
/// </summary>
38+
/// <param name="because"></param>
39+
/// <param name="becauseArgs"></param>
40+
/// <returns></returns>
41+
public AndConstraint<ResultAssertions> Fail(string because = "", params object[] becauseArgs)
42+
{
43+
Execute.Assertion
44+
.BecauseOf(because, becauseArgs)
45+
.Given(() => Subject.IsFailure)
46+
.ForCondition(isSuccess => isSuccess)
47+
.FailWith("Expected Result to be failure but it succeeded");
48+
49+
return new AndConstraint<ResultAssertions>(this);
50+
}
51+
}

src/CSharpFunctionalExtensions.FluentAssertions/ResultTExtensions.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,68 @@ namespace CSharpFunctionalExtensions.FluentAssertions;
66

77
public static class ResultTExtensions
88
{
9-
public static ResultAssertions<T> Should<T>(this Result<T> instance) => new(instance);
9+
public static ResultTAssertions<T> Should<T>(this Result<T> instance) => new(instance);
1010
}
1111

12-
public class ResultAssertions<T> : ReferenceTypeAssertions<Result<T>, ResultAssertions<T>>
12+
public class ResultTAssertions<T> : ReferenceTypeAssertions<Result<T>, ResultTAssertions<T>>
1313
{
14-
public ResultAssertions(Result<T> instance) : base(instance) { }
14+
public ResultTAssertions(Result<T> instance) : base(instance) { }
1515

1616
protected override string Identifier => "Result{T}";
1717

18-
public AndConstraint<ResultAssertions<T>> BeSuccessful(string because = "", params object[] becauseArgs)
18+
/// <summary>
19+
/// Asserts a result is a success.
20+
/// </summary>
21+
/// <param name="because"></param>
22+
/// <param name="becauseArgs"></param>
23+
/// <returns></returns>
24+
public AndConstraint<ResultTAssertions<T>> Succeed(string because = "", params object[] becauseArgs)
1925
{
2026
Execute.Assertion
2127
.BecauseOf(because, becauseArgs)
2228
.Given(() => Subject.IsSuccess)
2329
.ForCondition(isSuccess => isSuccess)
24-
.FailWith("Expected Result to be successful but found error");
30+
.FailWith("Expected Result to be successful but it failed");
2531

26-
return new AndConstraint<ResultAssertions<T>>(this);
32+
return new AndConstraint<ResultTAssertions<T>>(this);
2733
}
2834

29-
public AndConstraint<ResultAssertions<T>> BeSuccessfulWith(T value, string because = "", params object[] becauseArgs)
35+
/// <summary>
36+
/// Asserts a result is a failure.
37+
/// </summary>
38+
/// <param name="value"></param>
39+
/// <param name="because"></param>
40+
/// <param name="becauseArgs"></param>
41+
/// <returns></returns>
42+
public AndConstraint<ResultTAssertions<T>> SucceedWith(T value, string because = "", params object[] becauseArgs)
3043
{
3144
Execute.Assertion
3245
.BecauseOf(because, becauseArgs)
3346
.Given(() => Subject)
3447
.ForCondition(isSuccess => Subject.IsSuccess)
35-
.FailWith("Expected Result to be successful but found error")
48+
.FailWith("Expected Result to be successful but it failed")
3649
.Then
3750
.Given(s => s.Value)
3851
.ForCondition(v => v.Equals(value))
3952
.FailWith("Excepted Result value to be {0} but found {1}", value, Subject.Value);
4053

41-
return new AndConstraint<ResultAssertions<T>>(this);
54+
return new AndConstraint<ResultTAssertions<T>>(this);
4255
}
4356

44-
public AndConstraint<ResultAssertions<T>> BeFailure(string because = "", params object[] becauseArgs)
57+
/// <summary>
58+
///
59+
/// </summary>
60+
/// <param name="because"></param>
61+
/// <param name="becauseArgs"></param>
62+
/// <returns></returns>
63+
public AndConstraint<ResultTAssertions<T>> Fail(string because = "", params object[] becauseArgs)
4564
{
4665
Execute.Assertion
4766
.BecauseOf(because, becauseArgs)
4867
.Given(() => Subject.IsFailure)
4968
.ForCondition(isSuccess => isSuccess)
50-
.FailWith("Expected Result to be failure");
69+
.FailWith("Expected Result to be failure but it succeeded");
5170

52-
return new AndConstraint<ResultAssertions<T>>(this);
71+
return new AndConstraint<ResultTAssertions<T>>(this);
5372
}
5473
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Xunit;
2+
using Xunit.Sdk;
3+
using FluentAssertions;
4+
5+
namespace CSharpFunctionalExtensions.FluentAssertions.Tests;
6+
7+
public class ResultAssertionTests
8+
{
9+
[Fact]
10+
public void When_Result_Is_Expected_To_Have_Value_It_Should_Be_Successful()
11+
{
12+
var x = Result.Success();
13+
14+
x.Should().Succeed();
15+
}
16+
17+
[Fact]
18+
public void When_Result_Is_Expected_To_Have_Value_It_Should_Not_Be_Failure()
19+
{
20+
var x = Result.Success();
21+
22+
var action = () => x.Should().Fail();
23+
24+
action.Should().Throw<XunitException>().WithMessage("Expected Result to be failure but it succeeded");
25+
}
26+
27+
[Fact]
28+
public void When_Result_Is_Expected_To_Have_Error_It_Should_Not_Be_Failure()
29+
{
30+
var x = Result.Failure("error");
31+
32+
x.Should().Fail();
33+
}
34+
35+
[Fact]
36+
public void When_Result_Is_Expected_To_Have_Error_It_Should_Be_Failure()
37+
{
38+
var x = Result.Failure("error");
39+
40+
var action = () => x.Should().Succeed();
41+
42+
action.Should().Throw<XunitException>().WithMessage("Expected Result to be successful but it failed");
43+
}
44+
}

tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTAssertionsTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ public void When_Result_Is_Expected_To_Have_Value_It_Should_Be_Successful()
1111
{
1212
var x = Result.Success("test");
1313

14-
x.Should().BeSuccessful();
14+
x.Should().Succeed();
1515
}
1616

1717
[Fact]
1818
public void When_Result_Is_Expected_To_Have_Value_It_Should_Not_Be_Failure()
1919
{
2020
var x = Result.Success("test");
2121

22-
var action = () => x.Should().BeFailure();
22+
var action = () => x.Should().Fail();
2323

24-
action.Should().Throw<XunitException>().WithMessage("Expected Result to be failure");
24+
action.Should().Throw<XunitException>().WithMessage("Expected Result to be failure but it succeeded");
2525
}
2626

2727
[Fact]
@@ -30,15 +30,15 @@ public void When_Result_Is_Expected_To_Have_Value_It_Should_Be_Successful_With_V
3030
string expected = "test";
3131
var x = Result.Success(expected);
3232

33-
x.Should().BeSuccessfulWith(expected);
33+
x.Should().SucceedWith(expected);
3434
}
3535

3636
[Fact]
3737
public void When_Result_Is_Expected_To_Have_Value_It_Should_Not_Be_Successful_With_Different_Value()
3838
{
3939
var x = Result.Success("foo");
4040

41-
var action = () => x.Should().BeSuccessfulWith("bar");
41+
var action = () => x.Should().SucceedWith("bar");
4242

4343
action.Should().Throw<XunitException>().WithMessage(@"Excepted Result value to be ""bar"" but found ""foo""");
4444
}
@@ -48,16 +48,16 @@ public void When_Result_Is_Expected_To_Have_Error_It_Should_Not_Be_Failure()
4848
{
4949
var x = Result.Failure<string>("error");
5050

51-
x.Should().BeFailure();
51+
x.Should().Fail();
5252
}
5353

5454
[Fact]
5555
public void When_Result_Is_Expected_To_Have_Error_It_Should_Be_Failure()
5656
{
5757
var x = Result.Failure<string>("error");
5858

59-
var action = () => x.Should().BeSuccessful();
59+
var action = () => x.Should().Succeed();
6060

61-
action.Should().Throw<XunitException>().WithMessage("Expected Result to be successful but found error");
61+
action.Should().Throw<XunitException>().WithMessage("Expected Result to be successful but it failed");
6262
}
6363
}

0 commit comments

Comments
 (0)