/thirdparty/breakpad/third_party/protobuf/protobuf/gtest/test/gtest_environment_test.cc

http://github.com/tomahawk-player/tomahawk · C++ · 186 lines · 102 code · 24 blank · 60 comment · 7 complexity · 41cb073e7159c8d10340d2505b4bfea0 MD5 · raw file

  1. // Copyright 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: wan@google.com (Zhanyong Wan)
  31. //
  32. // Tests using global test environments.
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <gtest/gtest.h>
  36. namespace testing {
  37. GTEST_DECLARE_string_(filter);
  38. }
  39. namespace {
  40. enum FailureType {
  41. NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
  42. };
  43. // For testing using global test environments.
  44. class MyEnvironment : public testing::Environment {
  45. public:
  46. MyEnvironment() { Reset(); }
  47. // Depending on the value of failure_in_set_up_, SetUp() will
  48. // generate a non-fatal failure, generate a fatal failure, or
  49. // succeed.
  50. virtual void SetUp() {
  51. set_up_was_run_ = true;
  52. switch (failure_in_set_up_) {
  53. case NON_FATAL_FAILURE:
  54. ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
  55. break;
  56. case FATAL_FAILURE:
  57. FAIL() << "Expected fatal failure in global set-up.";
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. // Generates a non-fatal failure.
  64. virtual void TearDown() {
  65. tear_down_was_run_ = true;
  66. ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
  67. }
  68. // Resets the state of the environment s.t. it can be reused.
  69. void Reset() {
  70. failure_in_set_up_ = NO_FAILURE;
  71. set_up_was_run_ = false;
  72. tear_down_was_run_ = false;
  73. }
  74. // We call this function to set the type of failure SetUp() should
  75. // generate.
  76. void set_failure_in_set_up(FailureType type) {
  77. failure_in_set_up_ = type;
  78. }
  79. // Was SetUp() run?
  80. bool set_up_was_run() const { return set_up_was_run_; }
  81. // Was TearDown() run?
  82. bool tear_down_was_run() const { return tear_down_was_run_; }
  83. private:
  84. FailureType failure_in_set_up_;
  85. bool set_up_was_run_;
  86. bool tear_down_was_run_;
  87. };
  88. // Was the TEST run?
  89. bool test_was_run;
  90. // The sole purpose of this TEST is to enable us to check whether it
  91. // was run.
  92. TEST(FooTest, Bar) {
  93. test_was_run = true;
  94. }
  95. // Prints the message and aborts the program if condition is false.
  96. void Check(bool condition, const char* msg) {
  97. if (!condition) {
  98. printf("FAILED: %s\n", msg);
  99. abort();
  100. }
  101. }
  102. // Runs the tests. Return true iff successful.
  103. //
  104. // The 'failure' parameter specifies the type of failure that should
  105. // be generated by the global set-up.
  106. int RunAllTests(MyEnvironment* env, FailureType failure) {
  107. env->Reset();
  108. env->set_failure_in_set_up(failure);
  109. test_was_run = false;
  110. return RUN_ALL_TESTS();
  111. }
  112. } // namespace
  113. int main(int argc, char **argv) {
  114. testing::InitGoogleTest(&argc, argv);
  115. // Registers a global test environment, and verifies that the
  116. // registration function returns its argument.
  117. MyEnvironment* const env = new MyEnvironment;
  118. Check(testing::AddGlobalTestEnvironment(env) == env,
  119. "AddGlobalTestEnvironment() should return its argument.");
  120. // Verifies that RUN_ALL_TESTS() runs the tests when the global
  121. // set-up is successful.
  122. Check(RunAllTests(env, NO_FAILURE) != 0,
  123. "RUN_ALL_TESTS() should return non-zero, as the global tear-down "
  124. "should generate a failure.");
  125. Check(test_was_run,
  126. "The tests should run, as the global set-up should generate no "
  127. "failure");
  128. Check(env->tear_down_was_run(),
  129. "The global tear-down should run, as the global set-up was run.");
  130. // Verifies that RUN_ALL_TESTS() runs the tests when the global
  131. // set-up generates no fatal failure.
  132. Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
  133. "RUN_ALL_TESTS() should return non-zero, as both the global set-up "
  134. "and the global tear-down should generate a non-fatal failure.");
  135. Check(test_was_run,
  136. "The tests should run, as the global set-up should generate no "
  137. "fatal failure.");
  138. Check(env->tear_down_was_run(),
  139. "The global tear-down should run, as the global set-up was run.");
  140. // Verifies that RUN_ALL_TESTS() runs no test when the global set-up
  141. // generates a fatal failure.
  142. Check(RunAllTests(env, FATAL_FAILURE) != 0,
  143. "RUN_ALL_TESTS() should return non-zero, as the global set-up "
  144. "should generate a fatal failure.");
  145. Check(!test_was_run,
  146. "The tests should not run, as the global set-up should generate "
  147. "a fatal failure.");
  148. Check(env->tear_down_was_run(),
  149. "The global tear-down should run, as the global set-up was run.");
  150. // Verifies that RUN_ALL_TESTS() doesn't do global set-up or
  151. // tear-down when there is no test to run.
  152. testing::GTEST_FLAG(filter) = "-*";
  153. Check(RunAllTests(env, NO_FAILURE) == 0,
  154. "RUN_ALL_TESTS() should return zero, as there is no test to run.");
  155. Check(!env->set_up_was_run(),
  156. "The global set-up should not run, as there is no test to run.");
  157. Check(!env->tear_down_was_run(),
  158. "The global tear-down should not run, "
  159. "as the global set-up was not run.");
  160. printf("PASS\n");
  161. return 0;
  162. }