|
| 1 | +#include <gmock/gmock.h> |
| 2 | +#include <gtest/gtest.h> |
| 3 | +#include <tinyevents/tinyevents.hpp> |
| 4 | + |
| 5 | +using namespace tinyevents; |
| 6 | +using namespace testing; |
| 7 | + |
| 8 | +struct DispatcherTest : public Test { |
| 9 | + Dispatcher dispatcher{}; |
| 10 | +}; |
| 11 | + |
| 12 | +TEST_F(DispatcherTest, MessagesIgnoredWhenNoListeners) { |
| 13 | + dispatcher.send(123); |
| 14 | + dispatcher.send(123.0f); |
| 15 | + dispatcher.send("abc"); |
| 16 | + |
| 17 | + struct CustomType { }; |
| 18 | + dispatcher.send(CustomType{}); |
| 19 | +} |
| 20 | + |
| 21 | +TEST_F(DispatcherTest, MessagesAreDispatchedToSingleListeners) { |
| 22 | + dispatcher.listen<int>([](const auto &msg) { |
| 23 | + EXPECT_EQ(msg, 123); |
| 24 | + }); |
| 25 | + dispatcher.send(123); |
| 26 | + |
| 27 | + int n = 123; |
| 28 | + dispatcher.listen<int>([&n](const auto &msg) { |
| 29 | + EXPECT_EQ(msg, n); |
| 30 | + }); |
| 31 | + dispatcher.send(n); |
| 32 | +} |
| 33 | + |
| 34 | +TEST_F(DispatcherTest, TheSameListenerCanBeAddedMultipleTimes) { |
| 35 | + MockFunction<void(const int &)> intCallback; |
| 36 | + EXPECT_CALL(intCallback, Call(123)).Times(2); |
| 37 | + |
| 38 | + dispatcher.listen(intCallback.AsStdFunction()); |
| 39 | + dispatcher.listen(intCallback.AsStdFunction()); |
| 40 | + |
| 41 | + dispatcher.send(123); |
| 42 | +} |
| 43 | + |
| 44 | +TEST_F(DispatcherTest, DifferentListenersOfSameTypeAreCalled) { |
| 45 | + StrictMock<MockFunction<void(const int &)>> intCallback1; |
| 46 | + StrictMock<MockFunction<void(const int &)>> intCallback2; |
| 47 | + EXPECT_CALL(intCallback1, Call(123)).Times(1); |
| 48 | + EXPECT_CALL(intCallback2, Call(123)).Times(1); |
| 49 | + |
| 50 | + dispatcher.listen(intCallback1.AsStdFunction()); |
| 51 | + dispatcher.listen(intCallback2.AsStdFunction()); |
| 52 | + |
| 53 | + dispatcher.send(123); |
| 54 | +} |
| 55 | + |
| 56 | +TEST_F(DispatcherTest, DifferentListenersOfDifferentTypesAreCalled) { |
| 57 | + StrictMock<MockFunction<void(const int &)>> intCallback; |
| 58 | + StrictMock<MockFunction<void(const float &)>> floatCallback; |
| 59 | + |
| 60 | + |
| 61 | + dispatcher.listen(intCallback.AsStdFunction()); |
| 62 | + dispatcher.listen(floatCallback.AsStdFunction()); |
| 63 | + |
| 64 | + EXPECT_CALL(intCallback, Call(1)).Times(1); |
| 65 | + dispatcher.send(1); |
| 66 | + |
| 67 | + EXPECT_CALL(floatCallback, Call(2.0f)).Times(1); |
| 68 | + dispatcher.send(2.0f); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +TEST_F(DispatcherTest, ListenerWithCustomEmptyType) { |
| 73 | + struct EmptyType { }; |
| 74 | + |
| 75 | + StrictMock<MockFunction<void(const EmptyType &)>> emptyTypeCallback; |
| 76 | + dispatcher.listen(emptyTypeCallback.AsStdFunction()); |
| 77 | + |
| 78 | + EXPECT_CALL(emptyTypeCallback, Call(A<const EmptyType &>())).Times(1); |
| 79 | + dispatcher.send(EmptyType{}); |
| 80 | + |
| 81 | + EXPECT_CALL(emptyTypeCallback, Call(A<const EmptyType &>())).Times(1); |
| 82 | + dispatcher.send(EmptyType{}); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(DispatcherTest, ListenerWithCustomType) { |
| 86 | + struct CustomType { |
| 87 | + int n; |
| 88 | + }; |
| 89 | + |
| 90 | + StrictMock<MockFunction<void(const CustomType &)>> customTypeCallback; |
| 91 | + dispatcher.listen(customTypeCallback.AsStdFunction()); |
| 92 | + |
| 93 | + EXPECT_CALL(customTypeCallback, Call(Field(&CustomType::n, 111))).Times(1); |
| 94 | + dispatcher.send(CustomType{ 111 }); |
| 95 | + |
| 96 | + EXPECT_CALL(customTypeCallback, Call(Field(&CustomType::n, 222))).Times(1); |
| 97 | + dispatcher.send(CustomType{ 222 }); |
| 98 | + |
| 99 | + dispatcher.send(333); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_F(DispatcherTest, ParentMessageTypeListenerShouldNotBeCalledForChildMessage) { |
| 103 | + struct Parent { }; |
| 104 | + struct Child : Parent { }; |
| 105 | + |
| 106 | + StrictMock<MockFunction<void(const Parent &)>> parentCallback; |
| 107 | + StrictMock<MockFunction<void(const Child &)>> childCallback; |
| 108 | + dispatcher.listen(parentCallback.AsStdFunction()); |
| 109 | + dispatcher.listen(childCallback.AsStdFunction()); |
| 110 | + |
| 111 | + EXPECT_CALL(childCallback, Call(A<const Child &>())).Times(1); |
| 112 | + dispatcher.send(Child{}); |
| 113 | +} |
0 commit comments