|
20 | 20 | #endif
|
21 | 21 |
|
22 | 22 | #include "disk_interface.h"
|
| 23 | +#include "graph.h" |
| 24 | +#include "test.h" |
23 | 25 |
|
24 | 26 | using namespace std;
|
25 | 27 |
|
@@ -157,4 +159,101 @@ TEST_F(DiskInterfaceTest, RemoveFile) {
|
157 | 159 | EXPECT_EQ(1, disk_.RemoveFile("does not exist"));
|
158 | 160 | }
|
159 | 161 |
|
| 162 | +struct StatTest : public StateTestWithBuiltinRules, |
| 163 | + public DiskInterface { |
| 164 | + // DiskInterface implementation. |
| 165 | + virtual int Stat(const string& path); |
| 166 | + virtual bool MakeDir(const string& path) { |
| 167 | + assert(false); |
| 168 | + return false; |
| 169 | + } |
| 170 | + virtual string ReadFile(const string& path, string* err) { |
| 171 | + assert(false); |
| 172 | + return ""; |
| 173 | + } |
| 174 | + virtual int RemoveFile(const string& path) { |
| 175 | + assert(false); |
| 176 | + return 0; |
| 177 | + } |
| 178 | + |
| 179 | + map<string, time_t> mtimes_; |
| 180 | + vector<string> stats_; |
| 181 | +}; |
| 182 | + |
| 183 | +int StatTest::Stat(const string& path) { |
| 184 | + stats_.push_back(path); |
| 185 | + map<string, time_t>::iterator i = mtimes_.find(path); |
| 186 | + if (i == mtimes_.end()) |
| 187 | + return 0; // File not found. |
| 188 | + return i->second; |
| 189 | +} |
| 190 | + |
| 191 | +TEST_F(StatTest, Simple) { |
| 192 | + ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, |
| 193 | +"build out: cat in\n")); |
| 194 | + |
| 195 | + Node* out = GetNode("out"); |
| 196 | + out->file_->Stat(this); |
| 197 | + ASSERT_EQ(1u, stats_.size()); |
| 198 | + Edge* edge = out->in_edge_; |
| 199 | + edge->RecomputeDirty(NULL, this, NULL); |
| 200 | + ASSERT_EQ(2u, stats_.size()); |
| 201 | + ASSERT_EQ("out", stats_[0]); |
| 202 | + ASSERT_EQ("in", stats_[1]); |
| 203 | +} |
| 204 | + |
| 205 | +TEST_F(StatTest, TwoStep) { |
| 206 | + ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, |
| 207 | +"build out: cat mid\n" |
| 208 | +"build mid: cat in\n")); |
| 209 | + |
| 210 | + Node* out = GetNode("out"); |
| 211 | + out->file_->Stat(this); |
| 212 | + ASSERT_EQ(1u, stats_.size()); |
| 213 | + Edge* edge = out->in_edge_; |
| 214 | + edge->RecomputeDirty(NULL, this, NULL); |
| 215 | + ASSERT_EQ(3u, stats_.size()); |
| 216 | + ASSERT_EQ("out", stats_[0]); |
| 217 | + ASSERT_TRUE(GetNode("out")->dirty_); |
| 218 | + ASSERT_EQ("mid", stats_[1]); |
| 219 | + ASSERT_TRUE(GetNode("mid")->dirty_); |
| 220 | + ASSERT_EQ("in", stats_[2]); |
| 221 | +} |
| 222 | + |
| 223 | +TEST_F(StatTest, Tree) { |
| 224 | + ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, |
| 225 | +"build out: cat mid1 mid2\n" |
| 226 | +"build mid1: cat in11 in12\n" |
| 227 | +"build mid2: cat in21 in22\n")); |
| 228 | + |
| 229 | + Node* out = GetNode("out"); |
| 230 | + out->file_->Stat(this); |
| 231 | + ASSERT_EQ(1u, stats_.size()); |
| 232 | + Edge* edge = out->in_edge_; |
| 233 | + edge->RecomputeDirty(NULL, this, NULL); |
| 234 | + ASSERT_EQ(1u + 6u, stats_.size()); |
| 235 | + ASSERT_EQ("mid1", stats_[1]); |
| 236 | + ASSERT_TRUE(GetNode("mid1")->dirty_); |
| 237 | + ASSERT_EQ("in11", stats_[2]); |
| 238 | +} |
| 239 | + |
| 240 | +TEST_F(StatTest, Middle) { |
| 241 | + ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, |
| 242 | +"build out: cat mid\n" |
| 243 | +"build mid: cat in\n")); |
| 244 | + |
| 245 | + mtimes_["in"] = 1; |
| 246 | + mtimes_["mid"] = 0; // missing |
| 247 | + mtimes_["out"] = 1; |
| 248 | + |
| 249 | + Node* out = GetNode("out"); |
| 250 | + out->file_->Stat(this); |
| 251 | + ASSERT_EQ(1u, stats_.size()); |
| 252 | + Edge* edge = out->in_edge_; |
| 253 | + edge->RecomputeDirty(NULL, this, NULL); |
| 254 | + ASSERT_FALSE(GetNode("in")->dirty_); |
| 255 | + ASSERT_TRUE(GetNode("mid")->dirty_); |
| 256 | + ASSERT_TRUE(GetNode("out")->dirty_); |
| 257 | +} |
| 258 | + |
160 | 259 | } // namespace
|
0 commit comments