Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/testing/test/util/output/LogWriterTest.cc @ 9541

Last change on this file since 9541 was 9538, checked in by landauf, 11 years ago

small refactoring in LogWriter and tests added

File size: 3.7 KB
Line 
1#include <gtest/gtest.h>
2#include "util/Output.h"
3#include "util/output/LogWriter.h"
4
5namespace orxonox
6{
7    // test constructor opens file
8    TEST(LogWriterTest, ConstructorOpensFile)
9    {
10        LogWriter logWriter;
11        EXPECT_TRUE(logWriter.getFile().is_open());
12    }
13
14    // setLogDirectory changes file, opens correct file
15    bool fileExists(const std::string& path)
16    {
17        std::ifstream stream(path.c_str(), std::fstream::in);
18        bool exists = stream.is_open();
19        stream.close();
20        return exists;
21    }
22
23    bool fileSuccessfullyDeleted(const std::string& path)
24    {
25        return std::remove(path.c_str()) == 0;
26    }
27
28    TEST(LogWriterTest, SetLogDirectoryOpensNewFile)
29    {
30        std::string path = "./orxonox.log";
31
32        // cleanup before test
33        fileSuccessfullyDeleted(path);
34
35        {
36            LogWriter logWriter;
37            EXPECT_FALSE(fileExists(path));
38            logWriter.setLogDirectory(".");
39            EXPECT_TRUE(fileExists(path));
40        }
41
42        // cleanup after test
43        EXPECT_TRUE(fileSuccessfullyDeleted(path));
44    }
45
46    // prints output to logfile
47    TEST(LogWriterTest, PrintsOutputToLogfile)
48    {
49        std::string path;
50
51        {
52            // write lines to log file
53            std::vector<std::string> lines;
54            lines.push_back("mytestoutput");
55
56            LogWriter logWriter;
57            logWriter.setLogDirectory(".");
58            logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines);
59
60            path = logWriter.getPath();
61        }
62
63        {
64            std::ifstream file(path.c_str(), std::fstream::in);
65            ASSERT_TRUE(file.is_open());
66            ASSERT_TRUE(file.good());
67            ASSERT_FALSE(file.eof());
68
69            while (!file.eof())
70            {
71                std::string line;
72                std::getline(file, line);
73
74                // see if we find the output and return
75                if (line.find("mytestoutput") != std::string::npos)
76                    return;
77            }
78
79            // output not found - failure!
80            FAIL();
81        }
82    }
83
84    // prints time to logfile
85    TEST(LogWriterTest, PrintsTimestampToLogfile)
86    {
87        std::string path;
88
89        {
90            // write lines to log file
91            std::vector<std::string> lines;
92            lines.push_back("myothertestoutput");
93
94            LogWriter logWriter;
95            logWriter.setLogDirectory(".");
96            logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines);
97
98            path = logWriter.getPath();
99        }
100
101        {
102            std::ifstream file(path.c_str(), std::fstream::in);
103            ASSERT_TRUE(file.is_open());
104            ASSERT_TRUE(file.good());
105            ASSERT_FALSE(file.eof());
106
107            while (!file.eof())
108            {
109                std::string line;
110                std::getline(file, line);
111
112                // see if we find the output and return
113                if (line.find("myothertestoutput") != std::string::npos)
114                {
115                    EXPECT_TRUE(std::isdigit(line[0]));
116                    EXPECT_TRUE(std::isdigit(line[1]));
117                    EXPECT_EQ(':', line[2]);
118                    EXPECT_TRUE(std::isdigit(line[3]));
119                    EXPECT_TRUE(std::isdigit(line[4]));
120                    EXPECT_EQ(':', line[5]);
121                    EXPECT_TRUE(std::isdigit(line[6]));
122                    EXPECT_TRUE(std::isdigit(line[7]));
123                    EXPECT_EQ(' ', line[8]);
124                    return;
125                }
126            }
127
128            // output not found - failure!
129            FAIL();
130        }
131    }
132}
Note: See TracBrowser for help on using the repository browser.