1 | #include <gtest/gtest.h> |
---|
2 | #include <gmock/gmock.h> |
---|
3 | |
---|
4 | #include "core/object/ObjectListIterator.h" |
---|
5 | #include "core/object/Listable.h" |
---|
6 | #include "core/CoreIncludes.h" |
---|
7 | #include "core/module/ModuleInstance.h" |
---|
8 | |
---|
9 | namespace orxonox |
---|
10 | { |
---|
11 | namespace |
---|
12 | { |
---|
13 | class ListableTest : public Listable |
---|
14 | { |
---|
15 | public: |
---|
16 | ListableTest() { RegisterObject(ListableTest); } |
---|
17 | ListableTest(Context* context) : Listable(context) { RegisterObject(ListableTest); } |
---|
18 | MOCK_METHOD0(test, void()); |
---|
19 | }; |
---|
20 | |
---|
21 | RegisterClassNoArgs(ListableTest); |
---|
22 | |
---|
23 | // Fixture |
---|
24 | class ObjectListTest : public ::testing::Test |
---|
25 | { |
---|
26 | public: |
---|
27 | virtual void SetUp() override |
---|
28 | { |
---|
29 | new IdentifierManager(); |
---|
30 | ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
31 | Context::setRootContext(new Context(nullptr)); |
---|
32 | } |
---|
33 | |
---|
34 | virtual void TearDown() override |
---|
35 | { |
---|
36 | Context::destroyRootContext(); |
---|
37 | ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
38 | delete &IdentifierManager::getInstance(); |
---|
39 | } |
---|
40 | }; |
---|
41 | } |
---|
42 | |
---|
43 | TEST_F(ObjectListTest, CanIterateOverEmptyList) |
---|
44 | { |
---|
45 | size_t i = 0; |
---|
46 | for (ListableTest* object : ObjectList<ListableTest>()) |
---|
47 | { |
---|
48 | ++i; |
---|
49 | ((void*)object); // avoid warning about unused variable |
---|
50 | } |
---|
51 | EXPECT_EQ(0u, i); |
---|
52 | } |
---|
53 | |
---|
54 | TEST_F(ObjectListTest, CanIterateOverFullList) |
---|
55 | { |
---|
56 | ListableTest test1; |
---|
57 | ListableTest test2; |
---|
58 | ListableTest test3; |
---|
59 | |
---|
60 | size_t i = 0; |
---|
61 | for (ListableTest* object : ObjectList<ListableTest>()) |
---|
62 | { |
---|
63 | ++i; |
---|
64 | if (i == 1u) EXPECT_EQ(&test1, object); |
---|
65 | if (i == 2u) EXPECT_EQ(&test2, object); |
---|
66 | if (i == 3u) EXPECT_EQ(&test3, object); |
---|
67 | } |
---|
68 | EXPECT_EQ(3u, i); |
---|
69 | } |
---|
70 | |
---|
71 | TEST_F(ObjectListTest, CanIterateOverFullList_Expanded) |
---|
72 | { |
---|
73 | ListableTest test1; |
---|
74 | ListableTest test2; |
---|
75 | ListableTest test3; |
---|
76 | |
---|
77 | size_t i = 0; |
---|
78 | ObjectList<ListableTest>&& __range = ObjectList<ListableTest>(); |
---|
79 | ObjectListIterator<ListableTest> __begin = __range.begin(); |
---|
80 | ObjectListIterator<ListableTest>__end = __range.end(); |
---|
81 | for (; __begin != __end; ++__begin) |
---|
82 | { |
---|
83 | ListableTest* object = *__begin; |
---|
84 | |
---|
85 | ++i; |
---|
86 | if (i == 1u) EXPECT_EQ(&test1, object); |
---|
87 | if (i == 2u) EXPECT_EQ(&test2, object); |
---|
88 | if (i == 3u) EXPECT_EQ(&test3, object); |
---|
89 | } |
---|
90 | EXPECT_EQ(3u, i); |
---|
91 | } |
---|
92 | |
---|
93 | TEST_F(ObjectListTest, CanCallObjects) |
---|
94 | { |
---|
95 | ListableTest test1; |
---|
96 | ListableTest test2; |
---|
97 | ListableTest test3; |
---|
98 | |
---|
99 | EXPECT_CALL(test1, test()); |
---|
100 | EXPECT_CALL(test2, test()); |
---|
101 | EXPECT_CALL(test3, test()); |
---|
102 | |
---|
103 | for (ListableTest* object : ObjectList<ListableTest>()) |
---|
104 | object->test(); |
---|
105 | } |
---|
106 | |
---|
107 | TEST_F(ObjectListTest, UsesCorrectContext) |
---|
108 | { |
---|
109 | Context context1(Context::getRootContext()); |
---|
110 | Context context2(Context::getRootContext()); |
---|
111 | |
---|
112 | ListableTest test1(&context1); |
---|
113 | ListableTest test2(&context1); |
---|
114 | ListableTest test3(&context2); |
---|
115 | |
---|
116 | EXPECT_EQ(3u, ObjectList<ListableTest>().size()); |
---|
117 | EXPECT_EQ(2u, ObjectList<ListableTest>(&context1).size()); |
---|
118 | EXPECT_EQ(1u, ObjectList<ListableTest>(&context2).size()); |
---|
119 | } |
---|
120 | |
---|
121 | TEST_F(ObjectListTest, CanIterateOverCorrectContext) |
---|
122 | { |
---|
123 | Context context1(Context::getRootContext()); |
---|
124 | Context context2(Context::getRootContext()); |
---|
125 | |
---|
126 | ListableTest test1(&context1); |
---|
127 | ListableTest test2(&context1); |
---|
128 | ListableTest test3(&context2); |
---|
129 | |
---|
130 | { |
---|
131 | size_t i = 0; |
---|
132 | for (ListableTest* object : ObjectList<ListableTest>(&context1)) |
---|
133 | { |
---|
134 | ++i; |
---|
135 | if (i == 1u) EXPECT_EQ(&test1, object); |
---|
136 | if (i == 2u) EXPECT_EQ(&test2, object); |
---|
137 | } |
---|
138 | EXPECT_EQ(2u, i); |
---|
139 | } |
---|
140 | { |
---|
141 | size_t i = 0; |
---|
142 | for (ListableTest* object : ObjectList<ListableTest>(&context2)) |
---|
143 | { |
---|
144 | ++i; |
---|
145 | if (i == 1u) EXPECT_EQ(&test3, object); |
---|
146 | } |
---|
147 | EXPECT_EQ(1u, i); |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|