예제 #1
0
        public void NoEventsAreQueuedIfAGuardThrowsAnException()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var state2 = sm.CreateState("state2");

            var evt = new Event("evt");
            var evt2 = new Event("evt2");

            var exception = new Exception("foo");
            state1.TransitionOn(evt).To(state2).WithGuard(i =>
            {
                evt.Fire();
                throw exception;
            });
            state1.TransitionOn(evt2).To(state2);
            state2.TransitionOn(evt).To(state1);

            Assert.Throws<Exception>(() => evt.Fire());

            // Make sure that an event wasn't queued
            evt2.Fire();

            Assert.AreEqual(state2, sm.CurrentState);
        }
예제 #2
0
        public void FormatsStateMachines()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var state2 = sm.CreateState("state2");

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

            var child2 = state2.CreateChildStateMachine("child2");
            var state21 = child2.CreateInitialState("state21");
            var state22 = child2.CreateState("state22");

            var evt1 = new Event("evt1");
            var evt2 = new Event<string>("evt2");

            state1.InnerSelfTransitionOn(evt1);
            state1.TransitionOn(evt2).To(state2);

            state11.TransitionOn(evt2).ToDynamic(_ => state12);
            state12.TransitionOn(evt1).To(state11);

            state21.TransitionOn(evt1).To(state22);
            state21.TransitionOn(evt2).To(state22);

            var dot = sm.FormatDot(colorize: true);
            var dgml = sm.FormatDgml(colorize: true);
        }
예제 #3
0
        public void FormatsStateMachinesWithNoNames()
        {
            var sm = new StateMachine();
            var state1 = sm.CreateInitialState();
            var state2 = sm.CreateState();

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

            var child2 = state2.CreateChildStateMachine();
            var state21 = child2.CreateInitialState();
            var state22 = child2.CreateState();

            var evt1 = new Event();
            var evt2 = new Event<string>();

            state1.InnerSelfTransitionOn(evt1);
            state1.TransitionOn(evt2).To(state2);

            state11.TransitionOn(evt2).ToDynamic(_ => state12);
            state12.TransitionOn(evt1).To(state11);

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

            var dot = sm.FormatDot();
            var dgml = sm.FormatDgml();
        }
        public void TransitionNotFoundRaisedWhenTransitionNotFoundOnChild()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var state1 = sm.CreateState("state 1");
            var child = initial.CreateChildStateMachine();
            var childInitial = child.CreateInitialState("childInitial");
            var evt = new Event("evt");

            state1.InnerSelfTransitionOn(evt);

            TransitionNotFoundEventArgs<State> ea = null;
            sm.TransitionNotFound += (o, e) =>
            {
                ea = e;
            };

            evt.TryFire();

            Assert.NotNull(ea);
            Assert.AreEqual(evt, ea.Event);
            Assert.AreEqual(initial, ea.From);
            Assert.AreEqual(sm, ea.StateMachine);
            Assert.AreEqual(EventFireMethod.TryFire, ea.EventFireMethod);
        }
예제 #5
0
        public void CancellationTokenCancelsOperation()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var doingOperation = sm.CreateState("doingOperation");
            var completedOperation = sm.CreateState("completedOperation");
            var evt = new Event("evt");

            initial.TransitionOn(evt).To(doingOperation);
            doingOperation.TransitionOn(evt).To(completedOperation);

            var cts = new CancellationTokenSource();

            var op = new Operation<State>(evt, doingOperation, completedOperation);

            var task = op.TryFireAsync(cts.Token);

            Assert.False(task.IsCompleted);
            Assert.False(task.IsCanceled);

            cts.Cancel();

            Assert.True(task.IsCanceled);

            // Check nothing explodes
            evt.Fire();
        }
        public void CurrentChildStateReflectsCurrentStateOfChild()
        {
            var sm = new StateMachine("state machine");
            var initialState = sm.CreateInitialState("initial state");
            var state1 = sm.CreateState("state1");
            var childSm = state1.CreateChildStateMachine();
            var childInitialState = childSm.CreateInitialState("childInitialState");
            var childState1 = childSm.CreateState("childState1");

            var evt = new Event("evt");
            var evt2 = new Event("evt2");

            initialState.TransitionOn(evt).To(state1);
            childInitialState.TransitionOn(evt).To(childState1);
            state1.TransitionOn(evt2).To(initialState);

            Assert.AreEqual(initialState, sm.CurrentChildState);

            evt.Fire();

            Assert.AreEqual(childInitialState, sm.CurrentChildState);

            evt.Fire();

            Assert.AreEqual(childState1, sm.CurrentChildState);

            evt2.Fire();

            Assert.AreEqual(initialState, sm.CurrentChildState);
        }
예제 #7
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);
        }
예제 #8
0
        public void TryFireReturnsFalseIfTransitionNotFound()
        {
            var sm = new StateMachine("sm");
            var initialState = sm.CreateInitialState("initialState");
            var state1 = sm.CreateState("state1");
            var evt = new Event("evt");
            state1.InnerSelfTransitionOn(evt);

            Assert.False(evt.TryFire());
        }
