Exemplo n.º 1
0
        public void GoBack()
        {
            var sm = new SolidMachine <TelephoneTrigger>();

            var isGoingToDialling = false;

            sm.State <IdleState>()
            .On(TelephoneTrigger.PickingUpPhone).GoesTo <DiallingState>();

            sm.State <DiallingState>()
            .On(TelephoneTrigger.Answered, () => isGoingToDialling).GoesTo <ConversationState>()
            .On(TelephoneTrigger.Answered, () => !isGoingToDialling).GoesTo <WaitForAnswerState>();

            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);
            sm.Trigger(TelephoneTrigger.Answered);

            Assert.IsTrue(sm.CurrentState is WaitForAnswerState, "Expected state WaitForAnswerState");
            sm.GoBack();
            Assert.IsTrue(sm.CurrentState is DiallingState,
                          string.Format("Expected state DiallingState, was {0}", sm.CurrentState.GetType().Name));

            // Shift the track
            isGoingToDialling = true;
            sm.Trigger(TelephoneTrigger.Answered);

            Assert.IsTrue(sm.CurrentState is ConversationState, "Expected state ConversationState");
            sm.GoBack();
            Assert.IsTrue(sm.CurrentState is DiallingState, "Expected state DiallingState... again");
        }
Exemplo n.º 2
0
        public void CorrectOrderOnTransitions()
        {
            var states = new List <string>();

            var sm = new SolidMachine <int>();

            sm.Transitioned += (sender, args) =>
            {
                var stateName = args.TargetState.Name;
                Console.WriteLine("Transitioned to " + stateName);

                states.Add(stateName);
                if (states.Count == 5)
                {
                    var stateOrder = string.Join("_", states);
                    Assert.IsTrue(stateOrder == "IdleState_StepState1_StepState2_StepState3_StepState4",
                                  string.Format("Wrong state order: {0}", stateOrder));
                }
            };

            sm.State <IdleState>()
            .On(0).GoesTo <StepState1>();
            sm.State <StepState1>()
            .On(1).GoesTo <StepState2>();
            sm.State <StepState2>()
            .On(2).GoesTo <StepState3>();
            sm.State <StepState3>()
            .On(3).GoesTo <StepState4>();
            sm.Start();

            sm.Trigger(0);
        }
Exemplo n.º 3
0
        // Protected methods

        protected virtual void DoEntering(SolidMachine<int> machine)
        {
            // Just fire the trigger based on the type name (e.g. StepState1 -> fire trigger 1)
            var typeName = GetType().Name;
            var trigger = Convert.ToInt32(typeName.Substring(typeName.Length - 1, 1));

            if (trigger < 4)
                machine.Trigger(trigger);
        }
Exemplo n.º 4
0
        /// <summary>
        /// This method is called when any of the trigger buttons is pushed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TriggerButton_Click(object sender, EventArgs e)
        {
            // Get the Tag property of the button as a string
            var tag = (sender as Button).Tag.ToString();

            // Convert it to a TelephoneTrigger
            var trigger = (TelephoneTrigger)Enum.Parse(typeof(TelephoneTrigger), tag);

            _sm.Trigger(trigger);
        }
Exemplo n.º 5
0
        // Protected methods

        protected virtual void DoEntering(SolidMachine <int> machine)
        {
            // Just fire the trigger based on the type name (e.g. StepState1 -> fire trigger 1)
            var typeName = GetType().Name;
            var trigger  = Convert.ToInt32(typeName.Substring(typeName.Length - 1, 1));

            if (trigger < 4)
            {
                machine.Trigger(trigger);
            }
        }
Exemplo n.º 6
0
        public void UsingStateResolver()
        {
            // Lets this test class act as the state resolver...
            var sm = new SolidMachine <TelephoneTrigger>(null, this);

            sm.State <IdleState>()
            .On(TelephoneTrigger.PickingUpPhone).GoesTo <StateWithoutParameterlessConstructor>();

            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            Assert.IsTrue(sm.CurrentState is StateWithoutParameterlessConstructor);
        }
