public void GetDataTest()
        {
            StateMachine stateMachine = ForTest.stateMachine;
            string       dataName     = "Data1";
            Data         data1;
            bool         result;

            Assert.IsFalse(stateMachine.DataExists(dataName));

            //get data
            data1 = stateMachine.AddData(dataName);
            data1 = stateMachine.GetData(dataName);
            Assert.IsNotNull(data1);
            Assert.IsTrue(stateMachine.DataExists(dataName));
            stateMachine.DeleteData(dataName);
            Assert.ThrowsException <KeyNotFoundException>(() =>
            {
                data1 = stateMachine.GetData(dataName);
            });

            //try get data
            data1 = stateMachine.AddData(dataName);
            data1 = stateMachine.TryGetData(dataName, out result);
            Assert.IsNotNull(data1);
            Assert.IsTrue(result);
            Assert.IsTrue(stateMachine.DataExists(dataName));
            stateMachine.DeleteData(dataName);
            data1 = stateMachine.TryGetData(dataName, out result);
            Assert.IsNull(data1);
            Assert.IsFalse(result);
        }
        public void StateMachineFromCode()
        {
            var          loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole().AddDebug().SetMinimumLevel(LogLevel.Debug); });
            var          logger        = loggerFactory.CreateLogger <StateMachine>();
            StateMachine stateMachine  = new StateMachine(logger);
            State        state1        = stateMachine.AddState("State1", actionOnExit: Method1);
            State        state2        = stateMachine.AddState("State2");
            State        state3        = stateMachine.AddState("State3");
            State        state4        = stateMachine.AddState("State4");

            Assert.IsTrue(stateMachine.StateExists("State1"));

            stateMachine.OnChangeState(ActionOnChangeState);

            Transition transition1 = state1.AddTransitionFromThis("Transition1", state2);
            Transition transition2 = stateMachine.AddTransition("Transition2", state2, state3);
            Transition transition3 = state4.AddTransitionToThis("Transition3", state3);

            Assert.IsTrue(stateMachine.TransitionExists("Transition1"));

            state1.SetAsStartState();
            state2.OnExit(Method2);
            state3.OnExit(Method3);
            state4.OnExit(Method4);
            stateMachine.AddData("int1", 55);
            stateMachine.AddData("string1", "Roman");
            stateMachine.AddData("double1", 1001.0005);

            Assert.IsTrue(stateMachine.DataExists("int1"));
            stateMachine.Start(parametersForStart);

            Assert.AreEqual(stateMachine.CurrentState.Name, "State4");

            stateMachine.ToXDocument("text.xml");
        }
        public void ChangeDataTest()
        {
            StateMachine stateMachine = ForTest.stateMachine;
            int          eventCount   = 0;
            string       dataName     = "Data1";
            int          newDataValue = 5;
            Data         data1;

            eventCount  = 0;
            data1       = stateMachine.AddData(dataName);
            data1.Value = 5;
            Assert.AreEqual(eventCount, 0);
            stateMachine.DeleteData(dataName);

            eventCount = 0;
            data1      = stateMachine.AddData(dataName, 10, MethodOnChange);
            Assert.AreEqual(eventCount, 0);
            stateMachine.DeleteData(dataName);

            eventCount = 0;
            data1      = stateMachine.AddData(dataName, 10).OnChange(MethodOnChange);
            Assert.AreEqual(eventCount, 0);
            stateMachine.DeleteData(dataName);

            eventCount = 0;
            data1      = stateMachine.AddData(dataName);
            data1.OnChange(MethodOnChange);
            data1.Value = 5;
            Assert.AreEqual(eventCount, 1);
            stateMachine.DeleteData(dataName);

            eventCount  = 0;
            data1       = stateMachine.AddData(dataName, actionOnChange: MethodOnChange);
            data1.Value = 5;
            Assert.AreEqual(eventCount, 1);
            stateMachine.DeleteData(dataName);

            eventCount  = 0;
            data1       = stateMachine.AddData(dataName).OnChange(MethodOnChange);
            data1.Value = 5;
            Assert.AreEqual(eventCount, 1);
            stateMachine.DeleteData(dataName);

            eventCount = 0;
            data1      = stateMachine.AddData(dataName, actionOnChange: MethodOnChange).OnChange(MethodOnChange).OnChange(MethodOnChange);
            data1.OnChange(MethodOnChange);
            data1.OnChange(MethodOnChange);
            data1.Value = 5;
            Assert.AreEqual(eventCount, 5);
            stateMachine.DeleteData(dataName);

            void MethodOnChange(Data data, object newValue)
            {
                Assert.AreEqual(data.Name, dataName);
                Assert.AreEqual(newValue, newDataValue);
                eventCount++;
            }
        }
        public void AddDataTest()
        {
            StateMachine stateMachine = ForTest.stateMachine;
            string       dataName     = "Data1";

            Assert.IsFalse(stateMachine.DataExists(dataName));

            Data data1 = stateMachine.AddData(dataName);

            Assert.IsNotNull(data1);

            Assert.IsTrue(stateMachine.DataExists(dataName));

            Assert.ThrowsException <ArgumentException>(() =>
            {
                data1 = stateMachine.AddData(dataName);
            });

            data1 = stateMachine.TryAddData(out bool result, dataName);
            Assert.IsNull(data1);
            Assert.IsFalse(result);

            stateMachine.DeleteData(dataName);
        }
        public void DeleteDataTest()
        {
            StateMachine stateMachine = ForTest.stateMachine;
            string       dataName     = "Data1";
            Data         data1;
            bool         result;

            //delete data by name
            data1 = stateMachine.AddData(dataName);
            data1 = stateMachine.DeleteData(dataName);
            Assert.IsNotNull(data1);
            Assert.IsFalse(stateMachine.DataExists(dataName));
            Assert.ThrowsException <KeyNotFoundException>(() =>
            {
                data1 = stateMachine.DeleteData(dataName);
            });

            //delete data by object
            data1 = stateMachine.AddData(dataName);
            data1 = stateMachine.DeleteData(data1);
            Assert.IsNotNull(data1);
            Assert.IsFalse(stateMachine.DataExists(dataName));
            Assert.ThrowsException <KeyNotFoundException>(() =>
            {
                data1 = stateMachine.DeleteData(data1);
            });

            //try delete data by name
            data1 = stateMachine.AddData(dataName);
            data1 = stateMachine.TryDeleteData(dataName, out result);
            Assert.IsNotNull(data1);
            Assert.IsTrue(result);
            data1 = stateMachine.TryDeleteData(dataName, out result);
            Assert.IsNull(data1);
            Assert.IsFalse(result);

            //try delete data by object
            data1 = stateMachine.AddData(dataName);
            data1 = stateMachine.TryDeleteData(data1, out result);
            Assert.IsNotNull(data1);
            Assert.IsTrue(result);
            data1 = stateMachine.TryDeleteData(data1, out result);
            Assert.IsNull(data1);
            Assert.IsFalse(result);

            //delete data from object
            data1 = stateMachine.AddData(dataName);
            data1 = data1.Delete();
            Assert.IsNotNull(data1);
            Assert.IsFalse(stateMachine.DataExists(dataName));
            Assert.ThrowsException <KeyNotFoundException>(() =>
            {
                data1 = data1.Delete();
            });

            //try delete data from object
            data1 = stateMachine.AddData(dataName);
            data1 = data1.TryDelete(out result);
            Assert.IsNotNull(data1);
            Assert.IsTrue(result);
            Assert.IsFalse(stateMachine.DataExists(dataName));
            data1 = data1.TryDelete(out result);
            Assert.IsNull(data1);
            Assert.IsFalse(result);
        }