public void SimpleTransitionTest()
        {
            var machine = new TestMachine();

            registerMachineEvents(machine);
            machine.Send(Event.S1_to_S2);
            machine.Execute();
            assertMachineEvents(true, false, true, false);
            Assert.AreEqual(State.S2, machine.CurrentStateID);
        }
        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 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 GuardException()
        {
            var machine = new TestMachine <State, Event, EventArgs>();

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

            registerMachineEvents(machine);
            machine.Start(State.S1);
            machine.Send(Event.S1_to_S2);
            machine.Execute();
            assertMachineEvents(true, true, false, true);
            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 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);
        }