Пример #1
0
        public void EventでUnsubscribe()
        {
            var subscriber = new Subscriber();

            int callNum1 = 0;
            int callNum2 = 0;
            int callNum3 = 0;

            subscriber.Subscribe("topic1", () => { callNum1++; });
            var e = subscriber.Subscribe("topic1", () => { callNum2++; });

            subscriber.Subscribe("topic2", () => { callNum3++; });

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            subscriber.Call("topic1");
            subscriber.Call("topic2");

            subscriber.Unsubscribe(e);

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            subscriber.Call("topic1");
            subscriber.Call("topic2");
            Assert.AreEqual(callNum1, 4);
            Assert.AreEqual(callNum2, 2);
            Assert.AreEqual(callNum3, 4);
        }
Пример #2
0
        public void EventでUnsubscribe()
        {
            var subscriber = new Subscriber();

            int callNum1 = 0;
            int callNum2 = 0;
            int callNum3 = 0;
            subscriber.Subscribe("topic1", () => { callNum1++; });
            var e = subscriber.Subscribe("topic1", () => { callNum2++; });
            subscriber.Subscribe("topic2", () => { callNum3++; });

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            subscriber.Call("topic1");
            subscriber.Call("topic2");

            subscriber.Unsubscribe(e);

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            subscriber.Call("topic1");
            subscriber.Call("topic2");
            Assert.AreEqual(callNum1, 4);
            Assert.AreEqual(callNum2, 2);
            Assert.AreEqual(callNum3, 4);
        }
Пример #3
0
        public void Mute()
        {
            var subscriber = new Subscriber();

            int callNum1 = 0;
            subscriber.Subscribe("topic1", () => { callNum1++; });

            subscriber.Call("topic1");
            Assert.AreEqual(callNum1, 1);

            subscriber.Mute("topic1");

            subscriber.Call("topic1");
            Assert.AreEqual(callNum1, 1);
        }
Пример #4
0
        public void 引数の型を間違うとエラーになる()
        {
            var subscriber = new Subscriber();

            subscriber.SubscribeWithTopic <string>("topic1", (topic, arg) => {});
            subscriber.Call("topic1", 123);
        }
Пример #5
0
        public void 色んな引数でSubscribeしてみる()
        {
            var subscriber = new Subscriber();

            bool noArgs = false;

            subscriber.Subscribe("topic1", () => { noArgs = true; });

            bool topicOnly = false;

            subscriber.SubscribeWithTopic("topic1", (topic) => { topicOnly = true; });

            bool args1 = false;

            subscriber.SubscribeWithTopic <string>("topic1", (topic, arg) => { args1 = true; });

            bool args2 = false;

            subscriber.SubscribeWithTopic <string, string>("topic1", (topic, arg1, arg2) => { args2 = true; });

            var num = subscriber.Call("topic1", "ami", "mami");

            Assert.IsTrue(noArgs);
            Assert.IsTrue(topicOnly);
            Assert.IsTrue(args1);
            Assert.IsTrue(args2);
            Assert.AreEqual(num, 4);
        }
Пример #6
0
        public void Mute()
        {
            var subscriber = new Subscriber();

            int callNum1 = 0;

            subscriber.Subscribe("topic1", () => { callNum1++; });

            subscriber.Call("topic1");
            Assert.AreEqual(callNum1, 1);

            subscriber.Mute("topic1");

            subscriber.Call("topic1");
            Assert.AreEqual(callNum1, 1);
        }
Пример #7
0
        public void 複数登録したうち1つだけ解除しても動く()
        {
            var subscriber = new Subscriber();

            int callNum1 = 0;

            subscriber.Subscribe(new string[] { "topic1", "topic2" }, () => { callNum1++; });

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            Assert.AreEqual(callNum1, 2);

            subscriber.Unsubscribe("topic1");

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            Assert.AreEqual(callNum1, 3);
        }
