Skip to content

Commit

Permalink
Add more decider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zsrdjan committed May 17, 2024
1 parent d56641a commit ae3ba18
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 37 deletions.
2 changes: 1 addition & 1 deletion test/Fraktalio.FModel.Tests/DeciderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static IEnumerable<E> GivenEvents<C, S, E>(this IDecider<C, S, E> decider
return decider.Decide(command(), currentState);
}

public static S GivenState<C, S, E>(this IDecider<C, S, E> decider, S state, Func<C> command)
public static S GivenState<C, S, E>(this IDecider<C, S, E> decider, S? state, Func<C> command)
{
var currentState = state != null ? state : decider.InitialState;
var events = decider.Decide(command(), currentState);
Expand Down
2 changes: 1 addition & 1 deletion test/Fraktalio.FModel.Tests/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static void ExpectActions<A>(this IEnumerable<A> flow, params A[] expecte
list.Should().BeEquivalentTo(expected);
}

public static void ThenEvents<E>(this IEnumerable<E> flow, IEnumerable<E> expected)
public static void ThenEvents<E>(this IEnumerable<E> flow, params E[] expected)
{
var list = flow.ToList();
list.Should().BeEquivalentTo(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,43 @@
namespace Fraktalio.FModel.Tests;

[Category("unit")]
public class DeciderTest
public class EventSourcedDeciderTest
{
private EvenNumberDecider _evenNumberDecider;
private OddNumberDecider _oddNumberDecider;

[SetUp]
public void Setup()
{
_evenNumberDecider = new EvenNumberDecider();
_oddNumberDecider = new OddNumberDecider();
}
private readonly EvenNumberDecider _evenNumberDecider = new();
private readonly OddNumberDecider _oddNumberDecider = new();

[Test]
public void GivenEmptyEvents_AddEvenNumber() =>
_evenNumberDecider
.GivenEvents([],
() => new EvenNumberCommand.AddEvenNumber(Description.Create("2"), Number.Create(2)))
.ThenEvents(new[] { new EvenNumberAdded(Description.Create("2"), Number.Create(2)) });
.ThenEvents([new EvenNumberAdded(Description.Create("2"), Number.Create(2))]);

[Test]
public void GivenPreviousState_AddEvenNumber() =>
public void GivenEvents_AddEvenNumber() =>
_evenNumberDecider
.GivenEvents(new[] { new EvenNumberAdded(Description.Create("2"), Number.Create(2)) },
() => new EvenNumberCommand.AddEvenNumber(Description.Create("4"), Number.Create(4)))
.ThenEvents(new[] { new EvenNumberAdded(Description.Create("4"), Number.Create(6)) });
.ThenEvents([new EvenNumberAdded(Description.Create("4"), Number.Create(6))]);

[Test]
public void GivenPreviousState_SubtractEvenNumber() =>
public void GivenEvents_SubtractEvenNumber() =>
_evenNumberDecider
.GivenEvents(new EvenNumberAdded[] { new(Description.Create("8"), Number.Create(8)) },
() => new EvenNumberCommand.SubtractEvenNumber(Description.Create("2"), Number.Create(2)))
.ThenEvents(new EvenNumberSubtracted[] { new(Description.Create("2"), Number.Create(6)) });
.ThenEvents([new EvenNumberSubtracted(Description.Create("2"), Number.Create(6))]);

[Test]
public void GivenPreviousState_AddOddNumber() =>
public void GivenEvents_AddOddNumber() =>
_oddNumberDecider
.GivenEvents(new OddNumberAdded[] { new(Description.Create("3"), Number.Create(3)) },
() => new OddNumberCommand.AddOddNumber(Description.Create("1"), Number.Create(1)))
.ThenEvents(new OddNumberAdded[] { new(Description.Create("1"), Number.Create(4)) });
.ThenEvents([new OddNumberAdded(Description.Create("1"), Number.Create(4))]);

[Test]
public void GivenEvents_SubtractOddNumber() =>
_oddNumberDecider
.GivenEvents(new OddNumberAdded[] { new(Description.Create("3"), Number.Create(3)) },
() => new OddNumberCommand.SubtractOddNumber(Description.Create("1"), Number.Create(1)))
.ThenEvents([new OddNumberAdded(Description.Create("1"), Number.Create(2))]);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Fraktalio.FModel.Tests.Examples.Numbers.States;
using EvenNumberCommand = Fraktalio.FModel.Tests.Examples.Numbers.NumberCommand.EvenNumberCommand;
using EvenNumberEvent = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.EvenNumberEvent;

Expand Down
20 changes: 4 additions & 16 deletions test/Fraktalio.FModel.Tests/Examples/Numbers/NumberState.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
namespace Fraktalio.FModel.Tests.Examples.Numbers.States;
namespace Fraktalio.FModel.Tests.Examples.Numbers;

public abstract class NumberState
{
public abstract Description Description { get; }
public abstract Number Value { get; }
}
public abstract record NumberState(Description Description, Number Value);

public sealed class EvenNumberState(Description description, Number value) : NumberState
{
public override Description Description { get; } = description;
public override Number Value { get; } = value;
}
public sealed record EvenNumberState(Description Description, Number Value) : NumberState(Description, Value);

public sealed class OddNumberState(Description description, Number value) : NumberState
{
public override Description Description { get; } = description;
public override Number Value { get; } = value;
}
public sealed record OddNumberState(Description Description, Number Value) : NumberState(Description, Value);
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Fraktalio.FModel.Tests.Examples.Numbers.States;
using OddNumberCommand = Fraktalio.FModel.Tests.Examples.Numbers.NumberCommand.OddNumberCommand;
using OddNumberEvent = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.OddNumberEvent;

Expand Down
49 changes: 49 additions & 0 deletions test/Fraktalio.FModel.Tests/StateStoredDeciderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Fraktalio.FModel.Tests.Examples.Numbers;
using Fraktalio.FModel.Tests.Examples.Numbers.Even;
using Fraktalio.FModel.Tests.Examples.Numbers.Odd;
using EvenNumberCommand = Fraktalio.FModel.Tests.Examples.Numbers.NumberCommand.EvenNumberCommand;
using OddNumberCommand = Fraktalio.FModel.Tests.Examples.Numbers.NumberCommand.OddNumberCommand;

namespace Fraktalio.FModel.Tests;

[Category("unit")]
public class StateStoredDeciderTest
{
private readonly EvenNumberDecider _evenNumberDecider = new();
private readonly OddNumberDecider _oddNumberDecider = new();

[Test]
public void GivenEmptyState_AddEvenNumber() =>
_evenNumberDecider
.GivenState(null,
() => new EvenNumberCommand.AddEvenNumber(Description.Create("2"), Number.Create(2)))
.ThenState(new EvenNumberState(Description.Create("Initial state + 2"), Number.Create(2)));

[Test]
public void GivenState_AddEvenNumber() =>
_evenNumberDecider
.GivenState(new EvenNumberState(Description.Create("2"), Number.Create(2)),
() => new EvenNumberCommand.AddEvenNumber(Description.Create("4"), Number.Create(4)))
.ThenState(new EvenNumberState(Description.Create("2 + 4"), Number.Create(6)));

[Test]
public void GivenState_SubtractEvenNumber() =>
_evenNumberDecider
.GivenState(new EvenNumberState(Description.Create("8"), Number.Create(8)),
() => new EvenNumberCommand.SubtractEvenNumber(Description.Create("2"), Number.Create(2)))
.ThenState(new EvenNumberState(Description.Create("8 - 2"), Number.Create(6)));

[Test]
public void GivenState_AddOddNumber() =>
_oddNumberDecider
.GivenState(new OddNumberState(Description.Create("3"), Number.Create(3)),
() => new OddNumberCommand.AddOddNumber(Description.Create("1"), Number.Create(1)))
.ThenState(new OddNumberState(Description.Create("3 + 1"), Number.Create(4)));

[Test]
public void GivenState_SubtractOddNumber() =>
_oddNumberDecider
.GivenState(new OddNumberState(Description.Create("3"), Number.Create(3)),
() => new OddNumberCommand.SubtractOddNumber(Description.Create("1"), Number.Create(1)))
.ThenState(new OddNumberState(Description.Create("3 - 1"), Number.Create(2)));
}

0 comments on commit ae3ba18

Please sign in to comment.