Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial5/test/util/output/BaseWriterTest.cc @ 11103

Last change on this file since 11103 was 9545, checked in by landauf, 12 years ago

eol-style native

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1#include <gtest/gtest.h>
2#include <gmock/gmock.h>
3#include "util/Output.h"
4#include "util/output/BaseWriter.h"
5#include "util/output/OutputManager.h"
6
7namespace orxonox
8{
9    namespace context
10    {
11        namespace
12        {
13            REGISTER_OUTPUT_CONTEXT(unittest1);
14            REGISTER_OUTPUT_CONTEXT(unittest2);
15            REGISTER_OUTPUT_CONTEXT(unittest3);
16
17            REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub1);
18            REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub2);
19            REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub3);
20        }
21    }
22
23    namespace
24    {
25        class MockBaseWriter : public BaseWriter
26        {
27            public:
28                MockBaseWriter(const std::string& name, bool bRegister = true) : BaseWriter(name, bRegister) {}
29
30                MOCK_METHOD2(printLine, void(const std::string&, OutputLevel));
31
32                virtual void output(OutputLevel level, const OutputContextContainer& context, const std::vector<std::string>& lines)
33                    { this->BaseWriter::output(level, context, lines); }
34        };
35    }
36
37    // test getName
38    TEST(BaseWriterTest, GetName)
39    {
40        MockBaseWriter writer("mytestname");
41        EXPECT_EQ("mytestname", writer.getName());
42    }
43
44    // test configurable names contain name
45    TEST(BaseWriterTest, ConfigurableNamesContainName)
46    {
47        MockBaseWriter writer("mytestname");
48        EXPECT_NE(writer.getConfigurableMaxLevelName().find(writer.getName()), std::string::npos);
49        EXPECT_NE(writer.getConfigurableAdditionalContextsMaxLevelName().find(writer.getName()), std::string::npos);
50        EXPECT_NE(writer.getConfigurableAdditionalContextsName().find(writer.getName()), std::string::npos);
51
52        // but the section name is 'static' (does not contain the name)
53        EXPECT_EQ(writer.getConfigurableSectionName().find(writer.getName()), std::string::npos);
54    }
55
56    // output calls printLine for each line
57    TEST(BaseWriterTest, OutputCallsPrintLine)
58    {
59        MockBaseWriter writer("mytestname");
60
61        std::vector<std::string> lines;
62        lines.push_back("line1");
63        lines.push_back("line2");
64
65        EXPECT_CALL(writer, printLine(::testing::_, level::debug_output)).Times(2);
66
67        writer.output(level::debug_output, context::undefined(), lines);
68    }
69
70    // output adds prefix to first line
71    TEST(BaseWriterTest, OutputAddsPrefixToFirstLine)
72    {
73        MockBaseWriter writer("mytestname");
74
75        std::vector<std::string> lines;
76        lines.push_back("line1");
77
78        std::string prefix = OutputManager::getInstance().getDefaultPrefix(level::debug_output, context::undefined());
79        EXPECT_CALL(writer, printLine(prefix + "line1", level::debug_output));
80
81        writer.output(level::debug_output, context::undefined(), lines);
82    }
83
84    // setLevelMax sets level max
85    void testLevelMaxUserWarning(OutputLevel mask)
86    {
87        EXPECT_FALSE(mask & level::none);
88        EXPECT_TRUE(mask & level::message);
89        EXPECT_TRUE(mask & level::debug_output);
90        EXPECT_TRUE(mask & level::user_error);
91        EXPECT_TRUE(mask & level::user_warning);
92        EXPECT_FALSE(mask & level::user_status);
93        EXPECT_FALSE(mask & level::verbose);
94    }
95
96    TEST(BaseWriterTest, SetLevelMax)
97    {
98        MockBaseWriter writer("mytestname");
99
100        writer.setLevelMax(level::user_warning);
101        testLevelMaxUserWarning(writer.getLevelMask());
102    }
103
104    // setAdditionalContextsLevelMax sets additional contexts level max
105    TEST(BaseWriterTest, SetAdditionalContextsLevelMax)
106    {
107        MockBaseWriter writer("mytestname");
108
109        writer.setAdditionalContextsLevelMax(level::user_warning);
110        testLevelMaxUserWarning(writer.getAdditionalContextsLevelMask());
111    }
112
113    // changedConfigurableLevel sets max level to configurableMaxLevel_
114    TEST(BaseWriterTest, ChangedConfigurableLevel)
115    {
116        MockBaseWriter writer("mytestname");
117
118        writer.configurableMaxLevel_ = level::user_warning;
119        writer.changedConfigurableLevel();
120
121        testLevelMaxUserWarning(writer.getLevelMask());
122    }
123
124    // changedConfigurableAdditionalContextsLevel sets additional contexts level max to configurableAdditionalContextsMaxLevel_
125    TEST(BaseWriterTest, ChangedConfigurableAdditionalContextsLevel)
126    {
127        MockBaseWriter writer("mytestname");
128
129        writer.configurableAdditionalContextsMaxLevel_ = level::user_warning;
130        writer.changedConfigurableAdditionalContextsLevel();
131
132        testLevelMaxUserWarning(writer.getAdditionalContextsLevelMask());
133    }
134
135    // changedConfigurableAdditionalContexts sets additional contexts to configurableAdditionalContexts_
136    TEST(BaseWriterTest, ChangedConfigurableAdditionalContexts)
137    {
138        MockBaseWriter writer("mytestname");
139
140        writer.configurableAdditionalContexts_.push_back("unittest1");
141        writer.changedConfigurableAdditionalContexts();
142
143        EXPECT_TRUE(writer.getAdditionalContextsMask() & context::unittest1().mask);
144    }
145
146    // contexts added by name
147    TEST(BaseWriterTest, DefineContextsByName)
148    {
149        MockBaseWriter writer("mytestname");
150
151        writer.configurableAdditionalContexts_.push_back("unittest1");
152        writer.configurableAdditionalContexts_.push_back("unittest2");
153        writer.changedConfigurableAdditionalContexts();
154
155        EXPECT_TRUE(writer.acceptsOutput(level::message, context::unittest1()));
156        EXPECT_TRUE(writer.acceptsOutput(level::message, context::unittest2()));
157        EXPECT_FALSE(writer.acceptsOutput(level::message, context::unittest3()));
158    }
159
160    // subcontexts added by name, separated from base context with "::"
161    TEST(BaseWriterTest, DefineSubcontextsByName)
162    {
163        MockBaseWriter writer("mytestname");
164
165        writer.configurableAdditionalContexts_.push_back("unittest3::sub1");
166        writer.configurableAdditionalContexts_.push_back("unittest3::sub3");
167        writer.changedConfigurableAdditionalContexts();
168
169        EXPECT_FALSE(writer.acceptsOutput(level::message, context::unittest3()));
170        EXPECT_FALSE(writer.acceptsOutput(level::message, context::unittest3()));
171        EXPECT_FALSE(writer.acceptsOutput(level::message, context::unittest3()));
172        EXPECT_TRUE(writer.acceptsOutput(level::message, context::sub1()));
173        EXPECT_FALSE(writer.acceptsOutput(level::message, context::sub2()));
174        EXPECT_TRUE(writer.acceptsOutput(level::message, context::sub3()));
175    }
176}
Note: See TracBrowser for help on using the repository browser.