Exemplo n.º 1
0
        public void ForcefullyTransitioningFromChildStateMachineCallsExitHandlerOnCurrentState()
        {
            StateHandlerInfo<State> state22ExitInfo = null;

            var sm = new StateMachine("State Machine");
            var evt1 = new Event("Event 1");
            var state1 = sm.CreateInitialState("State 1");
            var state2 = sm.CreateState("State 2");
            var subSm = state2.CreateChildStateMachine();
            var state21 = subSm.CreateInitialState("State 2.1");
            var state22 = subSm.CreateState("State 2.2").WithExit(i => state22ExitInfo = i);

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

            // Enter state2, and start child state machine
            evt1.Fire();
            // Enter State 2.2
            evt1.Fire();
            // Transition from state2 to state1, exiting the child state machine
            sm.ForceTransition(state1, evt1);

            Assert.NotNull(state22ExitInfo);
            Assert.AreEqual(state22, state22ExitInfo.From);
            Assert.AreEqual(state1, state22ExitInfo.To);
            Assert.AreEqual(evt1, state22ExitInfo.Event);
        }
Exemplo n.º 2
0
 public void ThrowsIfForcedTransitionToAStateBelongingToAChildStateMachine()
 {
     var sm = new StateMachine("State Machine");
     var evt = sm.CreateEvent("Event");
     var state = sm.CreateInitialState("Initial State");
     var subSm = state.CreateChildStateMachine();
     var subState = subSm.CreateInitialState("Child Initial State");
     
     Assert.Throws<InvalidStateTransitionException>(() => sm.ForceTransition(subState, evt));
 }
        public void TransitionRaisedWhenForcedTransition()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var state1 = sm.CreateState("state1");
            var evt = new Event("evt");

            TransitionEventArgs<State> ea = null;
            sm.Transition += (o, e) => ea = e;

            sm.ForceTransition(state1, evt);

            Assert.NotNull(ea);
            Assert.AreEqual(initial, ea.From);
            Assert.AreEqual(state1, ea.To);
            Assert.AreEqual(evt, ea.Event);
            Assert.False(ea.IsInnerTransition);
            Assert.AreEqual(EventFireMethod.Fire, ea.EventFireMethod);
        }
        public void ForceTransitionCallsSynchronizerForceTransition()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var evt = new Event("evt");

            var synchronizer = new Mock<IStateMachineSynchronizer>();
            sm.Synchronizer = synchronizer.Object;

            sm.ForceTransition(initial, evt);

            synchronizer.Verify(x => x.ForceTransition(It.IsAny<Action>()));
        }
        public void ForcedTransitionDoesNotOccurUntilActionInvoked()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var state1 = sm.CreateState("state1");
            var evt = new Event("evt");

            var synchronizer = new Mock<IStateMachineSynchronizer>();
            sm.Synchronizer = synchronizer.Object;

            Action forceAction = null;
            synchronizer.Setup(x => x.ForceTransition(It.IsAny<Action>()))
                .Callback<Action>(a => forceAction = a);

            sm.ForceTransition(state1, evt);

            Assert.AreEqual(initial, sm.CurrentState);
            Assert.NotNull(forceAction);

            forceAction();

            Assert.AreEqual(state1, sm.CurrentState);
        }
Exemplo n.º 6
0
        public void ForceTransitionIsQueued()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var state1 = sm.CreateState("state1");
            var state2 = sm.CreateState("state2");
            var evt = sm.CreateEvent("evt");

            State entryFrom = null;
            state2.EntryHandler = i => entryFrom = i.From;

            initial.TransitionOn(evt).To(state1).WithHandler(i => sm.ForceTransition(state2, evt));

            evt.Fire();

            Assert.AreEqual(state1, entryFrom);
        }
Exemplo n.º 7
0
        public void ThrowsIfForcedTransitionOnAChildEvent()
        {
            var sm = new StateMachine("State Machine");
            var state1 = sm.CreateInitialState("Initial State");
            var state2 = sm.CreateState("State 2");
            var subSm = state1.CreateChildStateMachine();
            var evt = subSm.CreateEvent("Event");

            Assert.Throws<InvalidEventTransitionException>(() => sm.ForceTransition(state2, evt));
        }
Exemplo n.º 8
0
        public void ForcefullyTransitioningToChildStateMachineCallsEntryHandlerOnInitialState()
        {
            StateHandlerInfo<State> state21EntryInfo = null;

            var sm = new StateMachine("State Machine");
            var evt = new Event("Event");
            var state1 = sm.CreateInitialState("State 1");
            var state2 = sm.CreateState("State 2");
            var subSm = state2.CreateChildStateMachine();
            var state21 = subSm.CreateInitialState("State 2.1").WithEntry(i => state21EntryInfo = i);

            sm.ForceTransition(state2, evt);

            Assert.NotNull(state21EntryInfo);
            Assert.AreEqual(state1, state21EntryInfo.From);
            Assert.AreEqual(state21, state21EntryInfo.To);
            Assert.AreEqual(evt, state21EntryInfo.Event);
        }
