Skip to content

Commit

Permalink
Merge pull request #54 from wtjerry/feature/moveAllStateToStateContainer
Browse files Browse the repository at this point in the history
Feature/move all state to state container
  • Loading branch information
ursenzler authored Oct 3, 2019
2 parents fe8c371 + 238749f commit b347707
Show file tree
Hide file tree
Showing 96 changed files with 2,556 additions and 783 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace Appccelerate.StateMachine.Facts.AsyncMachine
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Infrastructure;
using StateMachine.AsyncMachine;
using StateMachine.Infrastructure;
using Xunit;

public class ExceptionCasesFacts
Expand All @@ -38,7 +38,7 @@ public void ThrowsExceptionIfNotInitialized_WhenFiringAnEvent()
.Build();

Func<Task> action = async () =>
await testee.Fire(Events.A, Missing.Value, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.A, Missing.Value, stateContainer, stateDefinitions)
.ConfigureAwait(false);
action.Should().Throw<InvalidOperationException>();
}
Expand Down Expand Up @@ -78,10 +78,10 @@ public async Task StaysInCurrentState_WhenAGuardThrowsAnException()

testee.TransitionExceptionThrown += (sender, eventArgs) => { };

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

stateContainer
Expand Down Expand Up @@ -114,10 +114,10 @@ public async Task DeclinesTransition_WhenAGuardThrowsAnException()
testee.TransitionDeclined += (sender, e) => transitionDeclined = true;
testee.TransitionExceptionThrown += (sender, eventArgs) => { };

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

transitionDeclined
Expand Down Expand Up @@ -155,10 +155,10 @@ public async Task CapturesException_WhenAGuardThrowsAnException()
eventArgs.Exception);
};

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

capturedException
Expand Down Expand Up @@ -189,10 +189,10 @@ public async Task ContinuesEvaluatingGuards_WhenAGuardThrowsAnException()

testee.TransitionExceptionThrown += (sender, eventArgs) => { };

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

stateContainer
Expand Down Expand Up @@ -228,10 +228,10 @@ public async Task CapturesException_WhenAnTransitionActionThrowsAnException()
eventArgs.Exception);
};

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

capturedException
Expand All @@ -258,10 +258,10 @@ public async Task StaysInCurrentState_WhenTransitionActionThrowsAnException()

testee.TransitionExceptionThrown += (sender, eventArgs) => { };

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

stateContainer
Expand Down Expand Up @@ -300,10 +300,10 @@ public async Task CapturesException_WhenEntryActionThrowsAnException()
eventArgs.Exception);
};

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

capturedException
Expand Down Expand Up @@ -333,10 +333,10 @@ public async Task EntersState_WhenEntryActionThrowsAnException()

testee.TransitionExceptionThrown += (sender, eventArgs) => { };

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

stateContainer
Expand Down Expand Up @@ -373,10 +373,10 @@ public async Task CapturesException_WhenExitActionThrowsAnException()
eventArgs.Exception);
};

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

capturedException
Expand Down Expand Up @@ -404,10 +404,10 @@ public async Task ExitsState_WhenExitActionThrowsAnException()

testee.TransitionExceptionThrown += (sender, eventArgs) => { };

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, eventArguments, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, eventArguments, stateContainer, stateDefinitions)
.ConfigureAwait(false);

stateContainer
Expand All @@ -427,7 +427,7 @@ public void ThrowsException_WhenFiringAnEventOntoAnNotInitializedStateMachine()
.Build();

Func<Task> action = async () =>
await testee.Fire(Events.B, Missing.Value, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, Missing.Value, stateContainer, stateDefinitions)
.ConfigureAwait(false);
action.Should().Throw<InvalidOperationException>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace Appccelerate.StateMachine.Facts.AsyncMachine
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Infrastructure;
using StateMachine.AsyncMachine;
using StateMachine.Infrastructure;
using Xunit;

public class GuardFacts
Expand Down Expand Up @@ -50,10 +50,10 @@ public async Task EventArgumentIsPassedToTheGuard_SyncUsage()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.A, ExpectedEventArgument, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.A, ExpectedEventArgument, stateContainer, stateDefinitions)
.ConfigureAwait(false);

actualEventArgument
Expand Down Expand Up @@ -84,10 +84,10 @@ public async Task EventArgumentIsPassedToTheGuard_AsyncUsage()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.A, ExpectedEventArgument, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.A, ExpectedEventArgument, stateContainer, stateDefinitions)
.ConfigureAwait(false);

actualEventArgument
Expand All @@ -111,10 +111,10 @@ public async Task GuardWithoutArguments()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, Missing.Value, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, Missing.Value, stateContainer, stateDefinitions)
.ConfigureAwait(false);

stateContainer
Expand All @@ -141,10 +141,10 @@ public async Task GuardWithASingleArgument()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, 3, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, 3, stateContainer, stateDefinitions)
.ConfigureAwait(false);

stateContainer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public class HierarchyBuilderFacts
private readonly HierarchyBuilder<string, int> testee;
private readonly IImplicitAddIfNotAvailableStateDefinitionDictionary<string, int> states;
private readonly StateDefinition<string, int> superState;
private readonly IDictionary<string, IStateDefinition<string, int>> initiallyLastActiveStates;
private readonly IDictionary<string, string> initiallyLastActiveStates;

