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

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

            try
            {
                EventBetter.Raise(new TestMessage());
                Assert.AreEqual(10, totalInvocations);
            }
            finally
            {
                handler.Dispose();
            }
        }
Example #2
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 #3
0
        public void TestNestedRegisterSimpleManual()
        {
            int totalInvocations = 0;

            manualHandlers.Add(EventBetter.ListenManual((TestMessage o) =>
            {
                InstanceHandle(null, null);
                ++totalInvocations;

                manualHandlers.Add(EventBetter.ListenManual((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);

            foreach (var handler in manualHandlers)
            {
                handler.Dispose();
            }
        }
Example #4
0
    public void RegisterManual()
    {
        int someValue = 666;

        var disp1 = EventBetter.ListenManual((TestMessage msg) => someValue++);
        var disp2 = EventBetter.ListenManual((TestMessage msg) => someValue++);

        Assert.AreEqual(true, EventBetter.Raise(new TestMessage()));
        Assert.AreEqual(668, someValue);

        disp1.Dispose();
        Assert.AreEqual(true, EventBetter.Raise(new TestMessage()));
        Assert.AreEqual(669, someValue);

        // whether double dispose breaks anything
        disp1.Dispose();
        Assert.AreEqual(true, EventBetter.Raise(new TestMessage()));
        Assert.AreEqual(670, someValue);

        disp2.Dispose();
        Assert.AreEqual(false, EventBetter.Raise(new TestMessage()));
        Assert.AreEqual(670, someValue);
    }