Skip to content

Commit

Permalink
added And unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skrasekmichael committed Mar 20, 2024
1 parent 06158ac commit dd4b0d2
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace RailwayResult.FunctionalExtensions.Tests.AndTests;

public sealed class AndTests
{
[Theory]
[ClassData(typeof(TheoryData_R1_And))]
public void R1_And(Func<R1, R2> and, R1 input, R2 expectedOutput)
{
and.Invoke(input).ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_TaskOfR1_And))]
public async Task TaskOfR1_And(Func<Task<R1>, Task<R2>> and, R1 input, R2 expectedOutput)
{
var result = await and.Invoke(Task.FromResult(input));
result.ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_R2_And))]
public void R2_And(Func<R2, R3> and, R2 input, R3 expectedOutput)
{
and.Invoke(input).ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_TaskOfR2_And))]
public async Task TaskOfR2_And(Func<Task<R2>, Task<R3>> and, R2 input, R3 expectedOutput)
{
var result = await and.Invoke(Task.FromResult(input));
result.ShouldBe(expectedOutput);
}

[Theory]
[ClassData(typeof(TheoryData_R1_AndAsync))]
public async Task R1_AndAsync(Func<R1, Task<R2>> andAsync, R1 input, R2 expectedOutput)
{
var result = await andAsync.Invoke(input);
result.ShouldBe(expectedOutput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace RailwayResult.FunctionalExtensions.Tests.AndTests;

public sealed class TheoryData_R1_And : TheoryData<Func<R1, R2>, R1, R2>
{
public TheoryData_R1_And()
{
Add(
result => result.And(() => O.B),
O.A,
(O.A, O.B)
);

//and should return input failure result
Add(
result => result.And(() => O.B),
Errors.ErrorA,
Errors.ErrorA
);

//nested result should be unwrapped
Add(
result => result.And(() => R1.Success(O.B)),
O.A,
(O.A, O.B)
);

//and should return input failure result
Add(
result => result.And(() => R1.Success(O.B)),
Errors.ErrorA,
Errors.ErrorA
);

//and should return error from nested result
Add(
result => result.And<O, O>(() => Errors.ErrorB),
O.A,
Errors.ErrorB
);

//nested result should be unwrapped
Add(
result => result.And(_ => R1.Success(O.B)),
O.A,
(O.A, O.B)
);

//and should return input failure result
Add(
result => result.And(_ => R1.Success(O.B)),
Errors.ErrorA,
Errors.ErrorA
);

//and should return error from nested result
Add(
result => result.And<O, O>(_ => Errors.ErrorB),
O.A,
Errors.ErrorB
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace RailwayResult.FunctionalExtensions.Tests.AndTests;

public sealed class TheoryData_R1_AndAsync : TheoryData<Func<R1, Task<R2>>, R1, R2>
{
public TheoryData_R1_AndAsync()
{
Add(
result => result.AndAsync(_ => Task.FromResult(O.B)),
O.A,
(O.A, O.B)
);

//and should return input failure result
Add(
result => result.AndAsync(_ => Task.FromResult(O.B)),
Errors.ErrorA,
Errors.ErrorA
);

// --- TaskOfR1 ---

Add(
result => Task.FromResult(result).AndAsync(_ => Task.FromResult(O.B)),
O.A,
(O.A, O.B)
);

//and should return input failure result
Add(
result => Task.FromResult(result).AndAsync(_ => Task.FromResult(O.B)),
Errors.ErrorA,
Errors.ErrorA
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace RailwayResult.FunctionalExtensions.Tests.AndTests;

public sealed class TheoryData_R2_And : TheoryData<Func<R2, R3>, R2, R3>
{
public TheoryData_R2_And()
{
//nested result should be unwrapped
Add(
result => result.And((_, _) => R1.Success(O.C)),
(O.A, O.B),
(O.A, O.B, O.C)
);

//and should return input failure result
Add(
result => result.And((_, _) => R1.Success(O.C)),
Errors.ErrorA,
Errors.ErrorA
);

//and should return error from nested result
Add(
result => result.And<O, O, O>((_, _) => Errors.ErrorB),
(O.A, O.B),
Errors.ErrorB
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace RailwayResult.FunctionalExtensions.Tests.AndTests;

public sealed class TheoryData_TaskOfR1_And : TheoryData<Func<Task<R1>, Task<R2>>, R1, R2>
{
public TheoryData_TaskOfR1_And()
{
Add(
result => result.And(() => O.B),
O.A,
(O.A, O.B)
);

//and should return input failure result
Add(
result => result.And(() => O.B),
Errors.ErrorA,
Errors.ErrorA
);

//nested result should be unwrapped
Add(
result => result.And(() => R1.Success(O.B)),
O.A,
(O.A, O.B)
);

//and should return input failure result
Add(
result => result.And(() => R1.Success(O.B)),
Errors.ErrorA,
Errors.ErrorA
);

//and should return error from nested result
Add(
result => result.And<O, O>(() => Errors.ErrorB),
O.A,
Errors.ErrorB
);

//nested result should be unwrapped
Add(
result => result.And(_ => R1.Success(O.B)),
O.A,
(O.A, O.B)
);

//and should return input failure result
Add(
result => result.And(_ => R1.Success(O.B)),
Errors.ErrorA,
Errors.ErrorA
);

//and should return error from nested result
Add(
result => result.And<O, O>(_ => Errors.ErrorB),
O.A,
Errors.ErrorB
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace RailwayResult.FunctionalExtensions.Tests.AndTests;

public sealed class TheoryData_TaskOfR2_And : TheoryData<Func<Task<R2>, Task<R3>>, R2, R3>
{
public TheoryData_TaskOfR2_And()
{
//nested result should be unwrapped
Add(
result => result.And((_, _) => R1.Success(O.C)),
(O.A, O.B),
(O.A, O.B, O.C)
);

//and should return input failure result
Add(
result => result.And((_, _) => R1.Success(O.C)),
Errors.ErrorA,
Errors.ErrorA
);

//and should return error from nested result
Add(
result => result.And<O, O, O>((_, _) => Errors.ErrorB),
(O.A, O.B),
Errors.ErrorB
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
global using R1N = RailwayResult.Result<RailwayResult.FunctionalExtensions.Tests.Obj?>;
global using R2 = RailwayResult.Result<(RailwayResult.FunctionalExtensions.Tests.Obj, RailwayResult.FunctionalExtensions.Tests.Obj)>;
global using R2N = RailwayResult.Result<(RailwayResult.FunctionalExtensions.Tests.Obj?, RailwayResult.FunctionalExtensions.Tests.Obj?)>;
global using R3 = RailwayResult.Result<(RailwayResult.FunctionalExtensions.Tests.Obj, RailwayResult.FunctionalExtensions.Tests.Obj, RailwayResult.FunctionalExtensions.Tests.Obj)>;
3 changes: 3 additions & 0 deletions tests/RailwayResult.FunctionalExtensions.Tests/Obj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ public sealed record Obj(string? Value)

public static readonly O Null = new(null);
public static readonly O Empty = new(string.Empty);
public static readonly O A = new("A");
public static readonly O B = new("B");
public static readonly O C = new("C");
}

0 comments on commit dd4b0d2

Please sign in to comment.