Example #1
0
        public void TestNestedRaiseSimple()
        {
            int nestedCalls      = 0;
            int totalInvocations = 0;

            EventBetter.Listen(this, (TestMessage o) =>
            {
                ++totalInvocations;
                InstanceHandle(null, null);
                try
                {
                    ++nestedCalls;
                    if (nestedCalls < 10)
                    {
                        EventBetter.Raise(new TestMessage());
                    }
                }
                finally
                {
                    --nestedCalls;
                }
            });

            EventBetter.Raise(new TestMessage());
            Assert.AreEqual(10, totalInvocations);
        }
Example #2
0
        public void TestNestedRegisterSimple()
        {
            int totalInvocations = 0;

            EventBetter.Listen(this, (TestMessage o) =>
            {
                InstanceHandle(null, null);
                ++totalInvocations;

                EventBetter.Listen(this, (TestMessage oo) =>
                {
                    InstanceHandle(null, null);
                    ++totalInvocations;
                });
            });

            EventBetter.Raise(new TestMessage());
            Assert.AreEqual(2, totalInvocations);

            EventBetter.Raise(new TestMessage());
            Assert.AreEqual(5, totalInvocations);

            EventBetter.Raise(new TestMessage());
            Assert.AreEqual(9, totalInvocations);
        }
Example #3
0
        public IEnumerator TestCoroutineCapture()
        {
            {
                // if nothing is captured - works
                yield return(null);

                EventBetter.Listen(this, (TestMessage msg) => { });
                Assert.IsTrue(EventBetter.UnlistenAll(this));
            }

            {
                // if nothing is captured - works
                int count = 0;
                yield return(null);

                using (var listener = EventBetter.ListenManual((TestMessage msg) => InstanceHandle(++count, 1)))
                {
                    Assert.IsTrue(EventBetter.Raise(new TestMessage()));
                    Assert.AreEqual(1, count);
                }
                Assert.IsFalse(EventBetter.Raise(new TestMessage()));
                Assert.AreEqual(1, count);
            }

            {
                // if local coroutine variables captured - won't
                int count = 0;
                yield return(null);

                EventBetter.Listen(this, (TestMessage msg) => InstanceHandle(++count, 1), once: true);
                Assert.IsTrue(EventBetter.Raise(new TestMessage()));
                Assert.AreEqual(1, count);
            }
        }
Example #4
0
        public void TestWorker()
        {
            var worker = GameObject.Find("EventBetterWorker");

            Assert.IsFalse(worker);

            EventBetter.Listen(this, (TestMessage o) => { });
            worker = GameObject.Find("EventBetterWorker");
            Assert.IsTrue(worker);
            Assert.AreEqual(worker, EventBetter.Test_Worker?.gameObject);

            worker.SetActive(false);
            Assert.Throws <InvalidOperationException>(() => EventBetter.Listen(this, (TestMessage o) => { }));
            worker.SetActive(true);
            EventBetter.Listen(this, (TestMessage o) => { });

            worker.GetComponent <MonoBehaviour>().enabled = false;
            Assert.Throws <InvalidOperationException>(() => EventBetter.Listen(this, (TestMessage o) => { }));
            worker.GetComponent <MonoBehaviour>().enabled = true;
            EventBetter.Listen(this, (TestMessage o) => { });

            DestroyImmediate(worker);
            EventBetter.Listen(this, (TestMessage o) => { });
            worker = GameObject.Find("EventBetterWorker");
            Assert.IsTrue(worker);
            Assert.AreEqual(worker, EventBetter.Test_Worker?.gameObject);
        }