Пример #8
0
        public void Call()
        {
            var subscriber = new Subscriber();

            int callNum1 = 0;
            int callNum2 = 0;
            int callNum3 = 0;
            subscriber.Subscribe("topic1", () => { callNum1++; });
            subscriber.Subscribe("topic1", () => { callNum2++; });
            subscriber.Subscribe("topic2", () => { callNum3++; });

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            subscriber.Call("topic1");
            subscriber.Call("topic2");
            Assert.AreEqual(callNum1, 2);
            Assert.AreEqual(callNum2, 2);
            Assert.AreEqual(callNum3, 2);
        }
Пример #9
0
        public void Call()
        {
            var subscriber = new Subscriber();

            int callNum1 = 0;
            int callNum2 = 0;
            int callNum3 = 0;

            subscriber.Subscribe("topic1", () => { callNum1++; });
            subscriber.Subscribe("topic1", () => { callNum2++; });
            subscriber.Subscribe("topic2", () => { callNum3++; });

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            subscriber.Call("topic1");
            subscriber.Call("topic2");
            Assert.AreEqual(callNum1, 2);
            Assert.AreEqual(callNum2, 2);
            Assert.AreEqual(callNum3, 2);
        }
Пример #10
0
        public void 合ってるTopicだけ呼び出される()
        {
            var subscriber = new Subscriber();

            bool topic1 = false;

            subscriber.Subscribe("topic1", () => { topic1 = true; });

            bool topic2 = false;

            subscriber.Subscribe("topic2", () => { topic2 = true; });

            bool topic3 = false;

            subscriber.Subscribe("topic3", () => { topic3 = true; });

            var num = subscriber.Call("topic2");

            Assert.IsFalse(topic1);
            Assert.IsTrue(topic2);
            Assert.IsFalse(topic3);
            Assert.AreEqual(num, 1);
        }
Пример #11
0
        public void 複数登録したうち1つだけ解除しても動く()
        {
            var subscriber = new Subscriber();

            int callNum1 = 0;
            subscriber.Subscribe(new string[]{"topic1", "topic2"}, () => { callNum1++; });

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            Assert.AreEqual(callNum1, 2);

            subscriber.Unsubscribe("topic1");

            subscriber.Call("topic1");
            subscriber.Call("topic2");
            Assert.AreEqual(callNum1, 3);
        }
Пример #12
0
        public void 色んな引数でSubscribeしてみる()
        {
            var subscriber = new Subscriber();

            bool noArgs = false;
            subscriber.Subscribe("topic1", () => { noArgs = true; });

            bool topicOnly = false;
            subscriber.SubscribeWithTopic("topic1", (topic) => { topicOnly = true; });

            bool args1 = false;
            subscriber.SubscribeWithTopic<string>("topic1", (topic, arg) => { args1 = true; });

            bool args2 = false;
            subscriber.SubscribeWithTopic<string, string>("topic1", (topic, arg1, arg2) => { args2 = true; });

            var num = subscriber.Call("topic1", "ami", "mami");

            Assert.IsTrue(noArgs);
            Assert.IsTrue(topicOnly);
            Assert.IsTrue(args1);
            Assert.IsTrue(args2);
            Assert.AreEqual(num, 4);
        }
Пример #13
0
 public void 引数の型を間違うとエラーになる()
 {
     var subscriber = new Subscriber();
     subscriber.SubscribeWithTopic<string>("topic1", (topic, arg) => {});
     subscriber.Call("topic1", 123);
 }
Пример #14
0
        public void 合ってるTopicだけ呼び出される()
        {
            var subscriber = new Subscriber();

            bool topic1 = false;
            subscriber.Subscribe("topic1", () => { topic1 = true; });

            bool topic2 = false;
            subscriber.Subscribe("topic2", () => { topic2 = true; });

            bool topic3 = false;
            subscriber.Subscribe("topic3", () => { topic3 = true; });

            var num = subscriber.Call("topic2");

            Assert.IsFalse(topic1);
            Assert.IsTrue(topic2);
            Assert.IsFalse(topic3);
            Assert.AreEqual(num, 1);
        }