public HierarchyBuilderFacts()
{
this.superState = new StateDefinition<string, int>(SuperState);
this.states = A.Fake<IImplicitAddIfNotAvailableStateDefinitionDictionary<string, int>>();
A.CallTo(() => this.states[SuperState]).Returns(this.superState);
this.initiallyLastActiveStates = A.Fake<IDictionary<string, IStateDefinition<string, int>>>();
this.initiallyLastActiveStates = A.Fake<IDictionary<string, string>>();

this.testee = new HierarchyBuilder<string, int>(SuperState, this.states, this.initiallyLastActiveStates);
}
Expand Down Expand Up @@ -84,7 +84,7 @@ public void SettingTheInitialSubStateAlsoAddsItToTheInitiallyLastActiveStates()

this.testee.WithInitialSubState(SubState);

A.CallTo(() => this.initiallyLastActiveStates.Add(SuperState, subState)).MustHaveHappenedOnceExactly();
A.CallTo(() => this.initiallyLastActiveStates.Add(SuperState, SubState)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task EntryAction()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

entered
Expand All @@ -67,7 +67,7 @@ public async Task EntryActions()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

entered1.Should().BeTrue("entry action was not executed.");
Expand All @@ -92,7 +92,7 @@ public async Task ParameterizedEntryAction()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

receivedValue
Expand All @@ -117,10 +117,10 @@ public async Task ExitAction()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, null, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, null, stateContainer, stateDefinitions)
.ConfigureAwait(false);

exit
Expand All @@ -147,10 +147,10 @@ public async Task ExitActions()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, null, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, null, stateContainer, stateDefinitions)
.ConfigureAwait(false);

exit1.Should().BeTrue("exit action was not executed.");
Expand All @@ -176,10 +176,10 @@ public async Task ParametrizedExitAction()
.WithStateContainer(stateContainer)
.Build();

await testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A)
await testee.EnterInitialState(stateContainer, stateDefinitions, States.A)
.ConfigureAwait(false);

await testee.Fire(Events.B, null, stateContainer, stateContainer, stateDefinitions)
await testee.Fire(Events.B, null, stateContainer, stateDefinitions)
.ConfigureAwait(false);

receivedValue.Should().Be(Parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Appccelerate.StateMachine.Facts.AsyncMachine
using FakeItEasy;
using FluentAssertions;
using StateMachine.AsyncMachine;
using StateMachine.AsyncMachine.States;
using StateMachine.Infrastructure;
using Xunit;

public class StateContainerFacts
Expand All @@ -33,35 +33,43 @@ public void ReturnsName()
const string Name = "container";
var stateContainer = new StateContainer<string, int>(Name);

stateContainer.Name.Should().Be(Name);
stateContainer
.Name
.Should()
.Be(Name);
}

[Fact]
public void ReturnsNullAsLastActiveStateWhenStateWasNeverSet()
public void ReturnsNothingAsLastActiveStateWhenStateWasNeverSet()
{
var stateContainer = new StateContainer<string, int>("container");
var stateContainer = new StateContainer<string, int>();

stateContainer.SetLastActiveStateFor("A", A.Fake<IStateDefinition<string, int>>());
stateContainer.SetLastActiveStateFor("A", "helloWorld");

stateContainer.GetLastActiveStateOrNullFor("B").Should().BeNull();
stateContainer
.GetLastActiveStateFor("B")
.Should()
.BeEquivalentTo(Optional<string>.Nothing());
}

[Fact]
public void ReturnsStateDefinitionXAsLastActiveStateWhenStateDefinitionXWasSetBefore()
public void ReturnsStateXAsLastActiveStateWhenXWasSetBefore()
{
var stateContainer = new StateContainer<string, int>("container");
var lastActiveState = A.Fake<IStateDefinition<string, int>>();
var stateContainer = new StateContainer<string, int>();

stateContainer.SetLastActiveStateFor("A", lastActiveState);
stateContainer.SetLastActiveStateFor("A", "Z");

stateContainer.GetLastActiveStateOrNullFor("A").Should().Be(lastActiveState);
stateContainer
.GetLastActiveStateFor("A")
.Should()
.BeEquivalentTo(Optional<string>.Just("Z"));
}

[Fact]
public async Task ExtensionsWhenExtensionsAreClearedThenNoExtensionIsRegistered()
{
var executed = false;
var extension = A.Fake<IExtension<string, int>>();
var extension = A.Fake<IExtensionInternal<string, int>>();

var testee = new StateContainer<string, int>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ namespace Appccelerate.StateMachine.Facts.AsyncMachine
using System.Collections.Generic;
using AsyncSyntax;
using StateMachine.AsyncMachine;
using StateMachine.AsyncMachine.States;

public class StateDefinitionsBuilder<TState, TEvent>
where TState : IComparable
where TEvent : IComparable
{
private readonly StandardFactory<TState, TEvent> factory = new StandardFactory<TState, TEvent>();
private readonly ImplicitAddIfNotAvailableStateDefinitionDictionary<TState, TEvent> stateDefinitionDictionary = new ImplicitAddIfNotAvailableStateDefinitionDictionary<TState, TEvent>();
private readonly Dictionary<TState, IStateDefinition<TState, TEvent>> initiallyLastActiveStates = new Dictionary<TState, IStateDefinition<TState, TEvent>>();
private readonly Dictionary<TState, TState> initiallyLastActiveStates = new Dictionary<TState, TState>();

public IEntryActionSyntax<TState, TEvent> In(TState state)
{
Expand Down
Loading

0 comments on commit b347707

Please sign in to comment.