From 0cc86895742403d8eb3b44fb42447e516b2fbc01 Mon Sep 17 00:00:00 2001 From: Nathan Floris Copier Date: Tue, 9 Dec 2014 19:25:03 -0700 Subject: [PATCH 1/2] [Red] Added test for GetTransitionsFromState. --- .../Transition Machine/TransitionMachine.cs | 5 +++++ nTransitionTests/UseATransitionMachineTests.cs | 18 ++++++++++++++++++ nTransitionTests/nTransitionTest.csproj | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/nTransition/Transition Machine/TransitionMachine.cs b/nTransition/Transition Machine/TransitionMachine.cs index 88e6f8c..433ae4a 100644 --- a/nTransition/Transition Machine/TransitionMachine.cs +++ b/nTransition/Transition Machine/TransitionMachine.cs @@ -35,5 +35,10 @@ public TState Between(TState fromState, TState toState) { return Configuration.Between(fromState, toState); } + + public IEnumerable GetTransitionsFromState(TState fromState) + { + throw new NotImplementedException(); + } } } diff --git a/nTransitionTests/UseATransitionMachineTests.cs b/nTransitionTests/UseATransitionMachineTests.cs index 6083576..9bd0a94 100644 --- a/nTransitionTests/UseATransitionMachineTests.cs +++ b/nTransitionTests/UseATransitionMachineTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.Remoting.Messaging; using eStateMachine.Interfaces; using NUnit.Framework; @@ -64,5 +65,22 @@ public void SimpleCircularMachineDoesNotThrow() t.State = 1; }); } + + [Test] + public void CanGetPossibleTransitionsFromState() + { + var TestMachine = new TransitionMachine(c => + { + c.From(1).To(2).Done(); + c.From(1).To(3).Done(); + c.From(2).To(4).Done(); + c.From(3).To(4).Done(); + c.From(4).To(1).Done(); + }); + var possibleTransitions = TestMachine.GetTransitionsFromState(1).ToArray(); + possibleTransitions.Length.ShouldBe(2); + possibleTransitions[0].ShouldBe(2); + possibleTransitions[1].ShouldBe(3); + } } } diff --git a/nTransitionTests/nTransitionTest.csproj b/nTransitionTests/nTransitionTest.csproj index f990501..fee6c52 100644 --- a/nTransitionTests/nTransitionTest.csproj +++ b/nTransitionTests/nTransitionTest.csproj @@ -66,7 +66,7 @@ - + {78fe77df-239c-4288-92f3-5a9aa05aa848} nTransition From 1535a177e889f552c02778e640c51bf8aabb911f Mon Sep 17 00:00:00 2001 From: Nathan Floris Copier Date: Tue, 9 Dec 2014 19:50:37 -0700 Subject: [PATCH 2/2] [Green] Transition machine has a method to get transitions from state. --- .../TransitionConfiguration.cs | 5 +++++ .../Transition Machine/TransitionMachine.cs | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/nTransition/Transition Machine/TransitionConfiguration.cs b/nTransition/Transition Machine/TransitionConfiguration.cs index 2e34e01..2ebe4e6 100644 --- a/nTransition/Transition Machine/TransitionConfiguration.cs +++ b/nTransition/Transition Machine/TransitionConfiguration.cs @@ -24,6 +24,11 @@ public IEnumerable States } } + public IEnumerable> StateTransitions + { + get { return _stateTransitions.Distinct(); } + } + public TState Between(TState current, TState newState) { var stateTransitions = _stateTransitions.Where(s => s.FromState.CompareTo(current) == 0 && s.ToState.CompareTo( newState) == 0 ); diff --git a/nTransition/Transition Machine/TransitionMachine.cs b/nTransition/Transition Machine/TransitionMachine.cs index 433ae4a..9fd25bb 100644 --- a/nTransition/Transition Machine/TransitionMachine.cs +++ b/nTransition/Transition Machine/TransitionMachine.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace eStateMachine { @@ -36,9 +37,22 @@ public TState Between(TState fromState, TState toState) return Configuration.Between(fromState, toState); } + + /// + /// Get possible states that can be transitioned to + /// + /// State to ne transitioned from + /// IEnumerable of states that can be transitioned to public IEnumerable GetTransitionsFromState(TState fromState) { - throw new NotImplementedException(); + //I'm not sure how efficient this is + foreach (var stateTransition in Configuration.StateTransitions) + { + if (stateTransition.FromState.Equals(fromState)) + { + yield return stateTransition.ToState; + } + } } } }