Skip to content

Commit

Permalink
Add IState.IsCurrent property
Browse files Browse the repository at this point in the history
  • Loading branch information
canton7 committed Aug 25, 2015
1 parent 91ecdcf commit ef2047c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/StateMechanic/IState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public interface IState
/// </summary>
string Name { get; }

/// <summary>
/// Gets a value indicating whether this state's parent state machine is in this state
/// </summary>
bool IsCurrent { get; }

/// <summary>
/// Gets the child state machine of this state, if any
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/StateMechanic/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public class State : IState<State>
/// </summary>
public string Name { get { return this.innerState.Name; } }

/// <summary>
/// Gets a value indicating whether this state's parent state machine is in this state
/// </summary>
public bool IsCurrent { get { return this.ParentStateMachine.CurrentState == this; } }

/// <summary>
/// Gets the child state machine of this state, if any
/// </summary>
Expand Down Expand Up @@ -256,6 +261,11 @@ public class State<TStateData> : IState<State<TStateData>>
/// </summary>
public string Name { get { return this.innerState.Name; } }

/// <summary>
/// Gets a value indicating whether this state's parent state machine is in this state
/// </summary>
public bool IsCurrent { get { return this.ParentStateMachine.CurrentState == this; } }

/// <summary>
/// Gets the child state machine of this state, if any
/// </summary>
Expand Down
34 changes: 34 additions & 0 deletions src/StateMechanicUnitTests/IntrospectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,39 @@ public void StateMachineReportsIsInStateCorrectly()
Assert.False(stateMachine.IsInState(state21));
Assert.True(stateMachine.IsInState(state22));
}

[Test]
public void StateReportsIsCurrentCorrectly()
{
var stateMachine = new StateMachine("State Machine");
var state1 = stateMachine.CreateInitialState("State 1");
var state2 = stateMachine.CreateState("State ");
var evt = stateMachine.CreateEvent("Event");
var subSm = state2.CreateChildStateMachine("Sub State Machine");
var state21 = subSm.CreateInitialState("State 2.1");
var state22 = subSm.CreateState("State 2.2");

state1.TransitionOn(evt).To(state2);
state21.TransitionOn(evt).To(state22);

Assert.True(state1.IsCurrent);
Assert.False(state2.IsCurrent);
Assert.False(state21.IsCurrent);
Assert.False(state22.IsCurrent);

evt.Fire();

Assert.False(state1.IsCurrent);
Assert.True(state2.IsCurrent);
Assert.True(state21.IsCurrent);
Assert.False(state22.IsCurrent);

evt.Fire();

Assert.False(state1.IsCurrent);
Assert.True(state2.IsCurrent);
Assert.False(state21.IsCurrent);
Assert.True(state22.IsCurrent);
}
}
}

0 comments on commit ef2047c

Please sign in to comment.