From fb0ad5f77bcd689b2cdbdf8920f3dae1f5abc5d2 Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Sun, 17 Oct 2010 10:06:51 -0700 Subject: [PATCH] drop gmock, better test factoring --- Makefile | 2 +- ninja.h | 4 +-- ninja_test.cc | 79 +++++++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 2fad958..a22651f 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CXXFLAGS := -Wall -g all_i_currently_care_about: ninja_test -ninja_test: LDFLAGS = -lgtest -lgmock -lgmock_main -lpthread +ninja_test: LDFLAGS = -lgtest -lgtest_main -lpthread ninja_test: ninja_test.o ninja_test.o: ninja_test.cc ninja.h manifest_parser.h diff --git a/ninja.h b/ninja.h index 300fddc..3c8d6c0 100644 --- a/ninja.h +++ b/ninja.h @@ -274,7 +274,7 @@ Edge* Plan::FindWork() { struct Shell { virtual ~Shell() {} - virtual bool RunCommand(const string& command) = 0; + virtual bool RunCommand(Edge* edge) = 0; }; struct Builder { @@ -292,7 +292,7 @@ struct Builder { bool Builder::Build(Shell* shell, string* err) { while (Edge* edge = plan_.FindWork()) { string command = edge->EvaluateCommand(); - if (!shell->RunCommand(command)) { + if (!shell->RunCommand(edge)) { err->assign("command '" + command + "' failed."); return false; } diff --git a/ninja_test.cc b/ninja_test.cc index 3ad9b09..cb85548 100644 --- a/ninja_test.cc +++ b/ninja_test.cc @@ -79,45 +79,70 @@ TEST(EvalString, OneVariable) { EXPECT_EQ("hi there", str.Evaluate(&env)); } -#include -using ::testing::Return; -using ::testing::_; +struct TestState : public State, public Shell { + TestState() { + LoadManifest(); + } + + void LoadManifest(); + + // shell override + virtual bool RunCommand(Edge* edge); -struct MockShell : public Shell { - MOCK_METHOD1(RunCommand, bool(const string& command)); + int now_; + vector commands_ran_; }; -TEST(Build, OneStep) { - State state; - ManifestParser parser(&state); +void TestState::LoadManifest() { + ManifestParser parser(this); string err; ASSERT_TRUE(parser.Parse( "rule cat\n" "command cat @in > $out\n" "\n" -"build lib: cat in1 in2\n" +"build cat1: cat in1\n" +"build cat2: cat in1 in2\n" "build bin: cat main lib\n", &err)); ASSERT_EQ("", err); +} - { - MockShell shell; - Builder builder(&state); - builder.AddTarget("bin"); - EXPECT_CALL(shell, RunCommand(_)) - .Times(0); - EXPECT_TRUE(builder.Build(&shell, &err)); - EXPECT_EQ("", err); +bool TestState::RunCommand(Edge* edge) { + commands_ran_.push_back(edge->EvaluateCommand()); + if (edge->rule_->name_ == "cat") { + for (vector::iterator out = edge->outputs_.begin(); + out != edge->outputs_.end(); ++out) { + (*out)->file_->Touch(now_); + } + return true; + } else { + printf("unkown command\n"); } - { - MockShell shell; - Builder builder(&state); - state.stat_cache()->GetFile("in1")->Touch(1); - builder.AddTarget("bin"); - EXPECT_CALL(shell, RunCommand("cat in1 in2 > lib")) - .WillOnce(Return(true)); - EXPECT_TRUE(builder.Build(&shell, &err)); - EXPECT_EQ("", err); - } + return false; +} + +TEST(Build, NoWork) { + TestState state; + Builder builder(&state); + builder.AddTarget("bin"); + string err; + EXPECT_TRUE(builder.Build(&state, &err)); + EXPECT_EQ("", err); + EXPECT_EQ(0, state.commands_ran_.size()); +} + +TEST(Build, OneStep) { + // Given a dirtytarget with one ready input, + // we should rebuild the target. + TestState state; + state.stat_cache()->GetFile("cat1")->Touch(1); + Builder builder(&state); + builder.AddTarget("cat1"); + string err; + EXPECT_TRUE(builder.Build(&state, &err)); + EXPECT_EQ("", err); + + ASSERT_EQ(1, state.commands_ran_.size()); + EXPECT_EQ("cat in1 > cat1", state.commands_ran_[0]); } -- 2.7.4