public void When_Predicate_Triggering_With_Matching_Value()
        {
            var stateMachine = new StateMachineBuilder().CreateStateMachine<char, int, StateMachineContext<char>>(
                    sm => sm.DefineState('a')
                        .WithTrigger(1, (string x) => x == "yes", 'x')
                        .WithTrigger(1, (string x) => x == "no", 'y')
                );

            var originalContext = new StateMachineContext<char>('a');

            Assert.Equal('x', stateMachine.Fire(originalContext, 1, "yes").GetState());
            Assert.Equal('y', stateMachine.Fire(originalContext, 1, "no").GetState());
        }
        public void When_Predicate_Triggering_With_Matching_Value()
        {
            var stateMachine = new StateMachineBuilder().CreateStateMachine <char, int, StateMachineContext <char> >(
                sm => sm.DefineState('a')
                .WithTrigger(1, (string x) => x == "yes", 'x')
                .WithTrigger(1, (string x) => x == "no", 'y')
                );

            var originalContext = new StateMachineContext <char>('a');

            Assert.Equal('x', stateMachine.Fire(originalContext, 1, "yes").GetState());
            Assert.Equal('y', stateMachine.Fire(originalContext, 1, "no").GetState());
        }
        public void When_Trigger_Does_Not_Exist()
        {
            var stateMachine = new StateMachineBuilder().CreateStateMachine<char, int, StateMachineContext<char>>(
                    sm => sm.DefineState('a')
                        .WithTrigger(1, 'b')
                );

            var originalContext = new StateMachineContext<char>('a');

            Assert.Throws<InvalidTriggerException<char, int>>(() => stateMachine.Fire(originalContext, 2));
        }
        public void When_Trigger_Does_Not_Exist()
        {
            var stateMachine = new StateMachineBuilder().CreateStateMachine <char, int, StateMachineContext <char> >(
                sm => sm.DefineState('a')
                .WithTrigger(1, 'b')
                );

            var originalContext = new StateMachineContext <char>('a');

            Assert.Throws <InvalidTriggerException <char, int> >(() => stateMachine.Fire(originalContext, 2));
        }
        public void When_Predicate_Triggering_With_Non_Matching_Value()
        {
            var stateMachine = new StateMachineBuilder().CreateStateMachine<char, int, StateMachineContext<char>>(
                    sm => sm.DefineState('a')
                        .WithTrigger(1, (string x) => x == "yes", 'x')
                        .WithTrigger(1, (string x) => x == "no", 'y')
                );

            var originalContext = new StateMachineContext<char>('a');

            Assert.Throws<InvalidTriggerException<char, int>>(() => stateMachine.Fire(originalContext, 1, "maybe"));
        }
        public void When_Predicate_Triggering_With_Non_Matching_Value()
        {
            var stateMachine = new StateMachineBuilder().CreateStateMachine <char, int, StateMachineContext <char> >(
                sm => sm.DefineState('a')
                .WithTrigger(1, (string x) => x == "yes", 'x')
                .WithTrigger(1, (string x) => x == "no", 'y')
                );

            var originalContext = new StateMachineContext <char>('a');

            Assert.Throws <InvalidTriggerException <char, int> >(() => stateMachine.Fire(originalContext, 1, "maybe"));
        }
        public void When_Trigger_Exists()
        {
            var stateMachine = new StateMachineBuilder().CreateStateMachine<char, int, StateMachineContext<char>>(
                    sm => sm.DefineState('a')
                        .WithTrigger(1, 'b')
                );

            var originalContext = new StateMachineContext<char>('a');

            var newContext = stateMachine.Fire(originalContext, 1);

            Assert.Equal(newContext.GetState(), 'b');
        }
        public void When_Trigger_Exists()
        {
            var stateMachine = new StateMachineBuilder().CreateStateMachine <char, int, StateMachineContext <char> >(
                sm => sm.DefineState('a')
                .WithTrigger(1, 'b')
                );

            var originalContext = new StateMachineContext <char>('a');

            var newContext = stateMachine.Fire(originalContext, 1);

            Assert.Equal(newContext.GetState(), 'b');
        }
        public void EntryActionException()
        {
            var exception = new Exception();

            var extension      = A.Fake <IExtension <States, Events> >();
            var stateContainer = new StateContainer <States, Events>();

            stateContainer.Extensions.Add(extension);

            var stateDefinitionBuilder = new StateDefinitionsBuilder <States, Events>();

            stateDefinitionBuilder
            .In(States.A)
            .On(Events.B)
            .Goto(States.B);
            stateDefinitionBuilder
            .In(States.B)
            .ExecuteOnEntry(() => throw exception);
            var stateDefinitions = stateDefinitionBuilder.Build();

            var testee = new StateMachineBuilder <States, Events>()
                         .WithStateContainer(stateContainer)
                         .Build();

            testee.TransitionExceptionThrown += (s, e) => { };

            testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

            testee.Fire(Events.B, stateContainer, stateContainer, stateDefinitions);

            A.CallTo(() => extension.HandlingEntryActionException(
                         stateContainer,
                         State(States.B),
                         Context(States.A, Events.B),
                         ref exception))
            .MustHaveHappened();

            A.CallTo(() => extension.HandledEntryActionException(
                         stateContainer,
                         State(States.B),
                         Context(States.A, Events.B),
                         exception))
            .MustHaveHappened();
        }
