1 | #include <gtest/gtest.h> |
---|
2 | #include <gmock/gmock.h> |
---|
3 | #include "util/Output.h" |
---|
4 | #include "util/output/SubcontextOutputListener.h" |
---|
5 | |
---|
6 | namespace orxonox |
---|
7 | { |
---|
8 | namespace context |
---|
9 | { |
---|
10 | namespace |
---|
11 | { |
---|
12 | REGISTER_OUTPUT_CONTEXT(unittest1); |
---|
13 | REGISTER_OUTPUT_CONTEXT(unittest2); |
---|
14 | REGISTER_OUTPUT_CONTEXT(unittest3); |
---|
15 | |
---|
16 | REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub1); |
---|
17 | REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub2); |
---|
18 | REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub3); |
---|
19 | } |
---|
20 | } |
---|
21 | |
---|
22 | namespace |
---|
23 | { |
---|
24 | class MockSubcontextOutputListener : public SubcontextOutputListener |
---|
25 | { |
---|
26 | public: |
---|
27 | MockSubcontextOutputListener(bool bRegister = true) : SubcontextOutputListener(bRegister) {} |
---|
28 | MOCK_METHOD3(output, void(OutputLevel, const OutputContextContainer&, const std::vector<std::string>&)); |
---|
29 | }; |
---|
30 | } |
---|
31 | |
---|
32 | TEST(SubcontextOutputListenerTest, ContextsDefinedCorrectly) |
---|
33 | { |
---|
34 | EXPECT_TRUE(context::unittest1().sub_id == context::no_subcontext); |
---|
35 | EXPECT_TRUE(context::unittest2().sub_id == context::no_subcontext); |
---|
36 | EXPECT_TRUE(context::unittest3().sub_id == context::no_subcontext); |
---|
37 | EXPECT_FALSE(context::sub1().sub_id == context::no_subcontext); |
---|
38 | EXPECT_FALSE(context::sub2().sub_id == context::no_subcontext); |
---|
39 | EXPECT_FALSE(context::sub3().sub_id == context::no_subcontext); |
---|
40 | } |
---|
41 | |
---|
42 | // test setAdditionalContextsMask |
---|
43 | TEST(SubcontextOutputListenerTest, SetAdditionalContextsMask) |
---|
44 | { |
---|
45 | MockSubcontextOutputListener listener; |
---|
46 | |
---|
47 | listener.setAdditionalContextsMask(context::unittest1().mask | context::unittest2().mask); |
---|
48 | |
---|
49 | OutputContextMask mask = listener.getAdditionalContextsMask(); |
---|
50 | |
---|
51 | EXPECT_TRUE(mask & context::unittest1().mask); |
---|
52 | EXPECT_TRUE(mask & context::unittest2().mask); |
---|
53 | EXPECT_FALSE(mask & context::unittest3().mask); |
---|
54 | } |
---|
55 | |
---|
56 | // test setAdditionalSubcontexts |
---|
57 | TEST(SubcontextOutputListenerTest, SetAdditionalSubcontexts) |
---|
58 | { |
---|
59 | MockSubcontextOutputListener listener; |
---|
60 | |
---|
61 | std::set<const OutputContextContainer*> subcontexts; |
---|
62 | subcontexts.insert(&context::sub1()); |
---|
63 | subcontexts.insert(&context::sub3()); |
---|
64 | |
---|
65 | listener.setAdditionalSubcontexts(subcontexts); |
---|
66 | OutputContextMask mask = listener.getAdditionalContextsMask(); |
---|
67 | |
---|
68 | EXPECT_FALSE(mask & context::unittest1().mask); |
---|
69 | EXPECT_FALSE(mask & context::unittest2().mask); |
---|
70 | EXPECT_TRUE(mask & context::unittest3().mask); |
---|
71 | } |
---|
72 | |
---|
73 | // test setAdditionalContextsMask and setAdditionalSubcontexts |
---|
74 | TEST(SubcontextOutputListenerTest, SetAdditionalContextsAndSubcontexts) |
---|
75 | { |
---|
76 | MockSubcontextOutputListener listener; |
---|
77 | |
---|
78 | std::set<const OutputContextContainer*> subcontexts; |
---|
79 | subcontexts.insert(&context::sub1()); |
---|
80 | subcontexts.insert(&context::sub3()); |
---|
81 | |
---|
82 | listener.setAdditionalSubcontexts(subcontexts); |
---|
83 | listener.setAdditionalContextsMask(context::unittest1().mask | context::unittest2().mask); |
---|
84 | |
---|
85 | OutputContextMask mask = listener.getAdditionalContextsMask(); |
---|
86 | |
---|
87 | EXPECT_TRUE(mask & context::unittest1().mask); |
---|
88 | EXPECT_TRUE(mask & context::unittest2().mask); |
---|
89 | EXPECT_TRUE(mask & context::unittest3().mask); |
---|
90 | } |
---|
91 | |
---|
92 | // test acceptsOutput |
---|
93 | namespace |
---|
94 | { |
---|
95 | void testContext(const OutputListener& listener, const OutputContextContainer& context, bool accepted) |
---|
96 | { |
---|
97 | EXPECT_TRUE(accepted == listener.acceptsOutput(level::message, context)); |
---|
98 | EXPECT_TRUE(accepted == listener.acceptsOutput(level::user_error, context)); |
---|
99 | EXPECT_TRUE(accepted == listener.acceptsOutput(level::internal_info, context)); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | TEST(SubcontextOutputListenerTest, NoContext) |
---|
104 | { |
---|
105 | MockSubcontextOutputListener listener; |
---|
106 | listener.setAdditionalContextsLevelMax(level::verbose); |
---|
107 | |
---|
108 | testContext(listener, context::unittest1(), false); |
---|
109 | testContext(listener, context::unittest2(), false); |
---|
110 | testContext(listener, context::unittest3(), false); |
---|
111 | testContext(listener, context::sub1(), false); |
---|
112 | testContext(listener, context::sub2(), false); |
---|
113 | testContext(listener, context::sub3(), false); |
---|
114 | } |
---|
115 | |
---|
116 | TEST(SubcontextOutputListenerTest, OneContext) |
---|
117 | { |
---|
118 | MockSubcontextOutputListener listener; |
---|
119 | listener.setAdditionalContextsLevelMax(level::verbose); |
---|
120 | |
---|
121 | listener.setAdditionalContextsMask(context::unittest1().mask); |
---|
122 | |
---|
123 | testContext(listener, context::unittest1(), true); |
---|
124 | testContext(listener, context::unittest2(), false); |
---|
125 | testContext(listener, context::unittest3(), false); |
---|
126 | testContext(listener, context::sub1(), false); |
---|
127 | testContext(listener, context::sub2(), false); |
---|
128 | testContext(listener, context::sub3(), false); |
---|
129 | } |
---|
130 | |
---|
131 | TEST(SubcontextOutputListenerTest, TwoContexts) |
---|
132 | { |
---|
133 | MockSubcontextOutputListener listener; |
---|
134 | listener.setAdditionalContextsLevelMax(level::verbose); |
---|
135 | |
---|
136 | listener.setAdditionalContextsMask(context::unittest1().mask | context::unittest2().mask); |
---|
137 | |
---|
138 | testContext(listener, context::unittest1(), true); |
---|
139 | testContext(listener, context::unittest2(), true); |
---|
140 | testContext(listener, context::unittest3(), false); |
---|
141 | testContext(listener, context::sub1(), false); |
---|
142 | testContext(listener, context::sub2(), false); |
---|
143 | testContext(listener, context::sub3(), false); |
---|
144 | } |
---|
145 | |
---|
146 | TEST(SubcontextOutputListenerTest, OneSubcontext) |
---|
147 | { |
---|
148 | MockSubcontextOutputListener listener; |
---|
149 | listener.setAdditionalContextsLevelMax(level::verbose); |
---|
150 | |
---|
151 | std::set<const OutputContextContainer*> subcontexts; |
---|
152 | subcontexts.insert(&context::sub1()); |
---|
153 | |
---|
154 | listener.setAdditionalSubcontexts(subcontexts); |
---|
155 | |
---|
156 | testContext(listener, context::unittest1(), false); |
---|
157 | testContext(listener, context::unittest2(), false); |
---|
158 | testContext(listener, context::unittest3(), false); |
---|
159 | testContext(listener, context::sub1(), true); |
---|
160 | testContext(listener, context::sub2(), false); |
---|
161 | testContext(listener, context::sub3(), false); |
---|
162 | } |
---|
163 | |
---|
164 | TEST(SubcontextOutputListenerTest, TwoSubcontexts) |
---|
165 | { |
---|
166 | MockSubcontextOutputListener listener; |
---|
167 | listener.setAdditionalContextsLevelMax(level::verbose); |
---|
168 | |
---|
169 | std::set<const OutputContextContainer*> subcontexts; |
---|
170 | subcontexts.insert(&context::sub1()); |
---|
171 | subcontexts.insert(&context::sub3()); |
---|
172 | |
---|
173 | listener.setAdditionalSubcontexts(subcontexts); |
---|
174 | |
---|
175 | testContext(listener, context::unittest1(), false); |
---|
176 | testContext(listener, context::unittest2(), false); |
---|
177 | testContext(listener, context::unittest3(), false); |
---|
178 | testContext(listener, context::sub1(), true); |
---|
179 | testContext(listener, context::sub2(), false); |
---|
180 | testContext(listener, context::sub3(), true); |
---|
181 | } |
---|
182 | |
---|
183 | TEST(SubcontextOutputListenerTest, ContextsAndSubcontexts) |
---|
184 | { |
---|
185 | MockSubcontextOutputListener listener; |
---|
186 | listener.setAdditionalContextsLevelMax(level::verbose); |
---|
187 | |
---|
188 | std::set<const OutputContextContainer*> subcontexts; |
---|
189 | subcontexts.insert(&context::sub1()); |
---|
190 | subcontexts.insert(&context::sub3()); |
---|
191 | |
---|
192 | listener.setAdditionalContextsMask(context::unittest1().mask | context::unittest2().mask); |
---|
193 | listener.setAdditionalSubcontexts(subcontexts); |
---|
194 | |
---|
195 | testContext(listener, context::unittest1(), true); |
---|
196 | testContext(listener, context::unittest2(), true); |
---|
197 | testContext(listener, context::unittest3(), false); |
---|
198 | testContext(listener, context::sub1(), true); |
---|
199 | testContext(listener, context::sub2(), false); |
---|
200 | testContext(listener, context::sub3(), true); |
---|
201 | } |
---|
202 | |
---|
203 | TEST(SubcontextOutputListenerTest, BaseContextEnablesAllSubcontexts) |
---|
204 | { |
---|
205 | MockSubcontextOutputListener listener; |
---|
206 | listener.setAdditionalContextsLevelMax(level::verbose); |
---|
207 | |
---|
208 | listener.setAdditionalContextsMask(context::unittest3().mask); |
---|
209 | |
---|
210 | testContext(listener, context::unittest1(), false); |
---|
211 | testContext(listener, context::unittest2(), false); |
---|
212 | testContext(listener, context::unittest3(), true); |
---|
213 | testContext(listener, context::sub1(), true); |
---|
214 | testContext(listener, context::sub2(), true); |
---|
215 | testContext(listener, context::sub3(), true); |
---|
216 | } |
---|
217 | } |
---|