Esempio n. 1
0
        public static IEnumerator Test1()
        {
            yield return(null);

            int[] c = { 0 };
            MessageBus.GetBus <int>().Subscribe((i) => { c[0] += i; });

            MessageBus.GetBus <int>().Send(1);
            Assert.AreEqual(c[0], 1, "Send does not send properly.");

            c[0] = 3;
            MessageBus.GetBus <int>().SendBuffered(2);
            Assert.AreEqual(c[0], 5, "Send Buffered does not send properly.");

            IBus <int> busCache = MessageBus.GetBus <int>();

            c[0] = 0;
            busCache.Subscribe((i) => { c[0] += i; });
            Assert.AreEqual(c[0], 2, "Send Buffered does not send to future subscribers properly.");

            var go = new GameObject("test");

            MessageBus.GetBus <int>(go).Send(8);
            Assert.AreEqual(c[0], 2, "Local busses leak into Global Bus.");

            int[] d = { 0 };
            MessageBus.GetBus <int>(go).SubscribeOnce((i) => { d[0] += i; });
            MessageBus.GetBus <int>(go).Send(1);
            MessageBus.GetBus <int>(go).Send(1);
            Assert.AreEqual(d[0], 1, "Subscribe Once should only receive one message before Releasing itself.");

            yield return(null);

            UnityEngine.Object.Destroy(go);
            yield return(null);

            d[0] = 0;

            MessageBus.GetBus <int>(go).Subscribe((i) => { d[0] += i; });
            MessageBus.GetBus <int>(go).Send(1);

            Assert.AreEqual(go, null, "Deleted gameobject should be null.");
            Assert.AreEqual(d[0], 0, "Getting the bus of a null object should return a dummy bus.");

            busCache.SendBuffered(76);
            busCache.ClearMessageBuffer();

            c[0] = 0;

            busCache.Subscribe((i) => { c[0] += i; });
            Assert.AreEqual(c[0], 0, "Clear Message Buffer didn't clear.");

            busCache.ReleaseAllSubscribers();
            busCache.Send(9);
            Assert.AreEqual(c[0], 0, "Release all subscriber Buffer didn't clear.");
        }