public void SimpleTransitionTest()
 {
     using (var machine = new TestMachine())
     {
         registerMachineEvents(machine);
         machine.Send(Event.S1_to_S2);
         machine.WaitForPendingEvents();
         assertMachineEvents(true, false, true, false);
         Assert.AreEqual(State.S2, machine.CurrentStateID);
     }
 }
 public void SimpleTransitionTest2()
 {
     using (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.WaitForPendingEvents();
         assertMachineEvents(true, false, true, false);
         Assert.AreEqual(State.S2, machine.CurrentStateID);
     }
 }
 public void Test_machine_with_String_state_and_Int32_event_types()
 {
     using (var machine = new TestMachine <string, int, EventArgs>())
     {
         machine.AddTransition("S1", 12, "S2");
         registerMachineEvents(machine);
         machine.Start("S1");
         machine.Send(12);
         machine.WaitForPendingEvents();
         assertMachineEvents(true, false, true, false);
         Assert.AreEqual("S2", machine.CurrentStateID);
     }
 }
        public void TransitionCompleted_ThrowsError()
        {
            using (var machine = new TestMachine <State, Event, EventArgs>())
            {
                machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);

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

                registerMachineEvents(machine);
                machine.Start(State.S1);
                machine.Send(Event.S2_to_S1);
                machine.WaitForPendingEvents();
                assertMachineEvents(true, true, false, false);
                Assert.AreEqual(State.S1, machine.CurrentStateID);
            }
        }
        public void GuardException_should_not_prevent_machine_from_checking_other_guards()
        {
            using (var machine = new TestMachine <State, Event, EventArgs>())
            {
                machine.AddTransition(State.S1, Event.S1_to_S2, guardException, State.S2);
                machine.AddTransition(State.S1, Event.S1_to_S2, delegate { return(true); }, State.S1_1);

                registerMachineEvents(machine);
                machine.Start(State.S1);
                machine.Send(Event.S1_to_S2);
                machine.WaitForPendingEvents();
                assertMachineEvents(true, false, true, true);
                Assert.AreEqual(State.S1_1, machine.CurrentStateID);
            }
        }
        public void GuardException()
        {
            using (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.WaitForPendingEvents();
                assertMachineEvents(true, true, false, true);
                Assert.AreEqual(State.S1, machine.CurrentStateID);
                Assert.IsInstanceOf <GuardException>(m_lastException);
            }
        }
        public void BeginDispatch()
        {
            using (var machine = new TestMachine <State, Event, int>())
            {
                machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);
                machine.BeginDispatch += (sender, e) =>
                {
                    Assert.AreEqual(State.S1, machine.CurrentStateID);
                    Assert.AreEqual(Event.S1_to_S2, e.EventID);
                    Assert.AreEqual(State.S1, e.SourceStateID);
                    Assert.AreEqual(123, e.EventArgs);
                };

                registerMachineEvents(machine);
                machine.Start(State.S1);
                machine.Send(Event.S1_to_S2, 123);
                machine.WaitForPendingEvents();
                assertMachineEvents(true, false, true, false);
                Assert.AreEqual(State.S2, machine.CurrentStateID);
            }
        }
        public void EntryExceptionOnSend()
        {
            TransitionErrorEventArgs <State, Event, EventArgs> errorEventArgs;

            using (var machine = new TestMachine <State, Event, EventArgs>())
            {
                machine[State.S2].EntryHandler += throwException;
                machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);
                errorEventArgs           = null;
                machine.ExceptionThrown += (sender, args) => errorEventArgs = args;
                registerMachineEvents(machine);
                machine.Start(State.S1);
                machine.Send(Event.S1_to_S2);
                machine.WaitForPendingEvents();
                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);
            Assert.IsInstanceOf <EntryException>(m_lastException);
        }
        public void TransitionActions_ThrowsExceptionTwice()
        {
            using (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.WaitForPendingEvents();
                assertMachineEvents(true, false, true, true);
                Assert.AreEqual(State.S2, machine.CurrentStateID);
                Assert.AreEqual(2, count);
                Assert.IsInstanceOf <ActionException>(m_lastException);
            }
        }