public void Appropriate_action_is_called_on_transition()
        {
            var wasDoorOpened = false;
            var wasDoorClosed = false;

            var machine = new TinyStateMachine <DoorState, DoorEvents>(
                DoorState.Closed
                );

            machine.Tr(DoorState.Closed, DoorEvents.Open, DoorState.Open)
            .On(() => wasDoorOpened = true)
            .Tr(DoorState.Open, DoorEvents.Close, DoorState.Closed)
            .On(() => wasDoorClosed = true);

            Assert.That(wasDoorOpened, Is.False);
            Assert.That(wasDoorClosed, Is.False);

            machine.Fire(DoorEvents.Open);

            Assert.That(wasDoorOpened, Is.True);
            Assert.That(wasDoorClosed, Is.False);

            machine.Fire(DoorEvents.Close);

            Assert.That(wasDoorOpened, Is.True);
            Assert.That(wasDoorClosed, Is.True);
        }
        public void Guard_is_called_with_the_expected_parameters()
        {
            var machine = new TinyStateMachine <DoorState, DoorEvents>(
                DoorState.Closed
                );

            machine.Tr(DoorState.Closed, DoorEvents.Open, DoorState.Open)
            .Guard((from, trigger, to) =>
            {
                Assert.That(from, Is.EqualTo(DoorState.Closed));
                Assert.That(trigger, Is.EqualTo(DoorEvents.Open));
                Assert.That(to, Is.EqualTo(DoorState.Open));
                return(true);
            })
            .Tr(DoorState.Open, DoorEvents.Close, DoorState.Closed)
            .Guard((from, trigger, to) =>
            {
                Assert.That(from, Is.EqualTo(DoorState.Open));
                Assert.That(trigger, Is.EqualTo(DoorEvents.Close));
                Assert.That(to, Is.EqualTo(DoorState.Closed));
                return(true);
            });

            Assert.That(machine.State, Is.EqualTo(DoorState.Closed));
            machine.Fire(DoorEvents.Open);
            Assert.That(machine.State, Is.EqualTo(DoorState.Open));
            machine.Fire(DoorEvents.Close);
            Assert.That(machine.State, Is.EqualTo(DoorState.Closed));
        }
        private static TinyStateMachine <DoorState, DoorEvents> GetFixture()
        {
            var machine = new TinyStateMachine <DoorState, DoorEvents>(
                DoorState.Closed
                );

            machine.Tr(DoorState.Closed, DoorEvents.Open, DoorState.Open)
            .Tr(DoorState.Open, DoorEvents.Close, DoorState.Closed);
            return(machine);
        }
        public void Guard_can_stop_transition()
        {
            var machine = new TinyStateMachine <DoorState, DoorEvents>(
                DoorState.Closed
                );

            machine.Tr(DoorState.Closed, DoorEvents.Open, DoorState.Open)
            .Guard(() => false)
            .Tr(DoorState.Open, DoorEvents.Close, DoorState.Closed);

            Assert.That(machine.State, Is.EqualTo(DoorState.Closed));
            machine.Fire(DoorEvents.Open);
            Assert.That(machine.State, Is.EqualTo(DoorState.Closed));
        }
        public void Calling_Reset_method_does_not_call_guard_or_trigger_transitions()
        {
            var machine = new TinyStateMachine <DoorState, DoorEvents>(
                DoorState.Closed
                );

            machine.Tr(DoorState.Closed, DoorEvents.Open, DoorState.Open)
            .On(() => Assert.Fail())
            .Guard((from, trigger, to) => false)
            .Tr(DoorState.Open, DoorEvents.Close, DoorState.Closed)
            .On(() => Assert.Fail())
            .Guard((from, trigger, to) => false);

            Assert.That(machine.State, Is.EqualTo(DoorState.Closed));
            machine.Reset(DoorState.Open);
            Assert.That(machine.State, Is.EqualTo(DoorState.Open));
            machine.Reset(DoorState.Closed);
            Assert.That(machine.State, Is.EqualTo(DoorState.Closed));
        }