Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FlappyOrx_HS17/test/util/output/OutputStreamTest.cc @ 11783

Last change on this file since 11783 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1#include <gtest/gtest.h>
2#include <gmock/gmock.h>
3
4#define DISABLE_OUTPUT_CONTEXT_STATIC_CACHE
5
6#include "util/Output.h"
7#include "util/output/OutputStream.h"
8#include "util/output/OutputManager.h"
9#include "util/output/MemoryWriter.h"
10
11namespace orxonox
12{
13    namespace context
14    {
15        namespace
16        {
17            REGISTER_OUTPUT_CONTEXT(unittest1);
18        }
19    }
20
21    namespace
22    {
23        class MockOutputManager : public OutputManager
24        {
25            public:
26                MOCK_METHOD3(pushMessage, void(OutputLevel, const OutputContextContainer&, const std::string&));
27        };
28    }
29
30    // test level and context in constructor
31    TEST(OutputStreamTest, Constructor)
32    {
33        OutputStream stream(level::user_warning, context::unittest1());
34
35        EXPECT_EQ(level::user_warning, stream.getOutputLevel());
36        EXPECT_EQ(&context::unittest1(), stream.getOutputContext());
37    }
38
39    // test setOutputAttributes
40    TEST(OutputStreamTest, SetOutputAttributes)
41    {
42        OutputStream stream;
43        stream.setOutputAttributes(level::user_warning, context::unittest1());
44
45        EXPECT_EQ(level::user_warning, stream.getOutputLevel());
46        EXPECT_EQ(&context::unittest1(), stream.getOutputContext());
47    }
48
49    // test manipulator applied
50    TEST(OutputStreamTest, ManipulatorApplied)
51    {
52        OutputStream stream;
53        stream << "test";
54        EXPECT_EQ(4u, stream.str().size());
55
56        // apply manipulator
57        stream << std::ends;
58        EXPECT_EQ(5u, stream.str().size());
59    }
60
61    // test manipulator only applied if output accepted
62    TEST(OutputStreamTest, ManipulatorOnlyAppliedIfOutputAccepted)
63    {
64        // disable MemoryWriter because otherwise we would always accept all output
65        OutputManager::getInstance().getMemoryWriter()->disable();
66
67        {
68            OutputStream stream;
69            EXPECT_TRUE(stream.acceptsOutput());
70            stream << std::ends;
71            EXPECT_EQ(1u, stream.str().size());
72        }
73
74        {
75            OutputStream stream(level::verbose, context::undefined());
76            EXPECT_FALSE(stream.acceptsOutput());
77            stream << std::ends;
78            EXPECT_EQ(0u, stream.str().size());
79        }
80    }
81
82    // Fixture
83    class OutputStreamTestWithMockedOutputManager : public ::testing::Test
84    {
85        public:
86            virtual void SetUp()
87            {
88                this->manager_ = new MockOutputManager();
89                OutputManager::Testing::getInstancePointer().reset(this->manager_);
90            }
91
92            virtual void TearDown()
93            {
94                OutputManager::Testing::getInstancePointer().reset(new OutputManager());
95            }
96
97        protected:
98            MockOutputManager* manager_;
99    };
100
101    // test endl-manipulator sends output to OutputManager
102    TEST_F(OutputStreamTestWithMockedOutputManager, EndlSendsToOutputManager)
103    {
104        OutputStream stream;
105
106        const OutputContextContainer& context = context::unittest1();
107
108        stream.setOutputAttributes(level::user_warning, context);
109        EXPECT_TRUE(stream.acceptsOutput());
110        EXPECT_EQ(context, *stream.getOutputContext());
111
112        EXPECT_CALL(*this->manager_, pushMessage(level::user_warning, context, "some output"));
113
114        stream << "some output" << std::endl;
115    }
116
117    // test multiple lines sent to OutputManager
118    TEST_F(OutputStreamTestWithMockedOutputManager, MultipleLinesSentToOutputManager)
119    {
120        OutputStream stream;
121
122        const OutputContextContainer& context = context::unittest1();
123
124        stream.setOutputAttributes(level::user_warning, context);
125        EXPECT_TRUE(stream.acceptsOutput());
126        EXPECT_EQ(context, *stream.getOutputContext());
127
128        EXPECT_CALL(*this->manager_, pushMessage(level::user_warning, context, "some output"));
129        EXPECT_CALL(*this->manager_, pushMessage(level::user_warning, context, "more output"));
130        EXPECT_CALL(*this->manager_, pushMessage(level::user_warning, context, "yet another line of output"));
131
132        stream << "some output" << std::endl;
133        stream << "more output" << std::endl;
134        stream << "yet another line of output" << std::endl;
135    }
136
137    // test output only sent if accepted
138    TEST_F(OutputStreamTestWithMockedOutputManager, OutputOnlySentToOutputManagerIfAccepted)
139    {
140        // disable MemoryWriter because otherwise we would always accept all output
141        OutputManager::getInstanceAndCreateListeners().getMemoryWriter()->disable();
142
143        OutputStream stream;
144
145        const OutputContextContainer& context = context::unittest1();
146
147        stream.setOutputAttributes(level::verbose, context);
148        EXPECT_FALSE(stream.acceptsOutput());
149
150        EXPECT_CALL(*this->manager_, pushMessage(::testing::_, ::testing::_, ::testing::_)).Times(0);
151
152        stream << "some output" << std::endl;
153    }
154
155    // test desctructor sends remaining output to OutputManager
156    TEST_F(OutputStreamTestWithMockedOutputManager, DestructorSendsRemainingOutputToOutputManager)
157    {
158        OutputStream stream;
159
160        const OutputContextContainer& context = context::undefined();
161
162        stream.setOutputAttributes(level::verbose, context);
163        EXPECT_TRUE(stream.acceptsOutput());
164
165        EXPECT_CALL(*this->manager_, pushMessage(::testing::_, ::testing::_, "some output [missing endl]"));
166
167        stream << "some output";
168    }
169}
Note: See TracBrowser for help on using the repository browser.