Example #5
0
 public void TestOnce()
 {
     {
         int count = 0;
         EventBetter.Listen(this, (TestMessage o) => ++ count);
         Assert.IsTrue(EventBetter.Raise(new TestMessage()));
         Assert.IsTrue(EventBetter.Raise(new TestMessage()));
         Assert.AreEqual(2, count);
         Assert.IsTrue(EventBetter.UnlistenAll(this));
     }
     {
         int count = 0;
         EventBetter.Listen(this, (TestMessage o) => ++ count, once: true);
         Assert.IsTrue(EventBetter.Raise(new TestMessage()));
         Assert.IsFalse(EventBetter.Raise(new TestMessage()));
         Assert.AreEqual(1, count);
         Assert.IsFalse(EventBetter.UnlistenAll(this));
     }
     {
         int count = 0;
         EventBetter.Listen(this, (TestMessage o) => ++ count, once: true);
         EventBetter.Listen(this, (TestMessage o) => ++ count, once: true);
         EventBetter.Listen(this, (TestMessage o) => ++ count, once: true);
         Assert.IsTrue(EventBetter.Raise(new TestMessage()));
         Assert.AreEqual(3, count);
         Assert.IsFalse(EventBetter.Raise(new TestMessage()));
         Assert.IsFalse(EventBetter.UnlistenAll(this));
     }
 }
Example #6
0
        public void TestUnregister()
        {
            Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Raise(new TestMessage()));

            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
            Assert.AreEqual(true, EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(true, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Raise(new TestMessage()));

            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
            Assert.AreEqual(true, EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(true, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Raise(new TestMessage()));

            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
            Assert.AreEqual(true, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this));
            Assert.AreEqual(false, EventBetter.Raise(new TestMessage()));
        }
Example #7
0
 public void NestedDifferentContext(int baseValue)
 {
     EventBetter.Listen(this, (TestMessage o) =>
     {
         InstanceHandle(null, null);
         ++nestedInvocations;
     });
 }
Example #8
0
        public void TestSelfAdv()
        {
            var date = System.DateTime.Now.Date;
            var str  = "TestString";

            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(date, System.DateTime.Now.Date));
            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(str, "TestString"));
        }
Example #9
0
        public void TestNestedMessedUp()
        {
            int nestedCalls      = 0;
            int totalInvocations = 0;

            int dummyMessage2Invocations = 0;

            EventBetter.Listen(this, (TestMessage2 o) =>
            {
                InstanceHandle(null, null);
                ++dummyMessage2Invocations;

                EventBetter.Unlisten <TestMessage>(this);
            });

            int dummyMessage3Invocations = 0;

            EventBetter.Listen(this, (TestMessage3 o) =>
            {
                InstanceHandle(null, null);
                ++dummyMessage3Invocations;

                EventBetter.Unlisten <TestMessage3>(this);
                EventBetter.Raise(new TestMessage());
            });

            EventBetter.Listen(this, (TestMessage o) =>
            {
                InstanceHandle(null, null);
                ++totalInvocations;
                ++nestedCalls;
                if (nestedCalls >= 10)
                {
                    EventBetter.Raise(new TestMessage2());
                    EventBetter.Raise(new TestMessage3());

                    EventBetter.Raise(new TestMessage2());
                    EventBetter.Raise(new TestMessage3());

                    EventBetter.Raise(new TestMessage2());
                    EventBetter.Raise(new TestMessage3());
                }

                EventBetter.Raise(new TestMessage());
            });

            EventBetter.Raise(new TestMessage());
            Assert.AreEqual(10, totalInvocations);
            Assert.AreEqual(3, dummyMessage2Invocations);
            Assert.AreEqual(1, dummyMessage3Invocations);
        }
Example #10
0
        public void TestStructStatic()
        {
            var someStruct = new SomeStruct()
            {
                Value = 123
            };

            EventBetter.Listen(this, (TestMessage o) => StaticHandle(null, null));
            EventBetter.Listen(this, (TestMessage o) => StaticHandle(someStruct, new SomeStruct()
            {
                Value = 123
            }));
            EventBetter.Listen(this, (TestMessage o) => StaticHandle(someStruct.Value, 123));
        }
Example #11
0
        public void TestClassStatic()
        {
            var someClass = new SomeClass()
            {
                Value = 456
            };

            EventBetter.Listen(this, (TestMessage o) => StaticHandle(null, null));
            EventBetter.Listen(this, (TestMessage o) => StaticHandle(someClass, new SomeClass()
            {
                Value = 456
            }));
            EventBetter.Listen(this, (TestMessage o) => StaticHandle(someClass.Value, 456));
        }
