gtest_environment_test.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 //
00032 // Tests using global test environments.
00033 
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include "gtest/gtest.h"
00037 
00038 #define GTEST_IMPLEMENTATION_ 1  // Required for the next #include.
00039 #include "src/gtest-internal-inl.h"
00040 #undef GTEST_IMPLEMENTATION_
00041 
00042 namespace testing {
00043 GTEST_DECLARE_string_(filter);
00044 }
00045 
00046 namespace {
00047 
00048 enum FailureType {
00049   NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
00050 };
00051 
00052 // For testing using global test environments.
00053 class MyEnvironment : public testing::Environment {
00054  public:
00055   MyEnvironment() { Reset(); }
00056 
00057   // Depending on the value of failure_in_set_up_, SetUp() will
00058   // generate a non-fatal failure, generate a fatal failure, or
00059   // succeed.
00060   virtual void SetUp() {
00061     set_up_was_run_ = true;
00062 
00063     switch (failure_in_set_up_) {
00064       case NON_FATAL_FAILURE:
00065         ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
00066         break;
00067       case FATAL_FAILURE:
00068         FAIL() << "Expected fatal failure in global set-up.";
00069         break;
00070       default:
00071         break;
00072     }
00073   }
00074 
00075   // Generates a non-fatal failure.
00076   virtual void TearDown() {
00077     tear_down_was_run_ = true;
00078     ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
00079   }
00080 
00081   // Resets the state of the environment s.t. it can be reused.
00082   void Reset() {
00083     failure_in_set_up_ = NO_FAILURE;
00084     set_up_was_run_ = false;
00085     tear_down_was_run_ = false;
00086   }
00087 
00088   // We call this function to set the type of failure SetUp() should
00089   // generate.
00090   void set_failure_in_set_up(FailureType type) {
00091     failure_in_set_up_ = type;
00092   }
00093 
00094   // Was SetUp() run?
00095   bool set_up_was_run() const { return set_up_was_run_; }
00096 
00097   // Was TearDown() run?
00098   bool tear_down_was_run() const { return tear_down_was_run_; }
00099 
00100  private:
00101   FailureType failure_in_set_up_;
00102   bool set_up_was_run_;
00103   bool tear_down_was_run_;
00104 };
00105 
00106 // Was the TEST run?
00107 bool test_was_run;
00108 
00109 // The sole purpose of this TEST is to enable us to check whether it
00110 // was run.
00111 TEST(FooTest, Bar) {
00112   test_was_run = true;
00113 }
00114 
00115 // Prints the message and aborts the program if condition is false.
00116 void Check(bool condition, const char* msg) {
00117   if (!condition) {
00118     printf("FAILED: %s\n", msg);
00119     testing::internal::posix::Abort();
00120   }
00121 }
00122 
00123 // Runs the tests.  Return true iff successful.
00124 //
00125 // The 'failure' parameter specifies the type of failure that should
00126 // be generated by the global set-up.
00127 int RunAllTests(MyEnvironment* env, FailureType failure) {
00128   env->Reset();
00129   env->set_failure_in_set_up(failure);
00130   test_was_run = false;
00131   testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
00132   return RUN_ALL_TESTS();
00133 }
00134 
00135 }  // namespace
00136 
00137 int main(int argc, char **argv) {
00138   testing::InitGoogleTest(&argc, argv);
00139 
00140   // Registers a global test environment, and verifies that the
00141   // registration function returns its argument.
00142   MyEnvironment* const env = new MyEnvironment;
00143   Check(testing::AddGlobalTestEnvironment(env) == env,
00144         "AddGlobalTestEnvironment() should return its argument.");
00145 
00146   // Verifies that RUN_ALL_TESTS() runs the tests when the global
00147   // set-up is successful.
00148   Check(RunAllTests(env, NO_FAILURE) != 0,
00149         "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
00150         "should generate a failure.");
00151   Check(test_was_run,
00152         "The tests should run, as the global set-up should generate no "
00153         "failure");
00154   Check(env->tear_down_was_run(),
00155         "The global tear-down should run, as the global set-up was run.");
00156 
00157   // Verifies that RUN_ALL_TESTS() runs the tests when the global
00158   // set-up generates no fatal failure.
00159   Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
00160         "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
00161         "and the global tear-down should generate a non-fatal failure.");
00162   Check(test_was_run,
00163         "The tests should run, as the global set-up should generate no "
00164         "fatal failure.");
00165   Check(env->tear_down_was_run(),
00166         "The global tear-down should run, as the global set-up was run.");
00167 
00168   // Verifies that RUN_ALL_TESTS() runs no test when the global set-up
00169   // generates a fatal failure.
00170   Check(RunAllTests(env, FATAL_FAILURE) != 0,
00171         "RUN_ALL_TESTS() should return non-zero, as the global set-up "
00172         "should generate a fatal failure.");
00173   Check(!test_was_run,
00174         "The tests should not run, as the global set-up should generate "
00175         "a fatal failure.");
00176   Check(env->tear_down_was_run(),
00177         "The global tear-down should run, as the global set-up was run.");
00178 
00179   // Verifies that RUN_ALL_TESTS() doesn't do global set-up or
00180   // tear-down when there is no test to run.
00181   testing::GTEST_FLAG(filter) = "-*";
00182   Check(RunAllTests(env, NO_FAILURE) == 0,
00183         "RUN_ALL_TESTS() should return zero, as there is no test to run.");
00184   Check(!env->set_up_was_run(),
00185         "The global set-up should not run, as there is no test to run.");
00186   Check(!env->tear_down_was_run(),
00187         "The global tear-down should not run, "
00188         "as the global set-up was not run.");
00189 
00190   printf("PASS\n");
00191   return 0;
00192 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:55