Holds configuration for a state machine
Ejemplo n.º 1
0
        public StateMachine(StateMachineConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            Configuration = configuration;
        }
Ejemplo n.º 2
0
 public void StateMachine_ValidConfigPassed_SetsConfigAsCurrentConfig()
 {
     var config = new StateMachineConfiguration { RaiseExceptionOnTriggerMatchingNoTransition = true };
     var machine = new StateMachine<StubStateModel>(config);
     Assert.Same(config, machine.Configuration);
     Assert.True(machine.Configuration.RaiseExceptionOnTriggerMatchingNoTransition);
 }
        public void FluentStateMachineBuilder_CompileConfig_ReturnsNewStateMachineWithDefinedParts_WithConfig()
        {
            var builder = new FluentStateMachineBuilder<StubStateModel>();
            builder.State("s1", null);
            builder.Initiates();
            var config = new StateMachineConfiguration { RaiseExceptionBeforeTransitionToSameState = true };
            var machine = builder.Compile(config);

            Assert.NotNull(machine);
            Assert.Same(config, machine.Configuration);
            Assert.Equal(true, machine.Configuration.RaiseExceptionBeforeTransitionToSameState);
        }
Ejemplo n.º 4
0
        public void StateMachine_Trigger_TransitionsToSameState_ConfigRaiseOnSameStateTranFalse_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionBeforeTransitionToSameState = false };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state1);
            model.CurrentState = state1;

            // set up scenario where state would transition from current to same state.
            // so no true transition would not occur

            // should not throw exception and that's good enough for us
            machine.Trigger(trigger1, model);
        }
Ejemplo n.º 5
0
        public void StateMachine_Trigger_TransitionsToSameState_ConfigRaiseOnSameStateTranTrue_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionBeforeTransitionToSameState = true };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state1);
            model.CurrentState = state1;

            // set up scenario where state would transition from current to same state.
            // so no true transition would not occur

            Assert.Throws<InvalidTriggerException>(() =>
                machine.Trigger(trigger1, model));
        }
Ejemplo n.º 6
0
        public void StateMachine_Trigger_TransitionFromCurrentToSame_ConfiggedNotToRaiseExcep_DoesNotRaiseTransitionChangeEvents()
        {
            var config = new StateMachineConfiguration { RaiseExceptionBeforeTransitionToSameState = false };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");

            var state1 = new State<StubStateModel>("state1");
            TransitionEventArgs<StubStateModel> transitioningArgs = null;
            state1.Entered += (s, e) => { transitioningArgs = e; };
            TransitionEventArgs<StubStateModel> transitionedArgs = null;
            state1.Exiting += (s, e) => { transitionedArgs = e; };

            state1.AddTransition(trigger1, state1);
            model.CurrentState = state1;

            // set up scenario where state would transition from current to same state.
            // so no true transition would not occur

            // should not throw exception and that's good enough for us
            machine.Trigger(trigger1, model);

            // verify that no transition events occurred
            Assert.Null(transitioningArgs);
            Assert.Null(transitionedArgs);
        }
Ejemplo n.º 7
0
        public void StateMachine_Trigger_NoStateOrGlobalTransOnTrigger_ConfigRaiseOnNoTransTrue_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionOnTriggerMatchingNoTransition = true };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var trigger2 = new Trigger("trigger2");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state2);
            model.CurrentState = state1;

            // set up so that current state of state 1 doesn't' define a trans for "trigger2", only "trigger1"

            Assert.Throws<InvalidTriggerException>(() =>
                machine.Trigger(trigger2, model));
        }
Ejemplo n.º 8
0
        public void StateMachine_Trigger_NoStateOrGlobalTransOnTrigger_ConfigRaiseOnNoTransFalse_NotThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionOnTriggerMatchingNoTransition = false };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var trigger2 = new Trigger("trigger2");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state2);
            model.CurrentState = state1;

            // no exception shoudl happen, and that's good enough for us.
            machine.Trigger(trigger2, model);
        }
Ejemplo n.º 9
0
        public void StateMachine_Trigger_NoPassingTransitionOnTrigger_ConfigRaiseOnNoPassingTranTrue_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionOnTriggerMatchingNoPassingTransition = true };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state2, m => 1 == 2);
            model.CurrentState = state1;

            // set up so that there's a matching transition, but the guard would fail when run

            Assert.Throws<InvalidTriggerException>(() =>
                machine.Trigger(trigger1, model));
        }
Ejemplo n.º 10
0
        public void StateMachine_Trigger_NoPassingTransitionOnTrigger_ConfigRaiseOnNoPassingTranFalse_ThrowsInvalidTrigger()
        {
            var config = new StateMachineConfiguration { RaiseExceptionOnTriggerMatchingNoPassingTransition = false };
            var machine = new StateMachine<StubStateModel>(config);
            var model = new StubStateModel();
            var trigger1 = new Trigger("trigger1");
            var state1 = new State<StubStateModel>("state1");
            var state2 = new State<StubStateModel>("state2");
            state1.AddTransition(trigger1, state2, m => 1 == 2);
            model.CurrentState = state1;

            // set up so that there's a matching transition, but the guard would fail when run

            // no exceptions should happen, and that's good enough for us
            machine.Trigger(trigger1, model);
        }