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