Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 10, 2013, 11:58:49 AM (12 years ago)
Author:
landauf
Message:

added more tests

Location:
code/branches/testing/test/util/output
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/testing/test/util/output/OutputDefinitionsTest.cc

    r9533 r9541  
    88        EXPECT_EQ(0x0000, level::none);
    99    }
     10
     11    TEST(OutputDefinitionsTest, OutputContextContainer)
     12    {
     13        OutputContextContainer container1;
     14        container1.mask = 1;
     15        container1.sub_id = 1;
     16        container1.name = "1";
     17
     18        OutputContextContainer container2;
     19        container2.mask = 1;
     20        container2.sub_id = 1;
     21        container2.name = "1";
     22
     23        EXPECT_TRUE(container1 == container2);
     24
     25        {
     26            OutputContextContainer container3(container2);
     27            EXPECT_TRUE(container1 == container3);
     28            container3.mask = 2;
     29            EXPECT_FALSE(container1 == container3);
     30        }
     31
     32        {
     33            OutputContextContainer container3(container2);
     34            EXPECT_TRUE(container1 == container3);
     35            container3.sub_id = 2;
     36            EXPECT_FALSE(container1 == container3);
     37        }
     38
     39        {
     40            OutputContextContainer container3(container2);
     41            EXPECT_TRUE(container1 == container3);
     42            container3.name = "2";
     43            EXPECT_FALSE(container1 == container3);
     44        }
     45    }
    1046}
  • code/branches/testing/test/util/output/OutputStreamTest.cc

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