Exemplo n.º 7
0
        public void OneStepGuardedTransition()
        {
            var isPhoneWorking = false;

            var sm = new SolidMachine <TelephoneTrigger>();

            sm.State <IdleState>()
            .On(TelephoneTrigger.PickingUpPhone, () => isPhoneWorking).GoesTo <DiallingState>()
            .On(TelephoneTrigger.PickingUpPhone, () => !isPhoneWorking).GoesTo <TelephoneBrokenState>()
            .On(TelephoneTrigger.IncomingCall).GoesTo <RingingState>();

            sm.State <RingingState>()
            .On(TelephoneTrigger.PickingUpPhone).GoesTo <ConversationState>()
            .On(TelephoneTrigger.NotAnswering).GoesTo <IdleState>();

            sm.State <TelephoneBrokenState>()
            .On(TelephoneTrigger.HangingUp).GoesTo <IdleState>();

            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            Assert.IsTrue(sm.CurrentState is TelephoneBrokenState,
                          string.Format("Telephone state is {0}, expected {1}", sm.CurrentState.GetType().Name,
                                        typeof(TelephoneBrokenState).Name));

            // Reset the machine to IdleState
            sm.Trigger(TelephoneTrigger.HangingUp);

            isPhoneWorking = true;

            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            Assert.IsTrue(sm.CurrentState is DiallingState,
                          string.Format("Telephone state is {0}, expected {1}", sm.CurrentState.GetType().Name,
                                        typeof(DiallingState).Name));
        }
Exemplo n.º 8
0
        public void InvalidTriggerWithHandler()
        {
            var _handlerCalledMessage = "";

            var sm = new SolidMachine <TelephoneTrigger>();

            sm.OnInvalidTrigger((state, trigger) =>
                                { _handlerCalledMessage = string.Format("{0}_{1}", state.Name, trigger); });

            sm.State <IdleState>()
            .On(TelephoneTrigger.PickingUpPhone).GoesTo <DiallingState>();

            sm.State <DiallingState>()
            .On(TelephoneTrigger.HangingUp).GoesTo <IdleState>();

            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);
            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            Assert.IsTrue(_handlerCalledMessage ==
                          string.Format("{0}_{1}", typeof(DiallingState).Name, TelephoneTrigger.PickingUpPhone),
                          string.Format("HandlerCalledMessage not what expected : {0}", _handlerCalledMessage));
        }
Exemplo n.º 9
0
        public void UsingExplicitContext()
        {
            var dateTime = new DateHolder();

            var sm = new SolidMachine <TelephoneTrigger>(dateTime);

            sm.State <IdleState>()
            .On(TelephoneTrigger.PickingUpPhone).GoesTo <DateReportingState>();
            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            // The state should be able to use the specified context (dateTime variable) to report the date.
            Assert.IsTrue(dateTime.CurrentDate.Equals(DateTime.Now.Date), "Wrong date in context!");
        }