예제 #9
0
        public void DoesNotThrowIfParentEventUsedInChildStateMachine()
        {
            var sm = new StateMachine("State Machine");
            var state = sm.CreateInitialState("Initial State");
            var subSm = state.CreateChildStateMachine();
            var subState = subSm.CreateInitialState("Child Initial State");
            var evt = new Event("Event");

            Assert.DoesNotThrow(() => subState.TransitionOn(evt).To(subState));
        }
        public void DynamicTransitionStateSelectorThrowsIfNullIsSet()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var evt = new Event<string>("evt");

            Assert.Throws<ArgumentNullException>(() => state1.TransitionOn(evt).To(null));

            var transition = state1.TransitionOn(evt).ToDynamic(i => null);
            Assert.Throws<ArgumentNullException>(() => transition.StateSelector = null);
        }
예제 #11
0
        public void DoesNotThrowIfTransitionAddedAfterAGuardedTransition()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var state2 = sm.CreateState("state2");

            var evt = new Event("evt");

            state1.TransitionOn(evt).To(state2).WithGuard(i => true);
            Assert.DoesNotThrow(() => state1.TransitionOn(evt).To(state1));
        }
예제 #12
0
        public void SetUp()
        {
            this.stateMachine = new StateMachine("State Machine");

            this.state1 = this.stateMachine.CreateInitialState("State 1");
            this.state2 = this.stateMachine.CreateState("State 2");

            this.event1 = this.stateMachine.CreateEvent("Event 1");

            this.transition12 = this.state1.TransitionOn(this.event1).To(this.state2);
        }
예제 #13
0
        public void DoesNotThrowIfTransitionAddedAfterADynamicTransition()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var state2 = sm.CreateState("state2");

            var evt = new Event("evt");

            state1.TransitionOn(evt).ToDynamic(i => null);
            Assert.DoesNotThrow(() => state1.TransitionOn(evt).To(state1));
        }
예제 #14
0
        public void InnerTransitionAddedToState()
        {
            var stateMachine = new StateMachine("State Machine");
            var state1 = stateMachine.CreateState("State 1");
            var evt = new Event("Event");
            state1.InnerSelfTransitionOn(evt);

            Assert.AreEqual(1, state1.Transitions.Count);
            Assert.AreEqual(state1, state1.Transitions[0].From);
            Assert.AreEqual(state1, state1.Transitions[0].To);
            Assert.AreEqual(evt, state1.Transitions[0].Event);
            Assert.True(state1.Transitions[0].IsInnerTransition);
        }
예제 #15
0
        public void EventTTryFireUsesDefaultEventData()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var state2 = sm.CreateState("state2");
            var evt = new Event<int>("evt");

            int? eventData = null;
            state1.TransitionOn(evt).To(state2).WithHandler(i => eventData = i.EventData);

            ((IEvent)evt).TryFire();
            Assert.AreEqual(0, eventData);
        }
        public void CurrentStateReflectsCurrentState()
        {
            var sm = new StateMachine("state machine");
            var initialState = sm.CreateInitialState("initial state");
            var state1 = sm.CreateState("state1");
            var evt = new Event("evt");
            initialState.TransitionOn(evt).To(state1);

            Assert.AreEqual(initialState, sm.CurrentState);

            evt.Fire();

            Assert.AreEqual(state1, sm.CurrentState);
        }
예제 #17
0
        public void ResetResetsCurrentStateOfStateMachine()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var state1 = sm.CreateState("state1");
            var evt = new Event("evt");
            initial.TransitionOn(evt).To(state1);

            evt.Fire();

            sm.Reset();

            Assert.AreEqual(initial, sm.CurrentState);
        }
        public void EventTryFireCallsSynchronizerEventFire()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var evt = new Event("evt");
            initial.TransitionOn(evt).To(initial);

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

            evt.TryFire();

            synchronizer.Verify(x => x.FireEvent(It.IsAny<Func<bool>>(), EventFireMethod.TryFire));
        }
예제 #19
0
        public void FireThrowsIfTransitionNotFound()
        {
            var sm = new StateMachine("sm");
            var initialState = sm.CreateInitialState("initialState");
            var state1 = sm.CreateState("state1");
            var evt = new Event("evt");

            state1.TransitionOn(evt).To(initialState);

            var e = Assert.Throws<TransitionNotFoundException>(() => evt.Fire());
            Assert.AreEqual(initialState, e.From);
            Assert.AreEqual(evt, e.Event);
            Assert.AreEqual(sm, e.StateMachine);
        }
        public void DynamicTransitionStateSelectorIsSettable()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var state2 = sm.CreateState("state2");
            var evt = new Event("evt");

            var transition = state1.TransitionOn(evt).ToDynamic(i => state2);
            transition.StateSelector = i => null;

            evt.TryFire();

            Assert.AreEqual(state1, sm.CurrentState);
        }
        public void IgnoredTEventDontTriggerTransitionNotFoundEvents()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var evt = new Event<string>("evt");

            state1.Ignore(evt);

            bool called = false;
            sm.TransitionNotFound += (o, e) => called = true;

            evt.Fire("foo");

            Assert.False(called);
        }
        public void IgnoredEventsDontTakePriorityOverNormalTransitions()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var state2 = sm.CreateState("state2");
            var evt = new Event("evt");
            var evt2 = new Event("evt2");

            state1.TransitionOn(evt).To(state2).WithGuard(i => true);
            state1.Ignore(evt, evt2);

            evt.Fire();

            Assert.True(state2.IsCurrent);
        }