Example #12
0
        public void TestSomeOtherHost()
        {
            {
                var go = new GameObject("blah");
                go.transform.SetParent(transform);
                EventBetter.Listen(go, (TestMessage o) => StaticHandle(go.name, "blah"));
            }

            {
                var go = new GameObject("blah2");
                go.transform.SetParent(transform);
                EventBetter.Listen(go, (TestMessage o) => InstanceHandle(go.name, "blah2"));
            }
        }
Example #13
0
        public void TestClass()
        {
            var someClass = new SomeClass()
            {
                Value = 456
            };

            // this captured implicitly
            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(someClass, new SomeClass()
            {
                Value = 456
            }));
            EventBetter.Listen(this, (TestMessage o) => InstanceHandle(someClass.Value, 456));
        }
Example #14
0
        public void TestIfActiveAndEnabled()
        {
            int count = 0;

            EventBetter.Listen(this, (TestMessage o) => ++ count);
            Assert.IsTrue(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(1, count);

            enabled = false;
            Assert.IsTrue(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(2, count);
            enabled = true;

            gameObject.SetActive(false);
            Assert.IsTrue(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(3, count);
            gameObject.SetActive(true);

            Assert.IsTrue(EventBetter.Unlisten <TestMessage>(this));
            Assert.IsFalse(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(3, count);

            EventBetter.Listen(this, (TestMessage o) => ++ count, exculdeInactive: true);
            Assert.IsTrue(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(4, count);

            enabled = false;
            Assert.IsFalse(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(4, count);
            enabled = true;

            Assert.IsTrue(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(5, count);

            gameObject.SetActive(false);
            Assert.IsFalse(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(5, count);
            gameObject.SetActive(true);

            Assert.IsTrue(EventBetter.Raise(new TestMessage()));
            Assert.AreEqual(6, count);
        }
Example #15
0
        public void TestMutableLambda()
        {
            {
                int startValue = 100;
                EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 100));
                EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 101));
                EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 102));
                EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 103));
                EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 104));
            }
            {
                int startValue = 200;
                EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue, 210));
                startValue += 10;
                EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue, 210));
            }

            Assert.IsTrue(EventBetter.Raise(new TestMessage()));
            Assert.IsTrue(EventBetter.UnlistenAll(this));
        }
Example #16
0
        public void TestNestedRaiseContexts()
        {
            int totalInvocations = 0;

            nestedInvocations = 0;

            EventBetter.Listen(this, (TestMessage o) =>
            {
                InstanceHandle(null, null);
                ++totalInvocations;
                NestedDifferentContext(totalInvocations);
            });

            EventBetter.Raise(new TestMessage());
            Assert.AreEqual(2, totalInvocations + nestedInvocations);

            EventBetter.Raise(new TestMessage());
            Assert.AreEqual(5, totalInvocations + nestedInvocations);

            EventBetter.Raise(new TestMessage());
            Assert.AreEqual(9, totalInvocations + nestedInvocations);
        }
Example #17
0
 private void Awake()
 {
     EventBetter.Listen(this, (CollisionMessage msg) => LogCollision(msg.reporter, msg.collision));
 }
Example #18
0
 private void Awake()
 {
     EventBetter.Listen(this, (TextMessage msg) => ++ count);
 }
Example #19
0
 public void TestSelf()
 {
     EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null));
 }
Example #20
0
 public void TestSelfStatic()
 {
     EventBetter.Listen(this, (TestMessage o) => StaticHandle(null, null));
 }
 private void Update()
 {
     EventBetter.Listen(this, handler);
     EventBetter.UnlistenAll(this);
 }
 void Awake()
 {
     EventBetter.Listen <SimpleConsumerNoLambda, TextMessage>(this, HandlePrintMessage, once: true);
 }
Example #23
0
 void Awake()
 {
     EventBetter.Listen(this, (TextMessage msg) => Debug.Log(msg.text, this));
 }