Changeset 9544 for code/branches/testing/test/util/output
- Timestamp:
- Mar 11, 2013, 11:13:21 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/testing/test/util/output/LogWriterTest.cc
r9538 r9544 2 2 #include "util/Output.h" 3 3 #include "util/output/LogWriter.h" 4 #include "util/Convert.h" 4 5 5 6 namespace orxonox 6 7 { 8 namespace 9 { 10 class MockLogWriter : public LogWriter 11 { 12 public: 13 virtual void printLine(const std::string& line, OutputLevel level) 14 { this->LogWriter::printLine(line, level); } 15 }; 16 } 17 7 18 // test constructor opens file 8 19 TEST(LogWriterTest, ConstructorOpensFile) … … 15 26 bool fileExists(const std::string& path) 16 27 { 17 std::ifstream stream(path.c_str() , std::fstream::in);18 bool exists = stream.is_open() ;28 std::ifstream stream(path.c_str()); 29 bool exists = stream.is_open() && stream.good(); 19 30 stream.close(); 20 31 return exists; 21 32 } 22 33 23 bool fileSuccessfullyDeleted(const std::string& path) 24 { 25 return std::remove(path.c_str()) == 0; 34 std::string getLineWhichContains(const std::string& path, const std::string& message) 35 { 36 std::ifstream file(path.c_str()); 37 EXPECT_TRUE(file.is_open()); 38 EXPECT_TRUE(file.good()); 39 EXPECT_FALSE(file.eof()); 40 41 while (file.is_open() && file.good() && !file.eof()) 42 { 43 std::string line; 44 std::getline(file, line); 45 46 // see if we find the output and return 47 if (line.find(message) == (line.size() - message.size())) 48 return line; 49 } 50 51 return std::string(); 52 } 53 54 bool fileContains(const std::string& path, const std::string& message) 55 { 56 return !getLineWhichContains(path, message).empty(); 57 } 58 59 bool deleteFile(const std::string& path) 60 { 61 bool result = std::remove(path.c_str()) == 0; 62 EXPECT_FALSE(fileExists(path)); 63 return result; 26 64 } 27 65 … … 31 69 32 70 // cleanup before test 33 fileSuccessfullyDeleted(path);71 deleteFile(path); 34 72 35 73 { … … 41 79 42 80 // cleanup after test 43 EXPECT_TRUE( fileSuccessfullyDeleted(path));81 EXPECT_TRUE(deleteFile(path)); 44 82 } 45 83 … … 61 99 } 62 100 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 } 101 EXPECT_TRUE(fileContains(path, "mytestoutput")); 82 102 } 83 103 … … 99 119 } 100 120 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 } 121 std::string line = getLineWhichContains(path, "myothertestoutput"); 122 EXPECT_FALSE(line.empty()); 123 EXPECT_TRUE(std::isdigit(line[0])); 124 EXPECT_TRUE(std::isdigit(line[1])); 125 EXPECT_EQ(':', line[2]); 126 EXPECT_TRUE(std::isdigit(line[3])); 127 EXPECT_TRUE(std::isdigit(line[4])); 128 EXPECT_EQ(':', line[5]); 129 EXPECT_TRUE(std::isdigit(line[6])); 130 EXPECT_TRUE(std::isdigit(line[7])); 131 EXPECT_EQ(' ', line[8]); 132 } 133 134 void deleteAllLogFiles() 135 { 136 std::string path; 137 { 138 LogWriter writer; 139 path = writer.getPath(); 140 } 141 142 deleteFile(path); 143 deleteFile(path + ".1"); 144 deleteFile(path + ".2"); 145 deleteFile(path + ".3"); 146 deleteFile(path + ".4"); 147 deleteFile(path + ".5"); 148 deleteFile(path + ".6"); 149 deleteFile(path + ".7"); 150 deleteFile(path + ".8"); 151 deleteFile(path + ".9"); 152 } 153 154 TEST(LogWriterTest, ArchivesOldLogFile) 155 { 156 deleteAllLogFiles(); 157 158 std::string path; 159 160 { 161 MockLogWriter writer; 162 path = writer.getPath(); 163 writer.printLine("test1", level::message); 164 } 165 166 EXPECT_TRUE(fileExists(path)); 167 EXPECT_TRUE(fileContains(path, "test1")); 168 EXPECT_FALSE(fileExists(path + ".1")); 169 EXPECT_FALSE(fileExists(path + ".2")); 170 EXPECT_FALSE(fileExists(path + ".3")); 171 172 { 173 MockLogWriter writer; 174 writer.printLine("test2", level::message); 175 } 176 177 EXPECT_TRUE(fileExists(path)); 178 EXPECT_TRUE(fileContains(path, "test2")); 179 EXPECT_TRUE(fileExists(path + ".1")); 180 EXPECT_TRUE(fileContains(path + ".1", "test1")); 181 EXPECT_FALSE(fileExists(path + ".2")); 182 EXPECT_FALSE(fileExists(path + ".3")); 183 184 { 185 MockLogWriter writer; 186 writer.printLine("test3", level::message); 187 } 188 189 EXPECT_TRUE(fileExists(path)); 190 EXPECT_TRUE(fileContains(path, "test3")); 191 EXPECT_TRUE(fileExists(path + ".1")); 192 EXPECT_TRUE(fileContains(path + ".1", "test2")); 193 EXPECT_TRUE(fileExists(path + ".2")); 194 EXPECT_TRUE(fileContains(path + ".2", "test1")); 195 EXPECT_FALSE(fileExists(path + ".3")); 196 } 197 198 TEST(LogWriterTest, ArchivesNineLogFiles) 199 { 200 deleteAllLogFiles(); 201 202 std::string path; 203 for (int i = 0; i < 20; ++i) 204 { 205 MockLogWriter writer; 206 path = writer.getPath(); 207 writer.printLine("test" + multi_cast<std::string>(i), level::message); 208 } 209 210 EXPECT_TRUE(fileContains(path, "test19")); 211 EXPECT_TRUE(fileContains(path + ".1", "test18")); 212 EXPECT_TRUE(fileContains(path + ".2", "test17")); 213 EXPECT_TRUE(fileContains(path + ".3", "test16")); 214 EXPECT_TRUE(fileContains(path + ".4", "test15")); 215 EXPECT_TRUE(fileContains(path + ".5", "test14")); 216 EXPECT_TRUE(fileContains(path + ".6", "test13")); 217 EXPECT_TRUE(fileContains(path + ".7", "test12")); 218 EXPECT_TRUE(fileContains(path + ".8", "test11")); 219 EXPECT_TRUE(fileContains(path + ".9", "test10")); 220 EXPECT_FALSE(fileExists(path + ".10")); 221 EXPECT_FALSE(fileExists(path + ".11")); 131 222 } 132 223 }
Note: See TracChangeset
for help on using the changeset viewer.