Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/towerdefenseFS15/test/util/output/OutputStreamTest.cc @ 10422

Last change on this file since 10422 was 9547, checked in by landauf, 12 years ago

return pointers instead of references because they could be null

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