public void Superstate_should_handle_event_when_guard_of_substate_does_not_pass()
        {
            var machine = new TestMachine <State, Event, EventArgs>();

            machine.AddTransition(State.S1, Event.E1, State.S2);
            machine.AddTransition(State.S1_1, Event.E1, State.S1_2);
            machine.AddTransition(State.S1_2, Event.E1, (sender, e) => false, State.S1_1);
            machine.SetupSubstates(State.S1, HistoryType.None, State.S1_1, State.S1_2);

            registerMachineEvents(machine);
            machine.Start(State.S1);
            Assert.AreEqual(State.S1_1, machine.CurrentStateID);
            machine.Send(Event.E1);
            machine.Execute();
            Assert.AreEqual(State.S1_2, machine.CurrentStateID);
            machine.Send(Event.E1);
            machine.Execute();
            Assert.AreEqual(State.S2, machine.CurrentStateID);
            assertMachineEvents(true, false, true, false);
        }
        public void SimpleTransitionTest2()
        {
            var machine = new TestMachine <State, Event, EventArgs>();

            machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);

            registerMachineEvents(machine);
            machine.Start(State.S1);
            machine.Send(Event.S1_to_S2);
            machine.Execute();
            assertMachineEvents(true, false, true, false);
            Assert.AreEqual(State.S2, machine.CurrentStateID);
        }
        public void TransitionDeclined()
        {
            var machine = new TestMachine <State, Event, EventArgs>();

            machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);
            machine[State.S1].ExitHandler += throwException;
            registerMachineEvents(machine);
            machine.Start(State.S1);
            machine.Send(Event.S2_to_S1);
            machine.Execute();
            assertMachineEvents(true, true, false, false);
            Assert.AreEqual(State.S1, machine.CurrentStateID);
        }
        public void TransitionCompleted_ThrowsError()
        {
            var machine = new TestMachine <State, Event, EventArgs>();

            machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);

            registerMachineEvents(machine);
            machine.TransitionCompleted += (sender, e) => { throw new Exception(); };
            machine.Start(State.S1);
            machine.Send(Event.S1_to_S2);
            machine.Execute();
            assertMachineEvents(true, false, true, true);
            Assert.AreEqual(State.S2, machine.CurrentStateID);
        }
        public void EntryExceptionOnInit()
        {
            var machine = new TestMachine <State, Event, EventArgs>();

            machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);
            machine[State.S1].EntryHandler += throwException;
            registerMachineEvents(machine);
            TransitionErrorEventArgs <State, Event, EventArgs> args = null;

            machine.ExceptionThrown += (sender, e) => args = e;
            machine.Start(State.S1);
            assertMachineEvents(false, false, false, true);
            Assert.AreEqual(State.S1, machine.CurrentStateID);
            Assert.AreEqual(false, args.MachineInitialized);
        }
        public void EntryExceptionOnSend()
        {
            TransitionErrorEventArgs <State, Event, EventArgs> errorEventArgs;
            var machine = new TestMachine <State, Event, EventArgs>();

            machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);
            machine[State.S2].EntryHandler += throwException;
            errorEventArgs           = null;
            machine.ExceptionThrown += (sender, args) => errorEventArgs = args;
            registerMachineEvents(machine);
            machine.Start(State.S1);
            machine.Send(Event.S1_to_S2);
            machine.Execute();
            assertMachineEvents(true, false, true, true);

            Assert.AreEqual(State.S1, errorEventArgs.SourceStateID);
            Assert.AreEqual(State.S2, machine.CurrentStateID);
            Assert.AreEqual(Event.S1_to_S2, errorEventArgs.EventID);
        }
        public void TransitionActions_ThrowsExceptionTwice()
        {
            var machine = new TestMachine <State, Event, EventArgs>();
            var count   = 0;
            EventHandler <TransitionEventArgs <State, Event, EventArgs> > actionHandler =
                (sender, e) =>
            {
                count++;
                throwException(sender, e);
            };

            machine.AddTransition(State.S1, Event.S1_to_S2, State.S2, actionHandler, actionHandler);
            registerMachineEvents(machine);
            machine.Start(State.S1);
            machine.Send(Event.S1_to_S2);
            machine.Execute();
            assertMachineEvents(true, false, true, true);
            Assert.AreEqual(State.S2, machine.CurrentStateID);
            Assert.AreEqual(2, count);
        }
        public void BeginDispatch()
        {
            var machine = new TestMachine <State, Event, int>();

            machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);
            IPassiveStateMachine <State, Event, int> m = machine;

            m.BeginDispatch += (sender, e) =>
            {
                Assert.AreEqual(State.S1, m.CurrentStateID);
                Assert.AreEqual(Event.S1_to_S2, e.EventID);
                Assert.AreEqual(State.S1, e.SourceStateID);
                Assert.AreEqual(123, e.EventArgs);
            };

            registerMachineEvents(m);
            machine.Start(State.S1);
            m.Send(Event.S1_to_S2, 123);
            m.Execute();
            assertMachineEvents(true, false, true, false);
            Assert.AreEqual(State.S2, machine.CurrentStateID);
        }