Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit b39616b

Browse files
committed
Split ninja_test.cc into state_test.cc and disk_interface_test.cc
Signed-off-by: Thiago Farina <tfarina@chromium.org>
1 parent f8c98ed commit b39616b

File tree

4 files changed

+151
-144
lines changed

4 files changed

+151
-144
lines changed

configure.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,17 @@ def cxx(name, **kwargs):
172172
test_libs = libs + ['-lgtest_main', '-lgtest']
173173

174174
objs = []
175-
for name in ['build_test', 'build_log_test', 'disk_interface_test',
176-
'eval_env_test', 'graph_test', 'ninja_test', 'parsers_test',
177-
'subprocess_test', 'util_test', 'clean_test', 'test']:
175+
for name in ['build_log_test',
176+
'build_test',
177+
'clean_test',
178+
'disk_interface_test',
179+
'eval_env_test',
180+
'graph_test',
181+
'parsers_test',
182+
'state_test',
183+
'subprocess_test',
184+
'test',
185+
'util_test']:
178186
objs += cxx(name, variables=[('cflags', test_cflags)])
179187

180188
if platform != 'mingw':

src/disk_interface_test.cc

+99
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#endif
2121

2222
#include "disk_interface.h"
23+
#include "graph.h"
24+
#include "test.h"
2325

2426
using namespace std;
2527

@@ -157,4 +159,101 @@ TEST_F(DiskInterfaceTest, RemoveFile) {
157159
EXPECT_EQ(1, disk_.RemoveFile("does not exist"));
158160
}
159161

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+
160259
} // namespace

src/ninja_test.cc

-141
This file was deleted.

src/state_test.cc

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2011 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <gtest/gtest.h>
16+
17+
#include "graph.h"
18+
#include "state.h"
19+
20+
namespace {
21+
22+
TEST(State, Basic) {
23+
State state;
24+
Rule* rule = new Rule("cat");
25+
string err;
26+
EXPECT_TRUE(rule->ParseCommand("cat $in > $out", &err));
27+
ASSERT_EQ("", err);
28+
state.AddRule(rule);
29+
Edge* edge = state.AddEdge(rule);
30+
state.AddIn(edge, "in1");
31+
state.AddIn(edge, "in2");
32+
state.AddOut(edge, "out");
33+
34+
EXPECT_EQ("cat in1 in2 > out", edge->EvaluateCommand());
35+
36+
EXPECT_FALSE(state.GetNode("in1")->dirty());
37+
EXPECT_FALSE(state.GetNode("in2")->dirty());
38+
EXPECT_FALSE(state.GetNode("out")->dirty());
39+
}
40+
41+
} // namespace

0 commit comments

Comments
 (0)