Ninja
ninja_test.cc
Go to the documentation of this file.
1 // Copyright 2013 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 <stdarg.h>
16 #include <stdio.h>
17 
18 #include "gtest/gtest.h"
19 #include "line_printer.h"
20 
21 string StringPrintf(const char* format, ...) {
22  const int N = 1024;
23  char buf[N];
24 
25  va_list ap;
26  va_start(ap, format);
27  vsnprintf(buf, N, format, ap);
28  va_end(ap);
29 
30  return buf;
31 }
32 
33 /// A test result printer that's less wordy than gtest's default.
34 struct LaconicPrinter : public testing::EmptyTestEventListener {
36  virtual void OnTestProgramStart(const testing::UnitTest& unit_test) {
37  test_count_ = unit_test.test_to_run_count();
38  }
39 
40  virtual void OnTestIterationStart(const testing::UnitTest& test_info,
41  int iteration) {
42  tests_started_ = 0;
43  iteration_ = iteration;
44  }
45 
46  virtual void OnTestStart(const testing::TestInfo& test_info) {
49  StringPrintf("[%d/%d%s] %s.%s",
52  iteration_ ? StringPrintf(" iter %d", iteration_).c_str()
53  : "",
54  test_info.test_case_name(),
55  test_info.name()),
57  }
58 
59  virtual void OnTestPartResult(
60  const testing::TestPartResult& test_part_result) {
61  if (!test_part_result.failed())
62  return;
64  "*** Failure in %s:%d\n%s\n", test_part_result.file_name(),
65  test_part_result.line_number(), test_part_result.summary()));
66  }
67 
68  virtual void OnTestProgramEnd(const testing::UnitTest& unit_test) {
69  printer_.PrintOnNewLine(unit_test.Passed() ? "passed\n" : "failed\n");
70  }
71 
72  private:
77 };
78 
79 int main(int argc, char **argv) {
80  testing::InitGoogleTest(&argc, argv);
81 
82  testing::TestEventListeners& listeners =
83  testing::UnitTest::GetInstance()->listeners();
84  delete listeners.Release(listeners.default_result_printer());
85  listeners.Append(new LaconicPrinter);
86 
87  return RUN_ALL_TESTS();
88 }
virtual void OnTestStart(const testing::TestInfo &test_info)
Definition: ninja_test.cc:46
virtual void OnTestIterationStart(const testing::UnitTest &test_info, int iteration)
Definition: ninja_test.cc:40
A test result printer that's less wordy than gtest's default.
Definition: ninja_test.cc:34
void PrintOnNewLine(const string &to_print)
Prints a string on a new line, not overprinting previous output.
virtual void OnTestProgramStart(const testing::UnitTest &unit_test)
Definition: ninja_test.cc:36
virtual void OnTestPartResult(const testing::TestPartResult &test_part_result)
Definition: ninja_test.cc:59
LinePrinter printer_
Definition: ninja_test.cc:73
int main(int argc, char **argv)
Definition: ninja_test.cc:79
string StringPrintf(const char *format,...)
Definition: ninja_test.cc:21
virtual void OnTestProgramEnd(const testing::UnitTest &unit_test)
Definition: ninja_test.cc:68
Prints lines of text, possibly overprinting previously printed lines if the terminal supports it...
Definition: line_printer.h:23
void Print(string to_print, LineType type)
Overprints the current line.
Definition: line_printer.cc:45