示例#1
0
        public void NoMatchingGuard(
            AsyncPassiveStateMachine <int, int> machine)
        {
            bool declined = false;

            "establish state machine with no matching guard".x(async() =>
            {
                machine = new AsyncPassiveStateMachine <int, int>();

                var currentStateExtension = new CurrentStateExtension();
                machine.AddExtension(currentStateExtension);

                machine.In(SourceState)
                .On(Event)
                .If(() => Task.FromResult(false)).Goto(ErrorState);

                machine.TransitionDeclined += (sender, e) => declined = true;

                await machine.Initialize(SourceState);
                await machine.Start();
            });

            "when an event is fired".x(()
                                       => machine.Fire(Event));

            "it should notify about declined transition".x(()
                                                           => declined.Should().BeTrue("TransitionDeclined event should be fired"));
        }
示例#2
0
        public void OtherwiseGuard(
            AsyncPassiveStateMachine <int, int> machine,
            CurrentStateExtension currentStateExtension)
        {
            "establish a state machine with otherwise guard and no machting other guard".x(async() =>
            {
                machine = new AsyncPassiveStateMachine <int, int>();

                currentStateExtension = new CurrentStateExtension();
                machine.AddExtension(currentStateExtension);

                machine.In(SourceState)
                .On(Event)
                .If(() => Task.FromResult(false)).Goto(ErrorState)
                .Otherwise().Goto(DestinationState);

                await machine.Initialize(SourceState);
                await machine.Start();
            });

            "when an event is fired".x(()
                                       => machine.Fire(Event));

            "it should_take_transition_guarded_with_otherwise".x(()
                                                                 => currentStateExtension.CurrentState.Should().Be(DestinationState));
        }
示例#3
0
        public void MatchingGuard(
            AsyncPassiveStateMachine <int, int> machine,
            CurrentStateExtension currentStateExtension)
        {
            "establish a state machine with guarded transitions".x(async() =>
            {
                machine = new AsyncPassiveStateMachine <int, int>();

                currentStateExtension = new CurrentStateExtension();
                machine.AddExtension(currentStateExtension);

                machine.In(SourceState)
                .On(Event)
                .If(() => false).Goto(ErrorState)
                .If(async() => await Task.FromResult(false)).Goto(ErrorState)
                .If(async() => await Task.FromResult(true)).Goto(DestinationState)
                .If(() => true).Goto(ErrorState)
                .Otherwise().Goto(ErrorState);

                await machine.Initialize(SourceState);
                await machine.Start();
            });

            "when an event is fired".x(()
                                       => machine.Fire(Event));

            "it should take transition guarded with first matching guard".x(()
                                                                            => currentStateExtension.CurrentState.Should().Be(DestinationState));
        }
示例#4
0
        public void Background()
        {
            "establish a hierarchical state machine"._(() =>
            {
                this.testExtension = new CurrentStateExtension();

                this.machine = new AsyncPassiveStateMachine <int, int>();

                this.machine.AddExtension(this.testExtension);

                this.machine.DefineHierarchyOn(SuperState)
                .WithHistoryType(HistoryType.None)
                .WithInitialSubState(LeafState);

                this.machine.In(SuperState)
                .ExecuteOnEntry(() => this.entryActionOfSuperStateExecuted = true);
                this.machine.In(LeafState)
                .ExecuteOnEntry(() => this.entryActionOfLeafStateExecuted = true);
            });
        }