예제 #23
0
        public void FirstRegisteredTransitionWins()
        {
            var sm = new StateMachine("state machine");
            var initial = sm.CreateInitialState("initial");
            var state1 = sm.CreateState("state1");
            var state2 = sm.CreateState("state2");

            var evt = new Event("evt");

            initial.TransitionOn(evt).To(state2).WithGuard(i => true);
            initial.TransitionOn(evt).To(state1);

            evt.Fire();

            Assert.AreEqual(state2, sm.CurrentState);
        }
예제 #24
0
        public void CorrectHandlersAreInvokedInNormalTransition()
        {
            var events = new List<string>();
            var sm = new StateMachine("State Machine");
            var evt = new Event("Event");
            var state1 = sm.CreateInitialState("State 1")
                .WithEntry(i => events.Add("State 1 Entry"))
                .WithExit(i => events.Add("State 1 Exit"));
            var state2 = sm.CreateState("State 2")
                .WithEntry(i => events.Add("State 2 Entry"))
                .WithExit(i => events.Add("State 2 Exit"));
            var transition = state1.TransitionOn(evt).To(state2).WithHandler(i => events.Add("Transition 1 2"));

            evt.Fire();

            Assert.That(events, Is.EquivalentTo(new[] { "State 1 Exit", "Transition 1 2", "State 2 Entry" }));
        }
예제 #25
0
        public void FireThrowsIfEventFireThrows()
        {
            var sm = new StateMachine("sm");
            var initial = sm.CreateInitialState("initial");
            var operation = sm.CreateState("state2");
            var success = sm.CreateState("success");
            var evt = new Event("evt");

            operation.TransitionOn(evt).To(operation);

            var op = new Operation<State>(evt, operation, success);

            var task = op.FireAsync();

            Assert.True(task.IsFaulted);
            Assert.Throws<TransitionNotFoundException>(() => task.GetAwaiter().GetResult());
        }
예제 #26
0
        public void StateMachineReportsCurrentStateCorrectly()
        {
            var stateMachine = new StateMachine("State Machine");
            var state1 = stateMachine.CreateInitialState("State 1");
            var state2 = stateMachine.CreateState("State 2");
            var evt = new Event("Event");
            var subSm = state2.CreateChildStateMachine();
            var state21 = subSm.CreateInitialState("State 2.1");

            state1.TransitionOn(evt).To(state2);
            evt.Fire();

            Assert.AreEqual(state2, stateMachine.CurrentState);
            Assert.AreEqual(state2, ((IStateMachine)stateMachine).CurrentState);

            Assert.AreEqual(state21, subSm.CurrentState);
            Assert.AreEqual(state21, ((IStateMachine)subSm).CurrentState);
        }
예제 #27
0
        public void DoesNotFireHandlersWhenTransitioningBetweenTwoStatesInGroup()
        {
            var sm = new StateMachine("State Machine");
            var state1 = sm.CreateInitialState("State 1");
            var state2 = sm.CreateState("State 2");
            var evt = new Event("Event");
            bool fired = false;
            var group = new StateGroup<State>("Group")
                .WithEntry(i => fired = true)
                .WithExit(i => fired = true);
            state1.AddToGroup(group);
            state2.AddToGroup(group);
            state1.TransitionOn(evt).To(state2);

            evt.Fire();

            Assert.False(fired);
        }
예제 #28
0
        public void CorrectInfoIsGivenInExitHandlerT()
        {
            StateHandlerInfo<State> handlerInfo = null;

            var sm = new StateMachine("State Machine");
            var evt = new Event<string>("Event");
            var state1 = sm.CreateInitialState("State 1").WithExit(i => handlerInfo = i);
            var state2 = sm.CreateState("State 2");
            state1.TransitionOn(evt).To(state2);

            evt.Fire("foo");

            Assert.NotNull(handlerInfo);
            Assert.AreEqual(state1, handlerInfo.From);
            Assert.AreEqual(state2, handlerInfo.To);
            Assert.AreEqual(evt, handlerInfo.Event);
            Assert.AreEqual("foo", handlerInfo.EventData);
        }
        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 IgnoredEventsDoTriggerEventIgnoredEvents()
        {
            var sm = new StateMachine("sm");
            var state1 = sm.CreateInitialState("state1");
            var evt = new Event<string>("evt");
            var evt2 = new Event<string>("evt2");

            state1.Ignore(evt, evt2);

            EventIgnoredEventArgs<State> ea = null;
            sm.EventIgnored += (o, e) => ea = e;

            evt.Fire("foo");

            Assert.NotNull(ea);
            Assert.AreEqual(evt, ea.Event);
            Assert.AreEqual(state1, ea.State);
            Assert.AreEqual(EventFireMethod.Fire, ea.EventFireMethod);
        }