drop gmock, better test factoring
authorEvan Martin <martine@danga.com>
Sun, 17 Oct 2010 17:06:51 +0000 (10:06 -0700)
committerEvan Martin <martine@danga.com>
Sun, 17 Oct 2010 17:06:51 +0000 (10:06 -0700)
Makefile
ninja.h
ninja_test.cc

index 2fad958..a22651f 100644 (file)
--- 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 (file)
--- 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;
     }
index 3ad9b09..cb85548 100644 (file)
@@ -79,45 +79,70 @@ TEST(EvalString, OneVariable) {
   EXPECT_EQ("hi there", str.Evaluate(&env));
 }
 
-#include <gmock/gmock.h>
-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<string> 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<Node*>::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]);
 }