[9545] | 1 | #include <gtest/gtest.h> |
---|
| 2 | #include "util/Output.h" |
---|
| 3 | #include "util/output/ConsoleWriter.h" |
---|
| 4 | #include "util/output/OutputManager.h" |
---|
| 5 | |
---|
| 6 | namespace orxonox |
---|
| 7 | { |
---|
[10624] | 8 | namespace |
---|
[9545] | 9 | { |
---|
[10624] | 10 | // Fixture |
---|
| 11 | class ConsoleWriterTest : public ::testing::Test |
---|
| 12 | { |
---|
| 13 | public: |
---|
[11071] | 14 | virtual void SetUp() override |
---|
[10624] | 15 | { |
---|
| 16 | // reset output manager |
---|
[11071] | 17 | OutputManager::Testing::getInstancePointer().reset(new OutputManager()); |
---|
[10624] | 18 | } |
---|
[9545] | 19 | |
---|
[11071] | 20 | virtual void TearDown() override |
---|
[10624] | 21 | { |
---|
| 22 | } |
---|
| 23 | }; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | TEST_F(ConsoleWriterTest, Disable) |
---|
| 27 | { |
---|
[11071] | 28 | std::ostream stream(nullptr); |
---|
[9545] | 29 | EXPECT_EQ(0U, OutputManager::getInstance().getListeners().size()); |
---|
| 30 | ConsoleWriter writer(stream); |
---|
| 31 | EXPECT_EQ(1U, OutputManager::getInstance().getListeners().size()); |
---|
| 32 | writer.disable(); |
---|
| 33 | EXPECT_EQ(0U, OutputManager::getInstance().getListeners().size()); |
---|
| 34 | } |
---|
| 35 | |
---|
[10624] | 36 | TEST_F(ConsoleWriterTest, Enable) |
---|
[9545] | 37 | { |
---|
[11071] | 38 | std::ostream stream(nullptr); |
---|
[9545] | 39 | ConsoleWriter writer(stream); |
---|
| 40 | writer.disable(); |
---|
| 41 | EXPECT_EQ(0U, OutputManager::getInstance().getListeners().size()); |
---|
| 42 | writer.enable(); |
---|
| 43 | EXPECT_EQ(1U, OutputManager::getInstance().getListeners().size()); |
---|
| 44 | } |
---|
| 45 | |
---|
[10624] | 46 | TEST_F(ConsoleWriterTest, WritesNoOutputToOutputStream) |
---|
[9545] | 47 | { |
---|
| 48 | std::stringbuf buffer; |
---|
| 49 | std::ostream stream(&buffer); |
---|
| 50 | ConsoleWriter writer(stream); |
---|
| 51 | writer.setLevelMax(level::verbose); |
---|
| 52 | |
---|
| 53 | EXPECT_EQ(0, buffer.in_avail()); |
---|
| 54 | EXPECT_EQ("", buffer.str()); |
---|
| 55 | } |
---|
| 56 | |
---|
[10624] | 57 | TEST_F(ConsoleWriterTest, WritesOutputToOutputStream) |
---|
[9545] | 58 | { |
---|
| 59 | std::stringbuf buffer; |
---|
| 60 | std::ostream stream(&buffer); |
---|
| 61 | ConsoleWriter writer(stream); |
---|
| 62 | writer.setLevelMax(level::verbose); |
---|
| 63 | |
---|
| 64 | std::vector<std::string> lines; |
---|
| 65 | lines.push_back("test"); |
---|
| 66 | |
---|
| 67 | EXPECT_TRUE(writer.acceptsOutput(level::debug_output, context::undefined())); |
---|
| 68 | |
---|
| 69 | writer.unfilteredOutput(level::debug_output, context::undefined(), lines); |
---|
| 70 | |
---|
| 71 | EXPECT_LT(0, buffer.in_avail()); |
---|
| 72 | EXPECT_NE("", buffer.str()); |
---|
| 73 | EXPECT_NE(std::string::npos, buffer.str().find("test")); |
---|
| 74 | } |
---|
| 75 | |
---|
[10624] | 76 | TEST_F(ConsoleWriterTest, DefaultConsoleWriterUsesCout) |
---|
[9545] | 77 | { |
---|
| 78 | OutputManager::getInstanceAndCreateListeners(); |
---|
[10712] | 79 | EXPECT_EQ(&std::cout, &OutputManager::getInstance().getConsoleWriter()->getOutputStream()); |
---|
[9545] | 80 | } |
---|
| 81 | } |
---|