Exemplo n.º 9
0
        public void ForcefullyTransitioningToMoreChildStateCallsCorrectHandlers()
        {
            var log = new List<Tuple<string, StateHandlerInfo<State>>>();

            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");

            var childSm1 = state1.CreateChildStateMachine("childSm1");
            var state11 = childSm1.CreateInitialState("state11");
            var state12 = childSm1.CreateState("state12");

            var childSm11 = state11.CreateChildStateMachine("childsm11");
            var state111 = childSm11.CreateInitialState("state111");
            var state112 = childSm11.CreateState("state112");

            var childSm12 = state12.CreateChildStateMachine("childSm12");
            var state121 = childSm12.CreateInitialState("state121");
            var state122 = childSm12.CreateState("state122");

            var evt = new Event("evt");

            // Start off in state112, forcefully transition to state122
            sm.ForceTransition(state112, evt);

            state1.EntryHandler = x => log.Add(Tuple.Create("state1 Entry", x));
            state1.ExitHandler = x => log.Add(Tuple.Create("state1 Exit", x));

            state11.EntryHandler = x => log.Add(Tuple.Create("state11 Entry", x));
            state11.ExitHandler = x => log.Add(Tuple.Create("state11 Exit", x));

            state111.EntryHandler = x => log.Add(Tuple.Create("state111 Entry", x));
            state111.ExitHandler = x => log.Add(Tuple.Create("state111 Exit", x));

            state112.EntryHandler = x => log.Add(Tuple.Create("state112 Entry", x));
            state112.ExitHandler = x => log.Add(Tuple.Create("state112 Exit", x));

            state12.EntryHandler = x => log.Add(Tuple.Create("state12 Entry", x));
            state12.ExitHandler = x => log.Add(Tuple.Create("state12 Exit", x));

            state121.EntryHandler = x => log.Add(Tuple.Create("state121 Entry", x));
            state121.ExitHandler = x => log.Add(Tuple.Create("state121 Exit", x));

            state122.EntryHandler = x => log.Add(Tuple.Create("state122 Entry", x));
            state122.ExitHandler = x => log.Add(Tuple.Create("state122 Exit", x));

            sm.ForceTransition(state122, evt);

            Assert.AreEqual(6, log.Count);

            Assert.AreEqual("state112 Exit", log[0].Item1);
            Assert.AreEqual(evt, log[0].Item2.Event);
            Assert.AreEqual(state112, log[0].Item2.From);
            Assert.AreEqual(state12, log[0].Item2.To);

            Assert.AreEqual("state11 Exit", log[1].Item1);
            Assert.AreEqual(evt, log[1].Item2.Event);
            Assert.AreEqual(state11, log[1].Item2.From);
            Assert.AreEqual(state12, log[1].Item2.To);

            Assert.AreEqual("state12 Entry", log[2].Item1);
            Assert.AreEqual(evt, log[2].Item2.Event);
            Assert.AreEqual(state11, log[2].Item2.From);
            Assert.AreEqual(state12, log[2].Item2.To);

            Assert.AreEqual("state121 Entry", log[3].Item1);
            Assert.AreEqual(evt, log[3].Item2.Event);
            Assert.AreEqual(state11, log[3].Item2.From);
            Assert.AreEqual(state121, log[3].Item2.To);

            Assert.AreEqual("state121 Exit", log[4].Item1);
            Assert.AreEqual(evt, log[4].Item2.Event);
            Assert.AreEqual(state121, log[4].Item2.From);
            Assert.AreEqual(state122, log[4].Item2.To);

            Assert.AreEqual("state122 Entry", log[5].Item1);
            Assert.AreEqual(evt, log[5].Item2.Event);
            Assert.AreEqual(state121, log[5].Item2.From);
            Assert.AreEqual(state122, log[5].Item2.To);
        }
Exemplo n.º 10
0
 public void ForceTransitionThrowsIfToStateIsNull()
 {
     var sm = new StateMachine();
     var evt = new Event("evt");
     Assert.Throws<ArgumentNullException>(() => sm.ForceTransition(null, evt));
 }
Exemplo n.º 11
0
 public void ForceTransitionThrowsIfEventIsNull()
 {
     var sm = new StateMachine();
     var state1 = sm.CreateInitialState("state1");
     Assert.Throws<ArgumentNullException>(() => sm.ForceTransition(state1, null));
 }