Ninja
ninja_test.cc
Go to the documentation of this file.
00001 // Copyright 2013 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include <stdarg.h>
00016 #include <stdio.h>
00017 
00018 #include "gtest/gtest.h"
00019 #include "line_printer.h"
00020 
00021 string StringPrintf(const char* format, ...) {
00022   const int N = 1024;
00023   char buf[N];
00024 
00025   va_list ap;
00026   va_start(ap, format);
00027   vsnprintf(buf, N, format, ap);
00028   va_end(ap);
00029 
00030   return buf;
00031 }
00032 
00033 /// A test result printer that's less wordy than gtest's default.
00034 struct LaconicPrinter : public testing::EmptyTestEventListener {
00035   LaconicPrinter() : tests_started_(0), test_count_(0), iteration_(0) {}
00036   virtual void OnTestProgramStart(const testing::UnitTest& unit_test) {
00037     test_count_ = unit_test.test_to_run_count();
00038   }
00039 
00040   virtual void OnTestIterationStart(const testing::UnitTest& test_info,
00041                                     int iteration) {
00042     tests_started_ = 0;
00043     iteration_ = iteration;
00044   }
00045 
00046   virtual void OnTestStart(const testing::TestInfo& test_info) {
00047     ++tests_started_;
00048     printer_.Print(
00049         StringPrintf("[%d/%d%s] %s.%s",
00050                      tests_started_,
00051                      test_count_,
00052                      iteration_ ? StringPrintf(" iter %d", iteration_).c_str()
00053                                 : "",
00054                      test_info.test_case_name(),
00055                      test_info.name()),
00056         LinePrinter::ELIDE);
00057   }
00058 
00059   virtual void OnTestPartResult(
00060       const testing::TestPartResult& test_part_result) {
00061     if (!test_part_result.failed())
00062       return;
00063     printer_.PrintOnNewLine(StringPrintf(
00064         "*** Failure in %s:%d\n%s\n", test_part_result.file_name(),
00065         test_part_result.line_number(), test_part_result.summary()));
00066   }
00067 
00068   virtual void OnTestProgramEnd(const testing::UnitTest& unit_test) {
00069     printer_.PrintOnNewLine(unit_test.Passed() ? "passed\n" : "failed\n");
00070   }
00071 
00072  private:
00073   LinePrinter printer_;
00074   int tests_started_;
00075   int test_count_;
00076   int iteration_;
00077 };
00078 
00079 int main(int argc, char **argv) {
00080   testing::InitGoogleTest(&argc, argv);
00081 
00082   testing::TestEventListeners& listeners =
00083       testing::UnitTest::GetInstance()->listeners();
00084   delete listeners.Release(listeners.default_result_printer());
00085   listeners.Append(new LaconicPrinter);
00086 
00087   return RUN_ALL_TESTS();
00088 }