Ninja
test.h
Go to the documentation of this file.
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 #ifndef NINJA_TEST_H_
16 #define NINJA_TEST_H_
17 
18 #include "disk_interface.h"
19 #include "state.h"
20 #include "util.h"
21 
22 // A tiny testing framework inspired by googletest, but much simpler and
23 // faster to compile. It supports most things commonly used from googltest. The
24 // most noticeable things missing: EXPECT_* and ASSERT_* don't support
25 // streaming notes to them with operator<<, and for failing tests the lhs and
26 // rhs are not printed. That's so that this header does not have to include
27 // sstream, which slows down building ninja_test almost 20%.
28 namespace testing {
29 class Test {
30  bool failed_;
32  public:
33  Test() : failed_(false), assertion_failures_(0) {}
34  virtual ~Test() {}
35  virtual void SetUp() {}
36  virtual void TearDown() {}
37  virtual void Run() = 0;
38 
39  bool Failed() const { return failed_; }
40  int AssertionFailures() const { return assertion_failures_; }
42  bool Check(bool condition, const char* file, int line, const char* error);
43 };
44 }
45 
46 void RegisterTest(testing::Test* (*)(), const char*);
47 
49 #define TEST_F_(x, y, name) \
50  struct y : public x { \
51  static testing::Test* Create() { return g_current_test = new y; } \
52  virtual void Run(); \
53  }; \
54  struct Register##y { \
55  Register##y() { RegisterTest(y::Create, name); } \
56  }; \
57  Register##y g_register_##y; \
58  void y::Run()
59 
60 #define TEST_F(x, y) TEST_F_(x, x##y, #x "." #y)
61 #define TEST(x, y) TEST_F_(testing::Test, x##y, #x "." #y)
62 
63 #define EXPECT_EQ(a, b) \
64  g_current_test->Check(a == b, __FILE__, __LINE__, #a " == " #b)
65 #define EXPECT_NE(a, b) \
66  g_current_test->Check(a != b, __FILE__, __LINE__, #a " != " #b)
67 #define EXPECT_GT(a, b) \
68  g_current_test->Check(a > b, __FILE__, __LINE__, #a " > " #b)
69 #define EXPECT_LT(a, b) \
70  g_current_test->Check(a < b, __FILE__, __LINE__, #a " < " #b)
71 #define EXPECT_GE(a, b) \
72  g_current_test->Check(a >= b, __FILE__, __LINE__, #a " >= " #b)
73 #define EXPECT_LE(a, b) \
74  g_current_test->Check(a <= b, __FILE__, __LINE__, #a " <= " #b)
75 #define EXPECT_TRUE(a) \
76  g_current_test->Check(static_cast<bool>(a), __FILE__, __LINE__, #a)
77 #define EXPECT_FALSE(a) \
78  g_current_test->Check(!static_cast<bool>(a), __FILE__, __LINE__, #a)
79 
80 #define ASSERT_EQ(a, b) \
81  if (!EXPECT_EQ(a, b)) { g_current_test->AddAssertionFailure(); return; }
82 #define ASSERT_NE(a, b) \
83  if (!EXPECT_NE(a, b)) { g_current_test->AddAssertionFailure(); return; }
84 #define ASSERT_GT(a, b) \
85  if (!EXPECT_GT(a, b)) { g_current_test->AddAssertionFailure(); return; }
86 #define ASSERT_LT(a, b) \
87  if (!EXPECT_LT(a, b)) { g_current_test->AddAssertionFailure(); return; }
88 #define ASSERT_GE(a, b) \
89  if (!EXPECT_GE(a, b)) { g_current_test->AddAssertionFailure(); return; }
90 #define ASSERT_LE(a, b) \
91  if (!EXPECT_LE(a, b)) { g_current_test->AddAssertionFailure(); return; }
92 #define ASSERT_TRUE(a) \
93  if (!EXPECT_TRUE(a)) { g_current_test->AddAssertionFailure(); return; }
94 #define ASSERT_FALSE(a) \
95  if (!EXPECT_FALSE(a)) { g_current_test->AddAssertionFailure(); return; }
96 #define ASSERT_NO_FATAL_FAILURE(a) \
97  { \
98  int f = g_current_test->AssertionFailures(); \
99  a; \
100  if (f != g_current_test->AssertionFailures()) { \
101  g_current_test->AddAssertionFailure(); \
102  return; \
103  } \
104  }
105 
106 // Support utilites for tests.
107 
108 struct Node;
109 
110 /// A base test fixture that includes a State object with a
111 /// builtin "cat" rule.
114 
115  /// Add a "cat" rule to \a state. Used by some tests; it's
116  /// otherwise done by the ctor to state_.
117  void AddCatRule(State* state);
118 
119  /// Short way to get a Node by its path from state_.
120  Node* GetNode(const string& path);
121 
123 };
124 
125 void AssertParse(State* state, const char* input);
126 void AssertHash(const char* expected, uint64_t actual);
127 
128 /// An implementation of DiskInterface that uses an in-memory representation
129 /// of disk state. It also logs file accesses and directory creations
130 /// so it can be used by tests to verify disk access patterns.
133 
134  /// "Create" a file with contents.
135  void Create(const string& path, const string& contents);
136 
137  /// Tick "time" forwards; subsequent file operations will be newer than
138  /// previous ones.
139  int Tick() {
140  return ++now_;
141  }
142 
143  // DiskInterface
144  virtual TimeStamp Stat(const string& path) const;
145  virtual bool WriteFile(const string& path, const string& contents);
146  virtual bool MakeDir(const string& path);
147  virtual string ReadFile(const string& path, string* err);
148  virtual int RemoveFile(const string& path);
149 
150  /// An entry for a single in-memory file.
151  struct Entry {
152  int mtime;
153  string contents;
154  };
155 
156  vector<string> directories_made_;
157  vector<string> files_read_;
158  typedef map<string, Entry> FileMap;
160  set<string> files_removed_;
161  set<string> files_created_;
162 
163  /// A simple fake timestamp for file operations.
164  int now_;
165 };
166 
168  /// Create a temporary directory and chdir into it.
169  void CreateAndEnter(const string& name);
170 
171  /// Clean up the temporary directory.
172  void Cleanup();
173 
174  /// The temp directory containing our dir.
175  string start_dir_;
176  /// The subdirectory name for our dir, or empty if it hasn't been set up.
178 };
179 
180 #endif // NINJA_TEST_H_
map< string, Entry > FileMap
Definition: test.h:158
FileMap files_
Definition: test.h:159
An implementation of DiskInterface that uses an in-memory representation of disk state.
Definition: test.h:131
Node * GetNode(const string &path)
Short way to get a Node by its path from state_.
Definition: test.cc:91
int Tick()
Tick "time" forwards; subsequent file operations will be newer than previous ones.
Definition: test.h:139
bool failed_
Definition: test.h:30
virtual void SetUp()
Definition: test.h:35
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
Definition: graph.h:35
set< string > files_removed_
Definition: test.h:160
virtual bool WriteFile(const string &path, const string &contents)
Create a file, with the specified name and contents Returns true on success, false on failure...
Definition: test.cc:121
Interface for accessing the disk.
int now_
A simple fake timestamp for file operations.
Definition: test.h:164
int TimeStamp
Definition: timestamp.h:22
void Create(const string &path, const string &contents)
"Create" a file with contents.
Definition: test.cc:107
virtual void TearDown()
Definition: test.h:36
set< string > files_created_
Definition: test.h:161
vector< string > directories_made_
Definition: test.h:156
A base test fixture that includes a State object with a builtin "cat" rule.
Definition: test.h:112
void AssertHash(const char *expected, uint64_t actual)
Definition: test.cc:103
int assertion_failures_
Definition: test.h:31
void AddCatRule(State *state)
Add a "cat" rule to state.
Definition: test.cc:85
bool Failed() const
Definition: test.h:39
testing::Test * g_current_test
Definition: ninja_test.cc:37
virtual ~Test()
Definition: test.h:34
string start_dir_
The temp directory containing our dir.
Definition: test.h:175
VirtualFileSystem()
Definition: test.h:132
void AddAssertionFailure()
Definition: test.h:41
virtual bool MakeDir(const string &path)
Create a directory, returning false on failure.
Definition: test.cc:126
virtual TimeStamp Stat(const string &path) const
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
Definition: test.cc:114
bool Check(bool condition, const char *file, int line, const char *error)
Definition: ninja_test.cc:115
void Cleanup()
Clean up the temporary directory.
Definition: test.cc:175
An entry for a single in-memory file.
Definition: test.h:151
void CreateAndEnter(const string &name)
Create a temporary directory and chdir into it.
Definition: test.cc:153
virtual string ReadFile(const string &path, string *err)
Read a file to a string. Fill in |err| on error.
Definition: test.cc:131
void AssertParse(State *state, const char *input)
Definition: test.cc:96
Global state (file status, loaded rules) for a single run.
Definition: state.h:83
vector< string > files_read_
Definition: test.h:157
int AssertionFailures() const
Definition: test.h:40
virtual int RemoveFile(const string &path)
Remove the file named path.
Definition: test.cc:139
unsigned long long uint64_t
Definition: win32port.h:22
string temp_dir_name_
The subdirectory name for our dir, or empty if it hasn't been set up.
Definition: test.h:177
void RegisterTest(testing::Test *(*)(), const char *)
virtual void Run()=0