Skip to content

Commit

Permalink
EnsureNotNull Tests (#9)
Browse files Browse the repository at this point in the history
* changed tested object from string to wrapped string (`Ob`j)
* added unit tests for `.EnsureNotNull` extension methods
  • Loading branch information
skrasekmichael authored Mar 19, 2024
1 parent d120611 commit 06158ac
Show file tree
Hide file tree
Showing 16 changed files with 387 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using RailwayResult.FunctionalExtensions.Tests.EnsureTests;

namespace RailwayResult.FunctionalExtensions.Tests.EnsureNotNullTests;

public sealed class EnsureNotNullTests
{
[Theory]
[ClassData(typeof(TheoryData_Obj_EnsureNotNull))]
public void Obj_EnsureNotNull(Func<O?, R1> ensureNotNull, O? input, R1 expectedOutput)
{
ensureNotNull.Invoke(input).ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_TaskOfObj_EnsureNotNull))]
public async Task TaskOfObj_EnsureNotNull(Func<Task<O?>, Task<R1>> ensureNotNull, O? input, R1 expectedOutput)
{
var result = await ensureNotNull.Invoke(Task.FromResult(input));
result.ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_R1_EnsureNotNull))]
public void R1_EnsureNotNull(Func<R1N, R1> ensureNotNull, R1N input, R1 expectedOutput)
{
ensureNotNull.Invoke(input).ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_TaskOfR1_EnsureNotNull))]
public async Task TaskOfR1_EnsureNotNull(Func<Task<R1N>, Task<R1>> ensureNotNull, R1N input, R1 expectedOutput)
{
var result = await ensureNotNull.Invoke(Task.FromResult(input));
result.ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_R2_EnsureNotNull))]
public void R2_EnsureNotNull(Func<R2N, R2> ensureNotNull, R2N input, R2 expectedOutput)
{
ensureNotNull.Invoke(input).ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_TaskOfR2_EnsureNotNull))]
public async Task TaskOfR2_EnsureNotNull(Func<Task<R2N>, Task<R2>> ensureNotNull, R2N input, R2 expectedOutput)
{
var result = await ensureNotNull.Invoke(Task.FromResult(input));
result.ShouldBe(expectedOutput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace RailwayResult.FunctionalExtensions.Tests.EnsureTests;

public sealed class TheoryData_Obj_EnsureNotNull : TheoryData<Func<O?, R1>, O?, R1>
{
public TheoryData_Obj_EnsureNotNull()
{
//ensure should pass
Add(
result => result.EnsureNotNull(Errors.ErrorA),
O.Empty,
O.Empty
);

//ensure should fail
Add(
result => result.EnsureNotNull(Errors.ErrorA),
null,
Errors.ErrorA
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace RailwayResult.FunctionalExtensions.Tests.EnsureTests;

public sealed class TheoryData_R1_EnsureNotNull : TheoryData<Func<R1N, R1>, R1N, R1>
{
public TheoryData_R1_EnsureNotNull()
{
//ensure should pass
Add(
result => result.EnsureNotNull(Errors.ErrorA),
O.Empty,
O.Empty
);

//ensure should fail
Add(
result => result.EnsureNotNull(Errors.ErrorA),
R1N.Success(null),
Errors.ErrorA
);

//ensure should return input failure result
Add(
result => result.EnsureNotNull(Errors.ErrorA),
Errors.ErrorD,
Errors.ErrorD
);

//ensure with selector should pass
Add(
result => result.EnsureNotNull(o => o!.Value, Errors.ErrorA)!,
O.Empty,
O.Empty
);

//ensure with selector should fail
Add(
result => result.EnsureNotNull(o => o!.Value, Errors.ErrorA)!,
O.Null,
Errors.ErrorA
);

//ensure with selector should return input failure result
Add(
result => result.EnsureNotNull(o => o!.Value, Errors.ErrorA)!,
Errors.ErrorD,
Errors.ErrorD
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace RailwayResult.FunctionalExtensions.Tests.EnsureTests;

public sealed class TheoryData_R2_EnsureNotNull : TheoryData<Func<R2N, R2>, R2N, R2>
{
public TheoryData_R2_EnsureNotNull()
{
//ensure should pass
Add(
result => result.EnsureNotNull((a, _) => a, Errors.ErrorA)!,
(O.Empty, null),
(O.Empty, null)!
);

//ensure should pass
Add(
result => result.EnsureNotNull((_, b) => b, Errors.ErrorA)!,
(null, O.Empty),
(null, O.Empty)!
);

//ensure should fail
Add(
result => result.EnsureNotNull((a, _) => a, Errors.ErrorA)!,
(null, O.Empty),
Errors.ErrorA
);

//ensure should fail
Add(
result => result.EnsureNotNull((_, b) => b, Errors.ErrorA)!,
(O.Empty, null),
Errors.ErrorA
);

//ensure should return input failure result
Add(
result => result.EnsureNotNull((a, _) => a, Errors.ErrorA)!,
Errors.ErrorD,
Errors.ErrorD
);

//ensure should return input failure result
Add(
result => result.EnsureNotNull((_, b) => b, Errors.ErrorA)!,
Errors.ErrorD,
Errors.ErrorD
);

//ensure should pass
Add(
result => result.EnsureSecondNotNull(Errors.ErrorA)!,
(null, O.Empty),
(null, O.Empty)!
);

//ensure should fail
Add(
result => result.EnsureSecondNotNull(Errors.ErrorA)!,
(O.Empty, null),
Errors.ErrorA
);

//ensure should return input failure result
Add(
result => result.EnsureSecondNotNull(Errors.ErrorA)!,
Errors.ErrorD,
Errors.ErrorD
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace RailwayResult.FunctionalExtensions.Tests.EnsureTests;

public sealed class TheoryData_TaskOfObj_EnsureNotNull : TheoryData<Func<Task<O?>, Task<R1>>, O?, R1>
{
public TheoryData_TaskOfObj_EnsureNotNull()
{
//ensure should pass
Add(
result => result.EnsureNotNull(Errors.ErrorA),
O.Empty,
O.Empty
);

//ensure should fail
Add(
result => result.EnsureNotNull(Errors.ErrorA),
null,
Errors.ErrorA
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace RailwayResult.FunctionalExtensions.Tests.EnsureTests;

public sealed class TheoryData_TaskOfR1_EnsureNotNull : TheoryData<Func<Task<R1N>, Task<R1>>, R1N, R1>
{
public TheoryData_TaskOfR1_EnsureNotNull()
{
//ensure should pass
Add(
result => result.EnsureNotNull(Errors.ErrorA),
O.Empty,
O.Empty
);

//ensure should fail
Add(
result => result.EnsureNotNull(Errors.ErrorA),
R1N.Success(null),
Errors.ErrorA
);

//ensure should return input failure result
Add(
result => result.EnsureNotNull(Errors.ErrorA),
Errors.ErrorD,
Errors.ErrorD
);

//ensure with selector should pass
Add(
result => result.EnsureNotNull(o => o!.Value, Errors.ErrorA)!,
O.Empty,
O.Empty
);

//ensure with selector should fail
Add(
result => result.EnsureNotNull(o => o!.Value, Errors.ErrorA)!,
O.Null,
Errors.ErrorA
);

//ensure with selector should return input failure result
Add(
result => result.EnsureNotNull(o => o!.Value, Errors.ErrorA)!,
Errors.ErrorD,
Errors.ErrorD
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace RailwayResult.FunctionalExtensions.Tests.EnsureTests;

public sealed class TheoryData_TaskOfR2_EnsureNotNull : TheoryData<Func<Task<R2N>, Task<R2>>, R2N, R2>
{
public TheoryData_TaskOfR2_EnsureNotNull()
{
//ensure should pass
Add(
result => result.EnsureNotNull((a, _) => a, Errors.ErrorA)!,
(O.Empty, null),
(O.Empty, null)!
);

//ensure should pass
Add(
result => result.EnsureNotNull((_, b) => b, Errors.ErrorA)!,
(null, O.Empty),
(null, O.Empty)!
);

//ensure should fail
Add(
result => result.EnsureNotNull((a, _) => a, Errors.ErrorA)!,
(null, O.Empty),
Errors.ErrorA
);

//ensure should fail
Add(
result => result.EnsureNotNull((_, b) => b, Errors.ErrorA)!,
(O.Empty, null),
Errors.ErrorA
);

//ensure should return input failure result
Add(
result => result.EnsureNotNull((a, _) => a, Errors.ErrorA)!,
Errors.ErrorD,
Errors.ErrorD
);

//ensure should return input failure result
Add(
result => result.EnsureNotNull((_, b) => b, Errors.ErrorA)!,
Errors.ErrorD,
Errors.ErrorD
);

//ensure should pass
Add(
result => result.EnsureSecondNotNull(Errors.ErrorA)!,
(null, O.Empty),
(null, O.Empty)!
);

//ensure should fail
Add(
result => result.EnsureSecondNotNull(Errors.ErrorA)!,
(O.Empty, null),
Errors.ErrorA
);

//ensure should return input failure result
Add(
result => result.EnsureSecondNotNull(Errors.ErrorA)!,
Errors.ErrorD,
Errors.ErrorD
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

public static class Rules
{
public static readonly Rule<string> IsNullOrWhiteSpaceRule = string.IsNullOrWhiteSpace;
public static readonly Rule<O> IsNullOrWhiteSpaceRule = o => string.IsNullOrWhiteSpace(o.Value);

public static readonly RuleWithError<string> IsNullOrWhiteSpace = new(IsNullOrWhiteSpaceRule, Errors.ErrorA);
public static readonly RuleWithError<string> IsNotNullOrWhiteSpace = new(s => !IsNullOrWhiteSpaceRule(s), Errors.ErrorA);
public static readonly RuleWithError<string> IsEmpty = new(string.IsNullOrEmpty, Errors.ErrorB);
public static readonly RuleWithError<O> IsNullOrWhiteSpace = new(IsNullOrWhiteSpaceRule, Errors.ErrorA);
public static readonly RuleWithError<O> IsNotNullOrWhiteSpace = new(s => !IsNullOrWhiteSpaceRule(s), Errors.ErrorA);
public static readonly RuleWithError<O> IsEmpty = new(o => string.IsNullOrEmpty(o.Value), Errors.ErrorB);

public static RuleWithError<string> CountIs(int len) => new(s => s.Length == len, Errors.ErrorC);
public static RuleWithError<(string A, string B)> CountIs(int l1, int l2) => new(context => context.A.Length == l1 && context.B.Length == l2, Errors.ErrorC);
public static RuleWithError<O> CountIs(int len) => new(s => s.Value?.Length == len, Errors.ErrorC);
public static RuleWithError<(O A, O B)> CountIs(int l1, int l2) => new(context => context.A.Value?.Length == l1 && context.B.Value?.Length == l2, Errors.ErrorC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ public TheoryData_Obj_Ensure()
//ensure should pass
Add(
result => result.Ensure(Rules.IsNullOrWhiteSpaceRule, Errors.ErrorA),
string.Empty,
string.Empty
O.Empty,
O.Empty
);

//ensure should fail
Add(
result => result.Ensure(s => !Rules.IsNullOrWhiteSpaceRule(s), Errors.ErrorA),
string.Empty,
O.Empty,
Errors.ErrorA
);

//all rules should pass
Add(
result => result.Ensure(Rules.IsEmpty, Rules.IsNullOrWhiteSpace, Rules.CountIs(0)),
string.Empty,
string.Empty
O.Empty,
O.Empty
);

//last rule should fail
Add(
result => result.Ensure(Rules.IsEmpty, Rules.IsNullOrWhiteSpace, Rules.CountIs(1)),
string.Empty,
O.Empty,
Rules.CountIs(1).Error
);
}
Expand Down
Loading

0 comments on commit 06158ac

Please sign in to comment.