Exemplo n.º 10
0
        public void TriggerWithoutStarted()
        {
            try
            {
                var sm = new SolidMachine <TelephoneTrigger>();

                sm.State <IdleState>()
                .IsInitialState()
                .On(TelephoneTrigger.PickingUpPhone).GoesTo <DiallingState>()
                .On(TelephoneTrigger.IncomingCall).GoesTo <RingingState>();

                sm.Trigger(TelephoneTrigger.IncomingCall);

                Assert.Fail("Trigger on unstarted state machine succeeded!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Assert.IsTrue(ex is SolidStateException);
                Assert.IsTrue((ex as SolidStateException).ErrorId == SolidStateConstants.ErrorStateMachineNotStarted);
            }
        }
        public void MultipleGuardsEvaluateToTrue()
        {
            try
            {
                var sm = new SolidMachine<TelephoneTrigger>();

                sm.State<IdleState>()
                    .IsInitialState()
                    .On(TelephoneTrigger.PickingUpPhone, () => ((1 + 1) == 2)).GoesTo<DiallingState>()
                    .On(TelephoneTrigger.PickingUpPhone, () => ((6 / 2) == 3)).GoesTo<RingingState>();

                sm.Start();

                sm.Trigger(TelephoneTrigger.PickingUpPhone);

                Assert.Fail("Multiple guard clauses that return True as permitted!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Assert.IsTrue(ex is SolidStateException);
                Assert.IsTrue((ex as SolidStateException).ErrorId == SolidStateConstants.ErrorMultipleGuardClausesAreTrue);
            }
        }
Exemplo n.º 12
0
        public void MultipleGuardsEvaluateToTrue()
        {
            try
            {
                var sm = new SolidMachine <TelephoneTrigger>();

                sm.State <IdleState>()
                .IsInitialState()
                .On(TelephoneTrigger.PickingUpPhone, () => ((1 + 1) == 2)).GoesTo <DiallingState>()
                .On(TelephoneTrigger.PickingUpPhone, () => ((6 / 2) == 3)).GoesTo <RingingState>();

                sm.Start();

                sm.Trigger(TelephoneTrigger.PickingUpPhone);

                Assert.Fail("Multiple guard clauses that return True as permitted!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Assert.IsTrue(ex is SolidStateException);
                Assert.IsTrue((ex as SolidStateException).ErrorId == SolidStateConstants.ErrorMultipleGuardClausesAreTrue);
            }
        }
Exemplo n.º 13
0
        public void UsingStateResolver()
        {
            // Lets this test class act as the state resolver...
            var sm = new SolidMachine<TelephoneTrigger>(null, this);

            sm.State<IdleState>()
                .On(TelephoneTrigger.PickingUpPhone).GoesTo<StateWithoutParameterlessConstructor>();

            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            Assert.IsTrue(sm.CurrentState is StateWithoutParameterlessConstructor);
        }
Exemplo n.º 14
0
 /// <summary>
 /// The Next button has been clicked.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnNext_Click(object sender, EventArgs e)
 {
     _sm.Trigger(WizardTrigger.Next);
 }
        public void TriggerWithoutStarted()
        {
            try
            {
                var sm = new SolidMachine<TelephoneTrigger>();

                sm.State<IdleState>()
                    .IsInitialState()
                    .On(TelephoneTrigger.PickingUpPhone).GoesTo<DiallingState>()
                    .On(TelephoneTrigger.IncomingCall).GoesTo<RingingState>();

                sm.Trigger(TelephoneTrigger.IncomingCall);

                Assert.Fail("Trigger on unstarted state machine succeeded!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Assert.IsTrue(ex is SolidStateException);
                Assert.IsTrue((ex as SolidStateException).ErrorId == SolidStateConstants.ErrorStateMachineNotStarted);
            }
        }
Exemplo n.º 16
0
        public void OneStepGuardedTransition()
        {
            var isPhoneWorking = false;

            var sm = new SolidMachine<TelephoneTrigger>();
            sm.State<IdleState>()
                .On(TelephoneTrigger.PickingUpPhone, () => isPhoneWorking).GoesTo<DiallingState>()
                .On(TelephoneTrigger.PickingUpPhone, () => !isPhoneWorking).GoesTo<TelephoneBrokenState>()
                .On(TelephoneTrigger.IncomingCall).GoesTo<RingingState>();

            sm.State<RingingState>()
                .On(TelephoneTrigger.PickingUpPhone).GoesTo<ConversationState>()
                .On(TelephoneTrigger.NotAnswering).GoesTo<IdleState>();

            sm.State<TelephoneBrokenState>()
                .On(TelephoneTrigger.HangingUp).GoesTo<IdleState>();

            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            Assert.IsTrue(sm.CurrentState is TelephoneBrokenState,
                          string.Format("Telephone state is {0}, expected {1}", sm.CurrentState.GetType().Name,
                                        typeof (TelephoneBrokenState).Name));

            // Reset the machine to IdleState
            sm.Trigger(TelephoneTrigger.HangingUp);

            isPhoneWorking = true;

            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            Assert.IsTrue(sm.CurrentState is DiallingState,
                          string.Format("Telephone state is {0}, expected {1}", sm.CurrentState.GetType().Name,
                                        typeof (DiallingState).Name));
        }
Exemplo n.º 17
0
        public void GoBack()
        {
            var sm = new SolidMachine<TelephoneTrigger>();

            var isGoingToDialling = false;

            sm.State<IdleState>()
                .On(TelephoneTrigger.PickingUpPhone).GoesTo<DiallingState>();

            sm.State<DiallingState>()
                .On(TelephoneTrigger.Answered, () => isGoingToDialling).GoesTo<ConversationState>()
                .On(TelephoneTrigger.Answered, () => !isGoingToDialling).GoesTo<WaitForAnswerState>();

            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);
            sm.Trigger(TelephoneTrigger.Answered);

            Assert.IsTrue(sm.CurrentState is WaitForAnswerState, "Expected state WaitForAnswerState");
            sm.GoBack();
            Assert.IsTrue(sm.CurrentState is DiallingState,
                          string.Format("Expected state DiallingState, was {0}", sm.CurrentState.GetType().Name));
            
            // Shift the track
            isGoingToDialling = true;
            sm.Trigger(TelephoneTrigger.Answered);

            Assert.IsTrue(sm.CurrentState is ConversationState, "Expected state ConversationState");
            sm.GoBack();
            Assert.IsTrue(sm.CurrentState is DiallingState, "Expected state DiallingState... again");

        }
Exemplo n.º 18
0
        public void InvalidTriggerWithHandler()
        {
            var _handlerCalledMessage = "";

            var sm = new SolidMachine<TelephoneTrigger>();
            sm.OnInvalidTrigger((state, trigger) =>
                { _handlerCalledMessage = string.Format("{0}_{1}", state.Name, trigger); });

            sm.State<IdleState>()
                .On(TelephoneTrigger.PickingUpPhone).GoesTo<DiallingState>();

            sm.State<DiallingState>()
                .On(TelephoneTrigger.HangingUp).GoesTo<IdleState>();

            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);
            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            Assert.IsTrue(_handlerCalledMessage ==
                          string.Format("{0}_{1}", typeof (DiallingState).Name, TelephoneTrigger.PickingUpPhone),
                          string.Format("HandlerCalledMessage not what expected : {0}", _handlerCalledMessage));
        }
Exemplo n.º 19
0
        public void CorrectOrderOnTransitions()
        {
            var states = new List<string>();

            var sm = new SolidMachine<int>();
            sm.Transitioned += (sender, args) =>
                {
                    var stateName = args.TargetState.Name;
                    Console.WriteLine("Transitioned to " + stateName);

                    states.Add(stateName);
                    if (states.Count == 5)
                    {
                        var stateOrder = string.Join("_", states);
                        Assert.IsTrue(stateOrder == "IdleState_StepState1_StepState2_StepState3_StepState4",
                                      string.Format("Wrong state order: {0}", stateOrder));
                    }

                };

            sm.State<IdleState>()
                .On(0).GoesTo<StepState1>();
            sm.State<StepState1>()
                .On(1).GoesTo<StepState2>();
            sm.State<StepState2>()
                .On(2).GoesTo<StepState3>();
            sm.State<StepState3>()
                .On(3).GoesTo<StepState4>();
            sm.Start();

            sm.Trigger(0);
        }
Exemplo n.º 20
0
        public void UsingExplicitContext()
        {
            var dateTime = new DateHolder();

            var sm = new SolidMachine<TelephoneTrigger>(dateTime);

            sm.State<IdleState>()
                .On(TelephoneTrigger.PickingUpPhone).GoesTo<DateReportingState>();
            sm.Start();

            sm.Trigger(TelephoneTrigger.PickingUpPhone);

            // The state should be able to use the specified context (dateTime variable) to report the date.
            Assert.IsTrue(dateTime.CurrentDate.Equals(DateTime.Now.Date), "Wrong date in context!");
        }