You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.3 KiB

  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. // Tests using global test environments.
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include "gtest/gtest.h"
  34. #include "src/gtest-internal-inl.h"
  35. namespace testing {
  36. GTEST_DECLARE_string_(filter);
  37. }
  38. namespace {
  39. enum FailureType {
  40. NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE
  41. };
  42. // For testing using global test environments.
  43. class MyEnvironment : public testing::Environment {
  44. public:
  45. MyEnvironment() { Reset(); }
  46. // Depending on the value of failure_in_set_up_, SetUp() will
  47. // generate a non-fatal failure, generate a fatal failure, or
  48. // succeed.
  49. void SetUp() override {
  50. set_up_was_run_ = true;
  51. switch (failure_in_set_up_) {
  52. case NON_FATAL_FAILURE:
  53. ADD_FAILURE() << "Expected non-fatal failure in global set-up.";
  54. break;
  55. case FATAL_FAILURE:
  56. FAIL() << "Expected fatal failure in global set-up.";
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. // Generates a non-fatal failure.
  63. void TearDown() override {
  64. tear_down_was_run_ = true;
  65. ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
  66. }
  67. // Resets the state of the environment s.t. it can be reused.
  68. void Reset() {
  69. failure_in_set_up_ = NO_FAILURE;
  70. set_up_was_run_ = false;
  71. tear_down_was_run_ = false;
  72. }
  73. // We call this function to set the type of failure SetUp() should
  74. // generate.
  75. void set_failure_in_set_up(FailureType type) {
  76. failure_in_set_up_ = type;
  77. }
  78. // Was SetUp() run?
  79. bool set_up_was_run() const { return set_up_was_run_; }
  80. // Was TearDown() run?
  81. bool tear_down_was_run() const { return tear_down_was_run_; }
  82. private:
  83. FailureType failure_in_set_up_;
  84. bool set_up_was_run_;
  85. bool tear_down_was_run_;
  86. };
  87. // Was the TEST run?
  88. bool test_was_run;
  89. // The sole purpose of this TEST is to enable us to check whether it
  90. // was run.
  91. TEST(FooTest, Bar) {
  92. test_was_run = true;
  93. }
  94. // Prints the message and aborts the program if condition is false.
  95. void Check(bool condition, const char* msg) {
  96. if (!condition) {
  97. printf("FAILED: %s\n", msg);
  98. testing::internal::posix::Abort();
  99. }
  100. }
  101. // Runs the tests. Return true if and only if successful.
  102. //
  103. // The 'failure' parameter specifies the type of failure that should
  104. // be generated by the global set-up.
  105. int RunAllTests(MyEnvironment* env, FailureType failure) {
  106. env->Reset();
  107. env->set_failure_in_set_up(failure);
  108. test_was_run = false;
  109. testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
  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. }