Blame nss/external_tests/google_test/gtest/samples/sample9_unittest.cc

Packit 40b132
// Copyright 2009 Google Inc. All Rights Reserved.
Packit 40b132
//
Packit 40b132
// Redistribution and use in source and binary forms, with or without
Packit 40b132
// modification, are permitted provided that the following conditions are
Packit 40b132
// met:
Packit 40b132
//
Packit 40b132
//     * Redistributions of source code must retain the above copyright
Packit 40b132
// notice, this list of conditions and the following disclaimer.
Packit 40b132
//     * Redistributions in binary form must reproduce the above
Packit 40b132
// copyright notice, this list of conditions and the following disclaimer
Packit 40b132
// in the documentation and/or other materials provided with the
Packit 40b132
// distribution.
Packit 40b132
//     * Neither the name of Google Inc. nor the names of its
Packit 40b132
// contributors may be used to endorse or promote products derived from
Packit 40b132
// this software without specific prior written permission.
Packit 40b132
//
Packit 40b132
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 40b132
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 40b132
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 40b132
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 40b132
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 40b132
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 40b132
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 40b132
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 40b132
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 40b132
//
Packit 40b132
// Author: vladl@google.com (Vlad Losev)
Packit 40b132
Packit 40b132
// This sample shows how to use Google Test listener API to implement
Packit 40b132
// an alternative console output and how to use the UnitTest reflection API
Packit 40b132
// to enumerate test cases and tests and to inspect their results.
Packit 40b132
Packit 40b132
#include <stdio.h>
Packit 40b132
Packit 40b132
#include "gtest/gtest.h"
Packit 40b132
Packit 40b132
using ::testing::EmptyTestEventListener;
Packit 40b132
using ::testing::InitGoogleTest;
Packit 40b132
using ::testing::Test;
Packit 40b132
using ::testing::TestCase;
Packit 40b132
using ::testing::TestEventListeners;
Packit 40b132
using ::testing::TestInfo;
Packit 40b132
using ::testing::TestPartResult;
Packit 40b132
using ::testing::UnitTest;
Packit 40b132
Packit 40b132
namespace {
Packit 40b132
Packit 40b132
// Provides alternative output mode which produces minimal amount of
Packit 40b132
// information about tests.
Packit 40b132
class TersePrinter : public EmptyTestEventListener {
Packit 40b132
 private:
Packit 40b132
  // Called before any test activity starts.
Packit 40b132
  virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {}
Packit 40b132
Packit 40b132
  // Called after all test activities have ended.
Packit 40b132
  virtual void OnTestProgramEnd(const UnitTest& unit_test) {
Packit 40b132
    fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
Packit 40b132
    fflush(stdout);
Packit 40b132
  }
Packit 40b132
Packit 40b132
  // Called before a test starts.
Packit 40b132
  virtual void OnTestStart(const TestInfo& test_info) {
Packit 40b132
    fprintf(stdout,
Packit 40b132
            "*** Test %s.%s starting.\n",
Packit 40b132
            test_info.test_case_name(),
Packit 40b132
            test_info.name());
Packit 40b132
    fflush(stdout);
Packit 40b132
  }
Packit 40b132
Packit 40b132
  // Called after a failed assertion or a SUCCEED() invocation.
Packit 40b132
  virtual void OnTestPartResult(const TestPartResult& test_part_result) {
Packit 40b132
    fprintf(stdout,
Packit 40b132
            "%s in %s:%d\n%s\n",
Packit 40b132
            test_part_result.failed() ? "*** Failure" : "Success",
Packit 40b132
            test_part_result.file_name(),
Packit 40b132
            test_part_result.line_number(),
Packit 40b132
            test_part_result.summary());
Packit 40b132
    fflush(stdout);
Packit 40b132
  }
Packit 40b132
Packit 40b132
  // Called after a test ends.
Packit 40b132
  virtual void OnTestEnd(const TestInfo& test_info) {
Packit 40b132
    fprintf(stdout,
Packit 40b132
            "*** Test %s.%s ending.\n",
Packit 40b132
            test_info.test_case_name(),
Packit 40b132
            test_info.name());
Packit 40b132
    fflush(stdout);
Packit 40b132
  }
Packit 40b132
};  // class TersePrinter
Packit 40b132
Packit 40b132
TEST(CustomOutputTest, PrintsMessage) {
Packit 40b132
  printf("Printing something from the test body...\n");
Packit 40b132
}
Packit 40b132
Packit 40b132
TEST(CustomOutputTest, Succeeds) {
Packit 40b132
  SUCCEED() << "SUCCEED() has been invoked from here";
Packit 40b132
}
Packit 40b132
Packit 40b132
TEST(CustomOutputTest, Fails) {
Packit 40b132
  EXPECT_EQ(1, 2)
Packit 40b132
      << "This test fails in order to demonstrate alternative failure messages";
Packit 40b132
}
Packit 40b132
Packit 40b132
}  // namespace
Packit 40b132
Packit 40b132
int main(int argc, char **argv) {
Packit 40b132
  InitGoogleTest(&argc, argv);
Packit 40b132
Packit 40b132
  bool terse_output = false;
Packit 40b132
  if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
Packit 40b132
    terse_output = true;
Packit 40b132
  else
Packit 40b132
    printf("%s\n", "Run this program with --terse_output to change the way "
Packit 40b132
           "it prints its output.");
Packit 40b132
Packit 40b132
  UnitTest& unit_test = *UnitTest::GetInstance();
Packit 40b132
Packit 40b132
  // If we are given the --terse_output command line flag, suppresses the
Packit 40b132
  // standard output and attaches own result printer.
Packit 40b132
  if (terse_output) {
Packit 40b132
    TestEventListeners& listeners = unit_test.listeners();
Packit 40b132
Packit 40b132
    // Removes the default console output listener from the list so it will
Packit 40b132
    // not receive events from Google Test and won't print any output. Since
Packit 40b132
    // this operation transfers ownership of the listener to the caller we
Packit 40b132
    // have to delete it as well.
Packit 40b132
    delete listeners.Release(listeners.default_result_printer());
Packit 40b132
Packit 40b132
    // Adds the custom output listener to the list. It will now receive
Packit 40b132
    // events from Google Test and print the alternative output. We don't
Packit 40b132
    // have to worry about deleting it since Google Test assumes ownership
Packit 40b132
    // over it after adding it to the list.
Packit 40b132
    listeners.Append(new TersePrinter);
Packit 40b132
  }
Packit 40b132
  int ret_val = RUN_ALL_TESTS();
Packit 40b132
Packit 40b132
  // This is an example of using the UnitTest reflection API to inspect test
Packit 40b132
  // results. Here we discount failures from the tests we expected to fail.
Packit 40b132
  int unexpectedly_failed_tests = 0;
Packit 40b132
  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
Packit 40b132
    const TestCase& test_case = *unit_test.GetTestCase(i);
Packit 40b132
    for (int j = 0; j < test_case.total_test_count(); ++j) {
Packit 40b132
      const TestInfo& test_info = *test_case.GetTestInfo(j);
Packit 40b132
      // Counts failed tests that were not meant to fail (those without
Packit 40b132
      // 'Fails' in the name).
Packit 40b132
      if (test_info.result()->Failed() &&
Packit 40b132
          strcmp(test_info.name(), "Fails") != 0) {
Packit 40b132
        unexpectedly_failed_tests++;
Packit 40b132
      }
Packit 40b132
    }
Packit 40b132
  }
Packit 40b132
Packit 40b132
  // Test that were meant to fail should not affect the test program outcome.
Packit 40b132
  if (unexpectedly_failed_tests == 0)
Packit 40b132
    ret_val = 0;
Packit 40b132
Packit 40b132
  return ret_val;
Packit 40b132
}