Esempio n. 1
0
    public void RunsEnterInitExitFunctionsInProperOrder()
    {
        BehaviorMap   behaviors = new BehaviorMap();
        List <string> list      = new List <string>();
        Callback      callback  = (stateName) => {
            list.Add(stateName);
        };

        MakeCallbacks(
            behaviors,
            new[] { "Root", "opened", "closed", "locked", "unlocked" },
            new[] { "init", "enter", "exit" },
            callback
            );

        StateChart chart = GetChart(behaviors);

        Assert.That(list, Is.EquivalentTo(new[] {
            "Root:init", "Root:enter",
            "closed:init", "closed:enter",
            "locked:init", "locked:enter"
        }));
        list.Clear();
        chart.Trigger(new Evt_DoorUnlock());
        chart.Tick();
        Assert.That(list, Is.EquivalentTo(new[] {
            "locked:exit", "unlocked:init", "unlocked:enter"
        }));
        list.Clear();
        chart.Trigger(new Evt_DoorLock());
        chart.Tick();
        Assert.That(list, Is.EquivalentTo(new[] {
            "unlocked:exit", "locked:enter"
        }));
    }
Esempio n. 2
0
    public void TransitionsToAnotherState()
    {
        StateChart chart = new StateChart((builder) => {
            Action <string, Action> State = builder.State;

            State("State-1", () => {
                builder.Transition <SomeEvt>("State-1-2");

                State("State-1-1", () => {
                    State("State-1-1-1", () => { });
                    State("State-1-1-2", () => { });
                });

                State("State-1-2", () => { });
            });
        });

        chart.Trigger(new SomeEvt());
        chart.Tick();
        Assert.IsTrue(chart.IsInState("State-1"));
        Assert.IsFalse(chart.IsInState("State-1-1"));
        Assert.IsFalse(chart.IsInState("State-1-1-1"));
        Assert.IsFalse(chart.IsInState("State-1-1-2"));
        Assert.IsTrue(chart.IsInState("State-1-2"));
    }
Esempio n. 3
0
    public void ProperlyQueuesEvents()
    {
        StateChart chart = GetChart2();

        Assert.IsTrue(chart.IsInState("closed"));
        chart.Tick();
        Assert.IsTrue(chart.IsInState("opened"));
    }
Esempio n. 4
0
    public void MovesFromClosedAndUnlockedToOpenedAndBackToClosedLocked()
    {
        StateChart chart = GetChart();

        chart.Trigger(new Evt_DoorUnlock());
        chart.Trigger(new Evt_DoorOpen());
        chart.Tick();
        Assert.IsTrue(chart.IsInState("opened"));
        Assert.IsFalse(chart.IsInState("closed"));
        Assert.IsFalse(chart.IsInState("locked"));
        Assert.IsFalse(chart.IsInState("unlocked"));
        chart.Trigger(new Evt_DoorClose());
        chart.Tick();
        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsFalse(chart.IsInState("locked"));
        Assert.IsTrue(chart.IsInState("unlocked"));
    }
Esempio n. 5
0
    public void CallsEventHandlers()
    {
        List <string> list = new List <string>();
        Action <string, StateChartEvent> callback = (key, evt) => {
            list.Add(key);
        };

        EvtPair[] pairs =
        {
            new EvtPair("Root",     typeof(Evt_DoorUnlock), callback),
            new EvtPair("closed",   typeof(Evt_DoorUnlock), callback),
            new EvtPair("locked",   typeof(Evt_DoorUnlock), callback),
            new EvtPair("unlocked", typeof(Evt_DoorUnlock), callback),
            new EvtPair("opened",   typeof(Evt_DoorUnlock), callback),
            new EvtPair("Root",     typeof(Evt_DoorLock),   callback),
            new EvtPair("closed",   typeof(Evt_DoorLock),   callback),
            new EvtPair("locked",   typeof(Evt_DoorLock),   callback),
            new EvtPair("unlocked", typeof(Evt_DoorLock),   callback)
        };

        StateChart chart = GetChart(null, pairs);

        chart.Trigger(new Evt_DoorUnlock());
        chart.Tick();
        Assert.That(list, Is.EquivalentTo(new[] {
            "Root:Evt_DoorUnlock",
            "closed:Evt_DoorUnlock",
            "locked:Evt_DoorUnlock"
        }));
        list.Clear();
        chart.Trigger(new Evt_DoorUnlock());
        chart.Trigger(new Evt_DoorLock());
        chart.Tick();
        Assert.That(list, Is.EquivalentTo(new[] {
            "Root:Evt_DoorUnlock",
            "closed:Evt_DoorUnlock",
            "unlocked:Evt_DoorUnlock",
            "Root:Evt_DoorLock",
            "closed:Evt_DoorLock",
            "unlocked:Evt_DoorLock"
        }));
    }
Esempio n. 6
0
    public void DoesNotTransitionIfGuardFnIsFalse()
    {
        StateChart chart = GetChart();

        chart.Trigger(new Evt_DoorUnlock(false));
        chart.Tick();
        Assert.IsFalse(chart.IsInState("unlocked"));
        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsTrue(chart.IsInState("locked"));
    }
Esempio n. 7
0
    public void DoesNotTransitionIfNoTransitionHandlerExistsForCongiuration()
    {
        StateChart chart = GetChart();

        chart.Trigger(new Evt_DoorOpen());
        chart.Tick();
        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsTrue(chart.IsInState("locked"));
        Assert.IsFalse(chart.IsInState("unlocked"));
    }
Esempio n. 8
0
    public void MovesToUnlockedFromClosedWithUnlockEvent()
    {
        StateChart chart = GetChart();

        chart.Trigger(new Evt_DoorUnlock());
        chart.Tick();
        Assert.IsTrue(chart.IsInState("unlocked"));
        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsFalse(chart.IsInState("locked"));
    }
Esempio n. 9
0
 public void Tick()
 {
     stateChart.Tick();
 }