Exemple #1
0
        public void Entering_TheSameNode_MultipleTimesWithCascade_Works()
        {
            Graph graph = new Graph();

            // A -> B <-> C
            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();
            Node nodeC = graph.MakeNode();

            Transition     abTransition     = new Transition(waitForManualExit: false);
            NodeTransition abNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = abTransition
            };

            graph.AddOutgoingTransitionForNode(nodeA, abNodeTransition);

            Transition bcTransition = new Transition(waitForManualExit: false);

            bcTransition.AddTransitionCondition(new TriggerTransitionCondition("BGoToC"));
            NodeTransition bcNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeC.Id }, transition = bcTransition
            };

            graph.AddOutgoingTransitionForNode(nodeB, bcNodeTransition);

            Transition cbTransition = new Transition(waitForManualExit: false);

            cbTransition.AddTransitionCondition(new TriggerTransitionCondition("CGoToB"));
            NodeTransition cbNodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = cbTransition
            };

            graph.AddOutgoingTransitionForNode(nodeC, cbNodeTransition);

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasTriggerParameterKey(Arg.Is("BGoToC")).Returns(true);
            stubContext.HasTriggerParameterKey(Arg.Is("CGoToB")).Returns(true);
            // returns true then false
            stubContext.HasTrigger(Arg.Is("BGoToC")).Returns(true, false);
            // returns true then false
            stubContext.HasTrigger(Arg.Is("CGoToB")).Returns(true, false);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            int bEnteredCount = 0;

            nodeB.OnEnter += () => { bEnteredCount++; };
            int cEnteredCount = 0;

            nodeC.OnEnter += () => { cEnteredCount++; };

            graph.Start();

            Assert.AreEqual(2, bEnteredCount);
            Assert.AreEqual(1, cEnteredCount);
        }
        public void TriggerTransitionCondition_NotTaken_WhenMissingStartingContextParameter(string key)
        {
            Graph graph = new Graph();

            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();

            Transition transition = new Transition(waitForManualExit: false);

            transition.AddTransitionCondition(new TriggerTransitionCondition(key));
            NodeTransition nodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = transition
            };

            graph.AddOutgoingTransitionForNode(nodeA, nodeTransition);

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasTriggerParameterKey(Arg.Any <string>()).Returns(false);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool entered = false;

            nodeB.OnEnter += () => { entered = true; };

            graph.Start();
            Assert.IsFalse(entered);
        }
        public void TriggerTransitionCondition_TakenWhenMatching_NotTakenWhenNotMatching(string key, bool expectedEntered)
        {
            Graph graph = new Graph();

            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();

            Transition transition = new Transition(waitForManualExit: false);

            transition.AddTransitionCondition(new TriggerTransitionCondition(key));
            NodeTransition nodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = transition
            };

            graph.AddOutgoingTransitionForNode(nodeA, nodeTransition);

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasTriggerParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.HasTrigger(Arg.Is("Key")).Returns(true);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool entered = false;

            nodeB.OnEnter += () => { entered = true; };

            graph.Start();
            Assert.AreEqual(expectedEntered, entered);
        }
Exemple #4
0
        public void TriggerTransitionCondition_MatchesOtherTests_AfterSerializingAndDeserializing(string key, bool expectedEntered)
        {
            Graph graph = new Graph();

            Node nodeA = graph.MakeNode();
            Node nodeB = graph.MakeNode();

            Transition transition = new Transition(waitForManualExit: false);
            TriggerTransitionCondition condition = new TriggerTransitionCondition(key);
            // NEW CODE
            string serialized = TransitionConditionSerializer.Serialize(condition);
            ITransitionCondition deserialized = TransitionConditionSerializer.Deserialize(serialized);

            transition.AddTransitionCondition(deserialized);
            // END NEW CODE

            NodeTransition nodeTransition = new NodeTransition {
                targets = new NodeId[] { nodeB.Id }, transition = transition
            };

            graph.AddOutgoingTransitionForNode(nodeA, nodeTransition);

            IGraphContext stubContext = Substitute.For <IGraphContext>();

            stubContext.HasTriggerParameterKey(Arg.Is("Key")).Returns(true);
            stubContext.HasTrigger(Arg.Is("Key")).Returns(true);

            IGraphContextFactory stubFactory = Substitute.For <IGraphContextFactory>();

            stubFactory.MakeContext().Returns(stubContext);
            GraphContextFactoryLocator.Provide(stubFactory);

            bool entered = false;

            nodeB.OnEnter += () => { entered = true; };

            graph.Start();
            Assert.AreEqual(expectedEntered, entered);
        }