Exemple #10
0
        public void NotifiesAboutTransitions()
        {
            const States Source = States.A;
            const States Target = States.B;
            const Events Event  = Events.B;

            var extension      = A.Fake <IExtension <States, Events> >();
            var stateContainer = new StateContainer <States, Events>();

            stateContainer.Extensions.Add(extension);

            var stateDefinitionBuilder = new StateDefinitionsBuilder <States, Events>();

            stateDefinitionBuilder
            .In(Source)
            .On(Event).Goto(Target);
            var stateDefinitions = stateDefinitionBuilder.Build();

            var testee = new StateMachineBuilder <States, Events>()
                         .WithStateContainer(stateContainer)
                         .Build();

            testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

            testee.Fire(Event, stateContainer, stateContainer, stateDefinitions);

            A.CallTo(() => extension.ExecutingTransition(
                         stateContainer,
                         A <ITransitionDefinition <States, Events> > .That.Matches(
                             t => t.Source.Id == Source && t.Target.Id == Target),
                         A <ITransitionContext <States, Events> > .That.Matches(
                             c => c.EventId.Value == Event && c.StateDefinition.Id == Source)))
            .MustHaveHappened();

            A.CallTo(() => extension.ExecutedTransition(
                         stateContainer,
                         A <ITransitionDefinition <States, Events> > .That.Matches(
                             t => t.Source.Id == Source && t.Target.Id == Target),
                         A <ITransitionContext <States, Events> > .That.Matches(
                             c => c.EventId.Value == Event && c.StateDefinition.Id == Source)))
            .MustHaveHappened();
        }
Exemple #11
0
        public void OverrideEntryActionException()
        {
            var exception           = new Exception();
            var overriddenException = new Exception();

            var extension         = A.Fake <IExtensionInternal <States, Events> >();
            var overrideExtension = new OverrideExtension();
            var stateContainer    = new StateContainer <States, Events>();

            stateContainer.Extensions.Add(extension);
            stateContainer.Extensions.Add(overrideExtension);

            var stateDefinitionBuilder = new StateDefinitionsBuilder <States, Events>();

            stateDefinitionBuilder
            .In(States.A)
            .On(Events.B)
            .Goto(States.B);
            stateDefinitionBuilder
            .In(States.B)
            .ExecuteOnEntry(() => throw exception);
            var stateDefinitions = stateDefinitionBuilder.Build();

            var testee = new StateMachineBuilder <States, Events>()
                         .WithStateContainer(stateContainer)
                         .Build();

            testee.TransitionExceptionThrown += (s, e) => { };

            testee.EnterInitialState(stateContainer, stateDefinitions, States.A);

            overrideExtension.OverriddenException = overriddenException;

            testee.Fire(Events.B, stateContainer, stateContainer, stateDefinitions);

            A.CallTo(() => extension.HandledEntryActionException(
                         A <IStateDefinition <States, Events> > ._,
                         A <ITransitionContext <States, Events> > ._,
                         overriddenException))
            .MustHaveHappened();
        }
Exemple #12
0
        public void Fire()
        {
            var extension      = A.Fake <IExtension <States, Events> >();
            var stateContainer = new StateContainer <States, Events>();

            stateContainer.Extensions.Add(extension);

            var stateDefinitionsBuilder = new StateDefinitionsBuilder <States, Events>();

            stateDefinitionsBuilder
            .In(States.A)
            .On(Events.B)
            .Goto(States.B);
            var stateDefinitions = stateDefinitionsBuilder.Build();

            var testee = new StateMachineBuilder <States, Events>()
                         .WithStateContainer(stateContainer)
                         .Build();

            testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

            var eventId       = Events.B;
            var eventArgument = new object();

            testee.Fire(eventId, eventArgument, stateContainer, stateContainer, stateDefinitions);

            A.CallTo(() => extension.FiringEvent(stateContainer, ref eventId, ref eventArgument))
            .MustHaveHappened();

            A.CallTo(() => extension.FiredEvent(
                         stateContainer,
                         A <ITransitionContext <States, Events> > .That.Matches(
                             context =>
                             context.StateDefinition.Id == States.A &&
                             context.EventId.Value == eventId &&
                             context.EventArgument == eventArgument)))
            .MustHaveHappened();
        }
Exemple #13
0
        public void OverrideFiredEvent()
        {
            var extension         = A.Fake <IExtension <States, Events> >();
            var overrideExtension = new OverrideExtension();
            var stateContainer    = new StateContainer <States, Events>();

            stateContainer.Extensions.Add(extension);
            stateContainer.Extensions.Add(overrideExtension);

            var stateDefinitionsBuilder = new StateDefinitionsBuilder <States, Events>();

            stateDefinitionsBuilder
            .In(States.A)
            .On(Events.B).Goto(States.C)
            .On(Events.C).Goto(States.C);
            var stateDefinitions = stateDefinitionsBuilder.Build();

            var testee = new StateMachineBuilder <States, Events>()
                         .WithStateContainer(stateContainer)
                         .Build();

            testee.EnterInitialState(stateContainer, stateContainer, stateDefinitions, States.A);

            const Events NewEvent         = Events.C;
            var          newEventArgument = new object();

            overrideExtension.OverriddenEvent         = NewEvent;
            overrideExtension.OverriddenEventArgument = newEventArgument;

            testee.Fire(Events.B, stateContainer, stateContainer, stateDefinitions);

            A.CallTo(() => extension.FiredEvent(
                         stateContainer,
                         A <ITransitionContext <States, Events> > .That.Matches(
                             c => c.EventId.Value == NewEvent && c.EventArgument == newEventArgument)))
            .MustHaveHappened();
        }