コード例 #1
0
 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);
     }
 }
コード例 #2
0
 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);
     }
 }
コード例 #3
0
 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);
     }
 }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
        public void EntryExceptionOnInit()
        {
            TransitionErrorEventArgs <State, Event, EventArgs> args;

            using (var machine = new TestMachine <State, Event, EventArgs>())
            {
                machine[State.S1].EntryHandler += throwException;
                machine.AddTransition(State.S1, Event.S1_to_S2, State.S2);
                registerMachineEvents(machine);
                args = null;
                machine.ExceptionThrown += (sender, e) => args = e;
                machine.Start(State.S1);
                machine.WaitForPendingEvents();
                assertMachineEvents(false, false, false, true);
                Assert.AreEqual(State.S1, machine.CurrentStateID);
            }
            Assert.AreEqual(false, args.MachineInitialized);
            Assert.IsInstanceOf <EntryException>(m_lastException);
        }
コード例 #9
0
        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);
            }
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
            }
        }