Exemple #1
0
        public void LoadingNonInitializedStateMachine(
            PassiveStateMachine <State, Event> loadedMachine)
        {
            "when a not started state machine is loaded".x(() =>
            {
                var loader = new StateMachineLoader <State>();
                loader.SetCurrentState(Initializable <State> .UnInitialized());
                loader.SetHistoryStates(new Dictionary <State, State>());

                var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <State, Event>();
                SetupStates(stateMachineDefinitionBuilder);
                loadedMachine = stateMachineDefinitionBuilder
                                .WithInitialState(State.A)
                                .Build()
                                .CreatePassiveStateMachine();
                loadedMachine.Load(loader);
            });

            "it should not be initialized already".x(() =>
            {
                var stateMachineSaver = new StateMachineSaver <State>();
                loadedMachine.Save(stateMachineSaver);
                stateMachineSaver
                .CurrentStateId
                .IsInitialized
                .Should()
                .BeFalse();
            });
        }
Exemple #2
0
        public void InitializeALoadedStateMachine(
            PassiveStateMachine <int, int> machine,
            Exception receivedException)
        {
            "establish a loaded initialized state machine".x(() =>
            {
                machine = new PassiveStateMachine <int, int>();

                var loader = new Persisting.StateMachineLoader <int>();
                loader.SetCurrentState(new Initializable <int> {
                    Value = 1
                });
                loader.SetHistoryStates(new Dictionary <int, int>());
                machine.Load(loader);
            });

            "when initializing the state machine".x(() =>
                                                    receivedException = Catch.Exception(() =>
                                                                                        machine.Initialize(0)));

            "should throw an invalid operation exception".x(() =>
            {
                receivedException
                .Should().BeAssignableTo <InvalidOperationException>()
                .Which.Message
                .Should().Be(ExceptionMessages.StateMachineIsAlreadyInitialized);
            });
        }
Exemple #3
0
        public void Loading(
            StateMachineSaver <State> saver,
            StateMachineLoader <State> loader,
            FakeExtension extension,
            State sourceState,
            State targetState)
        {
            "establish a saved state machine with history".x(() =>
            {
                var machine = new PassiveStateMachine <State, Event>();

                DefineMachine(machine);
                machine.Initialize(State.A);
                machine.Start();
                machine.Fire(Event.S2);     // set history of super state S
                machine.Fire(Event.B);      // set current state to B

                saver  = new StateMachineSaver <State>();
                loader = new StateMachineLoader <State>();

                machine.Save(saver);
            });

            "when state machine is loaded".x(() =>
            {
                loader.SetCurrentState(saver.CurrentStateId);
                loader.SetHistoryStates(saver.HistoryStates);

                extension         = new FakeExtension();
                var loadedMachine = new PassiveStateMachine <State, Event>();
                loadedMachine.AddExtension(extension);

                DefineMachine(loadedMachine);
                loadedMachine.Load(loader);

                loadedMachine.TransitionCompleted += (sender, args) =>
                {
                    sourceState = args.StateId;
                    targetState = args.NewStateId;
                };

                loadedMachine.Start();
                loadedMachine.Fire(Event.S);
            });

            "it should reset current state".x(() =>
                                              sourceState.Should().Be(State.B));

            "it should reset all history states of super states".x(() =>
                                                                   targetState.Should().Be(State.S2));

            "it should notify extensions".x(()
                                            => extension.LoadedCurrentState
                                            .Should().BeEquivalentTo(State.B));
        }
        public void LoadingEvents(
            PassiveStateMachine <string, int> machine)
        {
            "establish a state machine".x(() =>
            {
                var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>();
                stateMachineDefinitionBuilder
                .In("A")
                .On(1)
                .Goto("B");
                stateMachineDefinitionBuilder
                .In("B")
                .On(2)
                .Goto("C");
                stateMachineDefinitionBuilder
                .In("C")
                .On(3)
                .Goto("A");
                machine = stateMachineDefinitionBuilder
                          .WithInitialState("A")
                          .Build()
                          .CreatePassiveStateMachine();
            });

            "when it is loaded with Events".x(() =>
            {
                var loader = new StateMachineLoader <string, int>();
                loader.SetEvents(new List <EventInformation <int> >
                {
                    new EventInformation <int>(1, null),
                    new EventInformation <int>(2, null),
                });
                machine.Load(loader);
            });

            "it should process those events".x(() =>
            {
                var transitionRecords        = new List <TransitionRecord>();
                machine.TransitionCompleted += (sender, args) =>
                                               transitionRecords.Add(
                    new TransitionRecord(args.EventId, args.StateId, args.NewStateId));

                machine.Start();
                transitionRecords
                .Should()
                .HaveCount(2)
                .And
                .IsEquivalentInOrder(new List <TransitionRecord>
                {
                    new TransitionRecord(1, "A", "B"),
                    new TransitionRecord(2, "B", "C")
                });
            });
        }
Exemple #5
0
        public void LoadingNonInitializedStateMachine(
            PassiveStateMachine <State, Event> loadedMachine)
        {
            "when a non-initialized state machine is loaded".x(() =>
            {
                var loader = new StateMachineLoader <State>();
                loader.SetCurrentState(new Initializable <State>());
                loader.SetHistoryStates(new Dictionary <State, State>());

                loadedMachine = new PassiveStateMachine <State, Event>();
                DefineMachine(loadedMachine);
                loadedMachine.Load(loader);
            });

            "it should not be initialized already".x(() =>
            {
                Action act = () => loadedMachine.Initialize(State.S);
                act.Should().NotThrow();
            });
        }
        public void InitializeALoadedStateMachine(
            PassiveStateMachine<int, int> machine,
            Exception receivedException)
        {
            "establish a loaded state machine"._(() =>
                {
                    machine = new PassiveStateMachine<int, int>();

                    machine.Load(A.Fake<IStateMachineLoader<int>>());
                });

            "when initializing the state machine"._(() =>
                    receivedException = Catch.Exception(() =>
                        machine.Initialize(0)));

            "should throw an invalid operation exception"._(() =>
                {
                    receivedException
                        .Should().BeAssignableTo<InvalidOperationException>();
                    receivedException.Message
                        .Should().Be(ExceptionMessages.StateMachineIsAlreadyInitialized);
                });
        }
Exemple #7
0
        public void LoadingAnInitializedStateMachine(
            PassiveStateMachine <string, int> machine,
            Exception receivedException)
        {
            "establish a started state machine".x(() =>
            {
                var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>();
                stateMachineDefinitionBuilder.In("initial");
                machine = stateMachineDefinitionBuilder
                          .WithInitialState("initial")
                          .Build()
                          .CreatePassiveStateMachine();
                machine.Start();
            });

            "when state machine is loaded".x(() =>
                                             receivedException = Catch.Exception(() => machine.Load(A.Fake <IStateMachineLoader <string> >())));

            "it should throw invalid operation exception".x(() =>
            {
                receivedException.Should().BeOfType <InvalidOperationException>();
                receivedException.Message.Should().Be(ExceptionMessages.StateMachineIsAlreadyInitialized);
            });
        }