forked from ros/console_bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_TEST.cc
340 lines (271 loc) · 11.5 KB
/
console_TEST.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2020, Open Source Robotics Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#include <cassert>
#include <fstream>
#include <sstream>
#include <gtest/gtest.h>
#include <console_bridge/console.h>
class OutputHandlerString : public console_bridge::OutputHandler
{
public:
OutputHandlerString()
{
}
~OutputHandlerString() override
{
}
void log(const std::string & text, console_bridge::LogLevel level, const char *filename, int line) override
{
(void)line;
(void)filename;
text_ = text;
log_level_ = level;
}
std::string text_;
console_bridge::LogLevel log_level_{console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_NONE};
};
//////////////////////////////////////////////////
TEST(ConsoleTest, MacroExpansionTest_ItShouldCompile)
{
if (true)
CONSOLE_BRIDGE_logDebug("Testing Log");
if (true)
CONSOLE_BRIDGE_logDebug("Testing Log");
else
{
assert(true);
}
if (true)
{
CONSOLE_BRIDGE_logDebug("Testing Log");
}
else
{
CONSOLE_BRIDGE_logDebug("Testing Log");
}
}
//////////////////////////////////////////////////
TEST(ConsoleTest, StdoutStderrOutput)
{
console_bridge::setLogLevel(console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_DEBUG);
EXPECT_NO_THROW(
CONSOLE_BRIDGE_logDebug("Testing Log"));
EXPECT_NO_THROW(
CONSOLE_BRIDGE_logInform("Testing Log"));
EXPECT_NO_THROW(
CONSOLE_BRIDGE_logWarn("Testing Log"));
EXPECT_NO_THROW(
CONSOLE_BRIDGE_logError("Testing Log"));
}
//////////////////////////////////////////////////
TEST(ConsoleTest, MultipleArguments)
{
// This tests that multiple arguments to the CONSOLE_BRIDGE_* macros get
// formatted and output properly.
OutputHandlerString string_oh;
console_bridge::useOutputHandler(&string_oh);
EXPECT_EQ(&string_oh, console_bridge::getOutputHandler());
CONSOLE_BRIDGE_logError("no extra parameters");
EXPECT_EQ(string_oh.text_, "no extra parameters");
CONSOLE_BRIDGE_logError("one integer: %d", 42);
EXPECT_EQ(string_oh.text_, "one integer: 42");
CONSOLE_BRIDGE_logError("two floats: %.2f, %.2f", 42.01, 1/3.0);
EXPECT_EQ(string_oh.text_, "two floats: 42.01, 0.33");
}
TEST(ConsoleTest, BasicOutputHandler)
{
// This tests that we can install a custom OutputHandler and log to it.
OutputHandlerString string_oh;
console_bridge::useOutputHandler(&string_oh);
EXPECT_EQ(&string_oh, console_bridge::getOutputHandler());
console_bridge::setLogLevel(console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_DEBUG);
CONSOLE_BRIDGE_logDebug("Debug");
EXPECT_EQ(string_oh.text_, "Debug");
EXPECT_EQ(string_oh.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_DEBUG);
EXPECT_EQ(console_bridge::getLogLevel(), console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_DEBUG);
}
TEST(ConsoleTest, LogLevelTooLow)
{
// This tests that the custom OutputHandler log() method is *not* invoked if
// the log level set in console_bridge is higher than the message log level.
OutputHandlerString string_oh;
console_bridge::useOutputHandler(&string_oh);
EXPECT_EQ(&string_oh, console_bridge::getOutputHandler());
console_bridge::setLogLevel(console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_INFO);
CONSOLE_BRIDGE_logDebug("Debug");
EXPECT_EQ(string_oh.text_, "");
EXPECT_EQ(string_oh.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_NONE);
}
TEST(ConsoleTest, SwapHandlers)
{
// This tests the ability to swap output handlers from one to another.
OutputHandlerString string_oh1;
OutputHandlerString string_oh2;
console_bridge::useOutputHandler(&string_oh1);
EXPECT_EQ(&string_oh1, console_bridge::getOutputHandler());
console_bridge::setLogLevel(console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_INFO);
CONSOLE_BRIDGE_logInform("Info1");
console_bridge::useOutputHandler(&string_oh2);
EXPECT_EQ(&string_oh2, console_bridge::getOutputHandler());
CONSOLE_BRIDGE_logInform("Info2");
EXPECT_EQ(string_oh1.text_, "Info1");
EXPECT_EQ(string_oh1.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_INFO);
EXPECT_EQ(string_oh2.text_, "Info2");
EXPECT_EQ(string_oh2.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_INFO);
}
TEST(ConsoleTest, RestoreHandler)
{
// This tests the console_bridge::restorePreviousOutputHandler() function.
OutputHandlerString string_oh1;
OutputHandlerString string_oh2;
console_bridge::useOutputHandler(&string_oh1);
EXPECT_EQ(&string_oh1, console_bridge::getOutputHandler());
console_bridge::setLogLevel(console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_INFO);
console_bridge::useOutputHandler(&string_oh2);
EXPECT_EQ(&string_oh2, console_bridge::getOutputHandler());
CONSOLE_BRIDGE_logInform("Info2");
console_bridge::restorePreviousOutputHandler();
CONSOLE_BRIDGE_logInform("Info1");
EXPECT_EQ(string_oh1.text_, "Info1");
EXPECT_EQ(string_oh1.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_INFO);
EXPECT_EQ(string_oh2.text_, "Info2");
EXPECT_EQ(string_oh2.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_INFO);
}
TEST(ConsoleTest, NoOutputHandler)
{
// This tests that calling console_bridge::noOutputHandler() results in
// no output, even when our custom OutputHandler is "installed".
OutputHandlerString string_oh;
console_bridge::useOutputHandler(&string_oh);
EXPECT_EQ(&string_oh, console_bridge::getOutputHandler());
console_bridge::setLogLevel(console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_DEBUG);
CONSOLE_BRIDGE_logDebug("Debug");
EXPECT_EQ(string_oh.text_, "Debug");
EXPECT_EQ(string_oh.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_DEBUG);
string_oh.text_ = "";
string_oh.log_level_ = console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_NONE;
console_bridge::noOutputHandler();
EXPECT_EQ(nullptr, console_bridge::getOutputHandler());
CONSOLE_BRIDGE_logDebug("Debug");
EXPECT_EQ(string_oh.text_, "");
EXPECT_EQ(string_oh.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_NONE);
console_bridge::restorePreviousOutputHandler();
CONSOLE_BRIDGE_logDebug("Debug2");
EXPECT_EQ(string_oh.text_, "Debug2");
EXPECT_EQ(string_oh.log_level_, console_bridge::LogLevel::CONSOLE_BRIDGE_LOG_DEBUG);
}
TEST(ConsoleTest, TestLogLevel)
{
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_DEBUG);
EXPECT_EQ(console_bridge::getLogLevel(), console_bridge::CONSOLE_BRIDGE_LOG_DEBUG);
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_INFO);
EXPECT_EQ(console_bridge::getLogLevel(), console_bridge::CONSOLE_BRIDGE_LOG_INFO);
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_WARN);
EXPECT_EQ(console_bridge::getLogLevel(), console_bridge::CONSOLE_BRIDGE_LOG_WARN);
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_ERROR);
EXPECT_EQ(console_bridge::getLogLevel(), console_bridge::CONSOLE_BRIDGE_LOG_ERROR);
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_NONE);
EXPECT_EQ(console_bridge::getLogLevel(), console_bridge::CONSOLE_BRIDGE_LOG_NONE);
}
TEST(ConsoleTest, TestOutputHandlerFileBadFilename) {
console_bridge::OutputHandlerFile handler("/really/hoping/this/path/doesnt/exist.txt");
EXPECT_NO_THROW(
handler.log("This should not crash", console_bridge::CONSOLE_BRIDGE_LOG_WARN, "file.cpp", 42));
console_bridge::useOutputHandler(&handler);
EXPECT_NO_THROW(
CONSOLE_BRIDGE_logError("This also should not crash, nor actually log anything"));
// ~OutputHandlerFile() should not fail to close a non-existent file handle
}
class FileHandlerTest : public ::testing::Test {
public:
FileHandlerTest() : log_filename_("tmp.txt") {}
virtual void SetUp()
{
// Needs to be reset to avoid side effects from other tests
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_WARN);
}
virtual void TearDown()
{
remove(log_filename());
}
std::string getTextFromLogFile() {
std::ifstream f(log_filename_);
std::stringstream result;
result << f.rdbuf();
return result.str();
}
const char * log_filename() { return log_filename_.c_str(); }
private:
std::string log_filename_;
};
TEST_F(FileHandlerTest, TestInformDoesntLog) {
// Use scoping to call ~OutputHandlerFile() and force in to flush contents and close file
{
const std::string text = "Some logging text";
console_bridge::OutputHandlerFile handler(log_filename());
console_bridge::useOutputHandler(&handler);
CONSOLE_BRIDGE_logInform("This shouldn't log to file because it's only inform");
}
const std::string result = getTextFromLogFile();
EXPECT_TRUE(result.empty()) << "Log file was not empty, it contained:\n\n" << result;
}
TEST_F(FileHandlerTest, TestErrorLogs) {
const std::string text = "Some logging text";
// Use scoping to call ~OutputHandlerFile() and force in to flush contents and close file
{
console_bridge::OutputHandlerFile handler(log_filename());
console_bridge::useOutputHandler(&handler);
CONSOLE_BRIDGE_logError(text.c_str());
}
const std::string expected_text = "Error: " + text;
const std::string result = getTextFromLogFile();
// Just checking that expected text is in the file, not checking full log statement.
EXPECT_NE(result.find(expected_text), result.npos)
<< "Log file did not contain expected text, instead it contained:\n\n: " << result;
}
TEST_F(FileHandlerTest, TestInformLogsWithLogLevel) {
const std::string text = "Some logging text";
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_INFO);
// Use scoping to call ~OutputHandlerFile() and force in to flush contents and close file
{
console_bridge::OutputHandlerFile handler(log_filename());
console_bridge::useOutputHandler(&handler);
CONSOLE_BRIDGE_logInform(text.c_str());
}
const std::string expected_text = "Info: " + text;
const std::string result = getTextFromLogFile();
// Just checking that expected text is in the file, not checking full log statement.
EXPECT_NE(result.find(expected_text), result.npos)
<< "Log file did not contain expected text, instead it contained:\n\n: " << result;
}