Skip to content

Commit

Permalink
change ILastActiveStateModifier to not use IStateDefinitions but TState
Browse files Browse the repository at this point in the history
  • Loading branch information
wtjerry committed Oct 3, 2019
1 parent 28a5e76 commit cef9aa0
Show file tree
Hide file tree
Showing 32 changed files with 153 additions and 114 deletions.
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 @@ -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,28 +33,36 @@ 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]
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task CallsExtensionToHandleException()

this.ExtensionHost.Extension = extension;

await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => extension.HandlingTransitionException(this.TransitionDefinition, this.TransitionContext, ref this.exception)).MustHaveHappened();
A.CallTo(() => extension.HandledTransitionException(this.TransitionDefinition, this.TransitionContext, this.exception)).MustHaveHappened();
Expand All @@ -61,15 +61,15 @@ public async Task CallsExtensionToHandleException()
[Fact]
public async Task ReturnsFiredTransitionResult()
{
var result = await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
var result = await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

result.Fired.Should().BeTrue();
}

[Fact]
public async Task NotifiesExceptionOnTransitionContext()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.TransitionContext.OnExceptionThrown(this.exception)).MustHaveHappened();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task CallsExtensionToHandleException()

this.ExtensionHost.Extension = extension;

await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => extension.HandlingGuardException(this.TransitionDefinition, this.TransitionContext, ref this.exception)).MustHaveHappened();
A.CallTo(() => extension.HandledGuardException(this.TransitionDefinition, this.TransitionContext, this.exception)).MustHaveHappened();
Expand All @@ -61,15 +61,15 @@ public async Task CallsExtensionToHandleException()
[Fact]
public async Task ReturnsNotFiredTransitionResult()
{
var result = await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
var result = await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

result.Fired.Should().BeFalse();
}

[Fact]
public async Task NotifiesExceptionOnTransitionContext()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.TransitionContext.OnExceptionThrown(this.exception)).MustHaveHappened();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task Executes_WhenGuardIsMet()
var guard = Builder<States, Events>.CreateGuardHolder().ReturningTrue().Build();
this.TransitionDefinition.Guard = guard;

await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Entry(this.Target, this.TransitionContext)).MustHaveHappenedOnceExactly();
}
Expand All @@ -55,7 +55,7 @@ public async Task DoesNotExecute_WhenGuardIsNotMet()
var guard = Builder<States, Events>.CreateGuardHolder().ReturningFalse().Build();
this.TransitionDefinition.Guard = guard;

await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Entry(this.Target, this.TransitionContext)).MustNotHaveHappened();
}
Expand All @@ -66,7 +66,7 @@ public async Task ReturnsNotFiredTransitionResult_WhenGuardIsNotMet()
var guard = Builder<States, Events>.CreateGuardHolder().ReturningFalse().Build();
this.TransitionDefinition.Guard = guard;

var result = await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
var result = await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

result.Should().BeNotFiredTransitionResult<States>();
}
Expand All @@ -80,7 +80,7 @@ public async Task NotifiesExtensions_WhenGuardIsNotMet()
var guard = Builder<States, Events>.CreateGuardHolder().ReturningFalse().Build();
this.TransitionDefinition.Guard = guard;

await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => extension.SkippedTransition(
A<ITransitionDefinition<States, Events>>.That.Matches(t => t.Source == this.Source && t.Target == this.Target),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public HierarchicalTransitionFacts()
[Fact]
public async Task ExitsStatesUpToBelowCommonSuperState()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Exit(this.Source, this.TransitionContext, this.LastActiveStateModifier)).MustHaveHappened()
.Then(A.CallTo(() => this.StateLogic.Exit(this.superStateOfSource, this.TransitionContext, this.LastActiveStateModifier)).MustHaveHappened());
Expand All @@ -56,7 +56,7 @@ public async Task ExitsStatesUpToBelowCommonSuperState()
[Fact]
public async Task EntersStatesBelowCommonSuperStateToTarget()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Entry(this.superStateOfTarget, this.TransitionContext)).MustHaveHappened()
.Then(A.CallTo(() => this.StateLogic.Entry(this.Target, this.TransitionContext)).MustHaveHappened());
Expand All @@ -65,15 +65,15 @@ public async Task EntersStatesBelowCommonSuperStateToTarget()
[Fact]
public async Task DoesNotExitCommonSuperState()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Exit(this.root, this.TransitionContext, this.LastActiveStateModifier)).MustNotHaveHappened();
}

[Fact]
public async Task DoesNotEnterCommonSuperState()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Entry(this.root, this.TransitionContext)).MustNotHaveHappened();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public InternalTransitionFacts()
[Fact]
public async Task DoesNotExitState()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Exit(this.Source, this.TransitionContext, this.LastActiveStateModifier)).MustNotHaveHappened();
}

[Fact]
public async Task DoesNotEnterState()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Entry(this.Source, this.TransitionContext)).MustNotHaveHappened();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public SelfTransitionFacts()
[Fact]
public async Task ExitsState()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Exit(this.Source, this.TransitionContext, this.LastActiveStateModifier)).MustHaveHappened();
}

[Fact]
public async Task EntersState()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Entry(this.Target, this.TransitionContext)).MustHaveHappened();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SourceIsDescendantOfTargetTransitionFacts()
[Fact]
public async Task ExitsOfAllStatesFromSourceUpToTarget()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Exit(this.Source, this.TransitionContext, this.LastActiveStateModifier)).MustHaveHappened()
.Then(A.CallTo(() => this.StateLogic.Exit(this.intermediate, this.TransitionContext, this.LastActiveStateModifier)).MustHaveHappened())
Expand All @@ -52,7 +52,7 @@ public async Task ExitsOfAllStatesFromSourceUpToTarget()
[Fact]
public async Task EntersTargetState()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Entry(this.Target, this.TransitionContext)).MustHaveHappened();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SourceIsParentOfTargetTransitionFacts()
[Fact]
public async Task EntersAllStatesBelowSourceDownToTarget()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Entry(this.intermediate, this.TransitionContext)).MustHaveHappened()
.Then(A.CallTo(() => this.StateLogic.Entry(this.Target, this.TransitionContext)).MustHaveHappened());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class SuccessfulTransitionWithExecutedActionsFactsBase : Transit
[Fact]
public async Task ReturnsSuccessfulTransitionResult()
{
var result = await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
var result = await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

result.Should().BeSuccessfulTransitionResultWithNewState(this.Target);
}
Expand All @@ -45,7 +45,7 @@ public async Task ExecutesActions()

this.TransitionDefinition.ActionsModifiable.Add(new ArgumentLessActionHolder(() => executed = true));

await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

executed.Should().BeTrue("actions should be executed");
}
Expand All @@ -56,7 +56,7 @@ public async Task TellsExtensionsAboutExecutedTransition()
var extension = new FakeExtension();
this.ExtensionHost.Extension = extension;

await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

extension.Items.Should().Contain(new FakeExtension.Item(
this.Source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public TransitionDefinedInSuperStateTransitionFacts()
[Fact]
public async Task ExitsAllStatesFromCurrentUpToSource()
{
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier);
await this.Testee.Fire(this.TransitionDefinition, this.TransitionContext, this.LastActiveStateModifier, this.StateDefinitions);

A.CallTo(() => this.StateLogic.Exit(this.current, this.TransitionContext, this.LastActiveStateModifier)).MustHaveHappened()
.Then(A.CallTo(() => this.StateLogic.Exit(this.intermediate, this.TransitionContext, this.LastActiveStateModifier)).MustHaveHappened())
Expand Down
Loading

0 comments on commit cef9aa0

Please sign in to comment.