Example #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();
            });
        }
Example #2
0
        public void Map()
        {
            Initializable <SomeClass>
            .Initialized(new SomeClass { SomeValue = "A" })
            .Map(x => x.SomeValue)
            .Should()
            .BeEquivalentTo(Initializable <string> .Initialized("A"));

            Initializable <SomeClass>
            .UnInitialized()
            .Map(x => x.SomeValue)
            .Should()
            .BeEquivalentTo(Initializable <string> .UnInitialized());
        }
Example #3
0
        public void ExtractOr()
        {
            Initializable <string>
            .Initialized("A")
            .ExtractOr("B")
            .Should()
            .Be("A");

            Initializable <string>
            .UnInitialized()
            .ExtractOr("B")
            .Should()
            .Be("B");
        }
Example #4
0
        public void IsInitialized()
        {
            Initializable <SomeClass>
            .Initialized(new SomeClass())
            .IsInitialized
            .Should()
            .BeTrue();

            Initializable <SomeClass>
            .UnInitialized()
            .IsInitialized
            .Should()
            .BeFalse();
        }
Example #5
0
        public void ExtractOrThrow()
        {
            Initializable <string>
            .Initialized("A")
            .ExtractOrThrow()
            .Should()
            .Be("A");

            Initializable <string>
            .UnInitialized()
            .Invoking(x => x.ExtractOrThrow())
            .Should()
            .Throw <InvalidOperationException>()
            .WithMessage(ExceptionMessages.ValueNotInitialized);
        }
Example #6
0
        public void SetsHistoryStatesOnLoadingFromPersistedState()
        {
            var exitedD2 = false;

            var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <States, Events>();

            stateMachineDefinitionBuilder
            .DefineHierarchyOn(States.D)
            .WithHistoryType(HistoryType.Deep)
            .WithInitialSubState(States.D1)
            .WithSubState(States.D2);
            stateMachineDefinitionBuilder
            .In(States.A)
            .On(Events.D).Goto(States.D)
            .On(Events.A);
            stateMachineDefinitionBuilder
            .In(States.D2)
            .ExecuteOnExit(() => exitedD2 = true)
            .On(Events.A).Goto(States.A);
            var testee = stateMachineDefinitionBuilder
                         .WithInitialState(States.A)
                         .Build()
                         .CreatePassiveStateMachine();

            var loader = A.Fake <IStateMachineLoader <States, Events> >();

            A.CallTo(() => loader.LoadHistoryStates())
            .Returns(new Dictionary <States, States>
            {
                { States.D, States.D2 }
            });
            A.CallTo(() => loader.LoadCurrentState())
            .Returns(Initializable <States> .UnInitialized());

            testee.Load(loader);
            testee.Start();
            testee.Fire(Events.D); // should go to loaded last active state D2, not initial state D1
            exitedD2 = false;
            testee.Fire(Events.A);

            testee.Stop();

            exitedD2.Should().BeTrue();
        }
 public StateContainer(string name)
 {
     this.Name           = name;
     this.CurrentStateId = Initializable <TState> .UnInitialized();
 }
Example #8
0
        public void SetsEventsOnLoadingFromPersistedState(string dummyName, Func <StateMachineDefinition <States, Events>, IStateMachine <States, Events> > createStateMachine)
        {
            var enteredB  = false;
            var extension = A.Fake <IExtension <States, Events> >();

            var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <States, Events>();

            stateMachineDefinitionBuilder
            .In(States.A)
            .On(Events.E)
            .Goto(States.B);
            stateMachineDefinitionBuilder
            .In(States.B)
            .ExecuteOnEntry(() => enteredB = true);
            var stateMachineDefinition = stateMachineDefinitionBuilder
                                         .WithInitialState(States.A)
                                         .Build();
            var testee = createStateMachine(stateMachineDefinition);

            testee.AddExtension(extension);

            var loader = A.Fake <IStateMachineLoader <States, Events> >();

            var eventInformation = new EventInformation <Events>(Events.E, null);

            A.CallTo(() => loader.LoadEvents())
            .Returns(new List <EventInformation <Events> > {
                eventInformation
            });
            A.CallTo(() => loader.LoadCurrentState())
            .Returns(Initializable <States> .UnInitialized());

            testee.Load(loader);

            A.CallTo(() => extension.Loaded(
                         A <IStateMachineInformation <States, Events> > .Ignored,
                         A <Initializable <States> > .Ignored,
                         A <IReadOnlyDictionary <States, States> > .Ignored,
                         A <IReadOnlyCollection <EventInformation <Events> > >
                         .That
                         .Matches(c =>
                                  c.Count == 1 &&
                                  c.Contains(eventInformation))))
            .MustHaveHappenedOnceExactly();

            var signal = new AutoResetEvent(false);

            A.CallTo(() =>
                     extension.EnteredInitialState(
                         A <IStateMachineInformation <States, Events> > .Ignored,
                         A <States> .Ignored,
                         A <ITransitionContext <States, Events> > .Ignored))
            .Invokes(() => signal.Set());
            testee.AddExtension(extension);

            testee.Start();

            var stateMachineWasStarted = signal.WaitOne(500);

            stateMachineWasStarted.Should().BeTrue();

            testee.Stop();

            enteredB
            .Should()
            .BeTrue();
        }
Example #9
0
 public StateContainer(string name)
 {
     this.Name         = name;
     this.CurrentState = Initializable <IStateDefinition <TState, TEvent> > .UnInitialized();
 }