media/webrtc/trunk/testing/gtest/samples/sample9_unittest.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/samples/sample9_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,160 @@
     1.4 +// Copyright 2009 Google Inc. All Rights Reserved.
     1.5 +//
     1.6 +// Redistribution and use in source and binary forms, with or without
     1.7 +// modification, are permitted provided that the following conditions are
     1.8 +// met:
     1.9 +//
    1.10 +//     * Redistributions of source code must retain the above copyright
    1.11 +// notice, this list of conditions and the following disclaimer.
    1.12 +//     * Redistributions in binary form must reproduce the above
    1.13 +// copyright notice, this list of conditions and the following disclaimer
    1.14 +// in the documentation and/or other materials provided with the
    1.15 +// distribution.
    1.16 +//     * Neither the name of Google Inc. nor the names of its
    1.17 +// contributors may be used to endorse or promote products derived from
    1.18 +// this software without specific prior written permission.
    1.19 +//
    1.20 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.21 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.22 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.23 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.24 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.25 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.26 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.27 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.28 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.29 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.30 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.31 +//
    1.32 +// Author: vladl@google.com (Vlad Losev)
    1.33 +
    1.34 +// This sample shows how to use Google Test listener API to implement
    1.35 +// an alternative console output and how to use the UnitTest reflection API
    1.36 +// to enumerate test cases and tests and to inspect their results.
    1.37 +
    1.38 +#include <stdio.h>
    1.39 +
    1.40 +#include "gtest/gtest.h"
    1.41 +
    1.42 +using ::testing::EmptyTestEventListener;
    1.43 +using ::testing::InitGoogleTest;
    1.44 +using ::testing::Test;
    1.45 +using ::testing::TestCase;
    1.46 +using ::testing::TestEventListeners;
    1.47 +using ::testing::TestInfo;
    1.48 +using ::testing::TestPartResult;
    1.49 +using ::testing::UnitTest;
    1.50 +
    1.51 +namespace {
    1.52 +
    1.53 +// Provides alternative output mode which produces minimal amount of
    1.54 +// information about tests.
    1.55 +class TersePrinter : public EmptyTestEventListener {
    1.56 + private:
    1.57 +  // Called before any test activity starts.
    1.58 +  virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {}
    1.59 +
    1.60 +  // Called after all test activities have ended.
    1.61 +  virtual void OnTestProgramEnd(const UnitTest& unit_test) {
    1.62 +    fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
    1.63 +    fflush(stdout);
    1.64 +  }
    1.65 +
    1.66 +  // Called before a test starts.
    1.67 +  virtual void OnTestStart(const TestInfo& test_info) {
    1.68 +    fprintf(stdout,
    1.69 +            "*** Test %s.%s starting.\n",
    1.70 +            test_info.test_case_name(),
    1.71 +            test_info.name());
    1.72 +    fflush(stdout);
    1.73 +  }
    1.74 +
    1.75 +  // Called after a failed assertion or a SUCCEED() invocation.
    1.76 +  virtual void OnTestPartResult(const TestPartResult& test_part_result) {
    1.77 +    fprintf(stdout,
    1.78 +            "%s in %s:%d\n%s\n",
    1.79 +            test_part_result.failed() ? "*** Failure" : "Success",
    1.80 +            test_part_result.file_name(),
    1.81 +            test_part_result.line_number(),
    1.82 +            test_part_result.summary());
    1.83 +    fflush(stdout);
    1.84 +  }
    1.85 +
    1.86 +  // Called after a test ends.
    1.87 +  virtual void OnTestEnd(const TestInfo& test_info) {
    1.88 +    fprintf(stdout,
    1.89 +            "*** Test %s.%s ending.\n",
    1.90 +            test_info.test_case_name(),
    1.91 +            test_info.name());
    1.92 +    fflush(stdout);
    1.93 +  }
    1.94 +};  // class TersePrinter
    1.95 +
    1.96 +TEST(CustomOutputTest, PrintsMessage) {
    1.97 +  printf("Printing something from the test body...\n");
    1.98 +}
    1.99 +
   1.100 +TEST(CustomOutputTest, Succeeds) {
   1.101 +  SUCCEED() << "SUCCEED() has been invoked from here";
   1.102 +}
   1.103 +
   1.104 +TEST(CustomOutputTest, Fails) {
   1.105 +  EXPECT_EQ(1, 2)
   1.106 +      << "This test fails in order to demonstrate alternative failure messages";
   1.107 +}
   1.108 +
   1.109 +}  // namespace
   1.110 +
   1.111 +int main(int argc, char **argv) {
   1.112 +  InitGoogleTest(&argc, argv);
   1.113 +
   1.114 +  bool terse_output = false;
   1.115 +  if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
   1.116 +    terse_output = true;
   1.117 +  else
   1.118 +    printf("%s\n", "Run this program with --terse_output to change the way "
   1.119 +           "it prints its output.");
   1.120 +
   1.121 +  UnitTest& unit_test = *UnitTest::GetInstance();
   1.122 +
   1.123 +  // If we are given the --terse_output command line flag, suppresses the
   1.124 +  // standard output and attaches own result printer.
   1.125 +  if (terse_output) {
   1.126 +    TestEventListeners& listeners = unit_test.listeners();
   1.127 +
   1.128 +    // Removes the default console output listener from the list so it will
   1.129 +    // not receive events from Google Test and won't print any output. Since
   1.130 +    // this operation transfers ownership of the listener to the caller we
   1.131 +    // have to delete it as well.
   1.132 +    delete listeners.Release(listeners.default_result_printer());
   1.133 +
   1.134 +    // Adds the custom output listener to the list. It will now receive
   1.135 +    // events from Google Test and print the alternative output. We don't
   1.136 +    // have to worry about deleting it since Google Test assumes ownership
   1.137 +    // over it after adding it to the list.
   1.138 +    listeners.Append(new TersePrinter);
   1.139 +  }
   1.140 +  int ret_val = RUN_ALL_TESTS();
   1.141 +
   1.142 +  // This is an example of using the UnitTest reflection API to inspect test
   1.143 +  // results. Here we discount failures from the tests we expected to fail.
   1.144 +  int unexpectedly_failed_tests = 0;
   1.145 +  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
   1.146 +    const TestCase& test_case = *unit_test.GetTestCase(i);
   1.147 +    for (int j = 0; j < test_case.total_test_count(); ++j) {
   1.148 +      const TestInfo& test_info = *test_case.GetTestInfo(j);
   1.149 +      // Counts failed tests that were not meant to fail (those without
   1.150 +      // 'Fails' in the name).
   1.151 +      if (test_info.result()->Failed() &&
   1.152 +          strcmp(test_info.name(), "Fails") != 0) {
   1.153 +        unexpectedly_failed_tests++;
   1.154 +      }
   1.155 +    }
   1.156 +  }
   1.157 +
   1.158 +  // Test that were meant to fail should not affect the test program outcome.
   1.159 +  if (unexpectedly_failed_tests == 0)
   1.160 +    ret_val = 0;
   1.161 +
   1.162 +  return ret_val;
   1.163 +}

mercurial