gmock.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2008, 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. #include "gmock/gmock.h"
  30. #include "gmock/internal/gmock-port.h"
  31. namespace testing {
  32. GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
  33. "true if and only if Google Mock should report leaked "
  34. "mock objects as failures.");
  35. GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity,
  36. "Controls how verbose Google Mock's output is."
  37. " Valid values:\n"
  38. " info - prints all messages.\n"
  39. " warning - prints warnings and errors.\n"
  40. " error - prints errors only.");
  41. GMOCK_DEFINE_int32_(default_mock_behavior, 1,
  42. "Controls the default behavior of mocks."
  43. " Valid values:\n"
  44. " 0 - by default, mocks act as NiceMocks.\n"
  45. " 1 - by default, mocks act as NaggyMocks.\n"
  46. " 2 - by default, mocks act as StrictMocks.");
  47. namespace internal {
  48. // Parses a string as a command line flag. The string should have the
  49. // format "--gmock_flag=value". When def_optional is true, the
  50. // "=value" part can be omitted.
  51. //
  52. // Returns the value of the flag, or NULL if the parsing failed.
  53. static const char* ParseGoogleMockFlagValue(const char* str,
  54. const char* flag,
  55. bool def_optional) {
  56. // str and flag must not be NULL.
  57. if (str == nullptr || flag == nullptr) return nullptr;
  58. // The flag must start with "--gmock_".
  59. const std::string flag_str = std::string("--gmock_") + flag;
  60. const size_t flag_len = flag_str.length();
  61. if (strncmp(str, flag_str.c_str(), flag_len) != 0) return nullptr;
  62. // Skips the flag name.
  63. const char* flag_end = str + flag_len;
  64. // When def_optional is true, it's OK to not have a "=value" part.
  65. if (def_optional && (flag_end[0] == '\0')) {
  66. return flag_end;
  67. }
  68. // If def_optional is true and there are more characters after the
  69. // flag name, or if def_optional is false, there must be a '=' after
  70. // the flag name.
  71. if (flag_end[0] != '=') return nullptr;
  72. // Returns the string after "=".
  73. return flag_end + 1;
  74. }
  75. // Parses a string for a Google Mock bool flag, in the form of
  76. // "--gmock_flag=value".
  77. //
  78. // On success, stores the value of the flag in *value, and returns
  79. // true. On failure, returns false without changing *value.
  80. static bool ParseGoogleMockBoolFlag(const char* str, const char* flag,
  81. bool* value) {
  82. // Gets the value of the flag as a string.
  83. const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
  84. // Aborts if the parsing failed.
  85. if (value_str == nullptr) return false;
  86. // Converts the string value to a bool.
  87. *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
  88. return true;
  89. }
  90. // Parses a string for a Google Mock string flag, in the form of
  91. // "--gmock_flag=value".
  92. //
  93. // On success, stores the value of the flag in *value, and returns
  94. // true. On failure, returns false without changing *value.
  95. template <typename String>
  96. static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
  97. String* value) {
  98. // Gets the value of the flag as a string.
  99. const char* const value_str = ParseGoogleMockFlagValue(str, flag, false);
  100. // Aborts if the parsing failed.
  101. if (value_str == nullptr) return false;
  102. // Sets *value to the value of the flag.
  103. *value = value_str;
  104. return true;
  105. }
  106. static bool ParseGoogleMockIntFlag(const char* str, const char* flag,
  107. int32_t* value) {
  108. // Gets the value of the flag as a string.
  109. const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
  110. // Aborts if the parsing failed.
  111. if (value_str == nullptr) return false;
  112. // Sets *value to the value of the flag.
  113. return ParseInt32(Message() << "The value of flag --" << flag,
  114. value_str, value);
  115. }
  116. // The internal implementation of InitGoogleMock().
  117. //
  118. // The type parameter CharType can be instantiated to either char or
  119. // wchar_t.
  120. template <typename CharType>
  121. void InitGoogleMockImpl(int* argc, CharType** argv) {
  122. // Makes sure Google Test is initialized. InitGoogleTest() is
  123. // idempotent, so it's fine if the user has already called it.
  124. InitGoogleTest(argc, argv);
  125. if (*argc <= 0) return;
  126. for (int i = 1; i != *argc; i++) {
  127. const std::string arg_string = StreamableToString(argv[i]);
  128. const char* const arg = arg_string.c_str();
  129. // Do we see a Google Mock flag?
  130. if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks",
  131. &GMOCK_FLAG(catch_leaked_mocks)) ||
  132. ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose)) ||
  133. ParseGoogleMockIntFlag(arg, "default_mock_behavior",
  134. &GMOCK_FLAG(default_mock_behavior))) {
  135. // Yes. Shift the remainder of the argv list left by one. Note
  136. // that argv has (*argc + 1) elements, the last one always being
  137. // NULL. The following loop moves the trailing NULL element as
  138. // well.
  139. for (int j = i; j != *argc; j++) {
  140. argv[j] = argv[j + 1];
  141. }
  142. // Decrements the argument count.
  143. (*argc)--;
  144. // We also need to decrement the iterator as we just removed
  145. // an element.
  146. i--;
  147. }
  148. }
  149. }
  150. } // namespace internal
  151. // Initializes Google Mock. This must be called before running the
  152. // tests. In particular, it parses a command line for the flags that
  153. // Google Mock recognizes. Whenever a Google Mock flag is seen, it is
  154. // removed from argv, and *argc is decremented.
  155. //
  156. // No value is returned. Instead, the Google Mock flag variables are
  157. // updated.
  158. //
  159. // Since Google Test is needed for Google Mock to work, this function
  160. // also initializes Google Test and parses its flags, if that hasn't
  161. // been done.
  162. GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
  163. internal::InitGoogleMockImpl(argc, argv);
  164. }
  165. // This overloaded version can be used in Windows programs compiled in
  166. // UNICODE mode.
  167. GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
  168. internal::InitGoogleMockImpl(argc, argv);
  169. }
  170. // This overloaded version can be used on Arduino/embedded platforms where
  171. // there is no argc/argv.
  172. GTEST_API_ void InitGoogleMock() {
  173. // Since Arduino doesn't have a command line, fake out the argc/argv arguments
  174. int argc = 1;
  175. const auto arg0 = "dummy";
  176. char* argv0 = const_cast<char*>(arg0);
  177. char** argv = &argv0;
  178. internal::InitGoogleMockImpl(&argc, argv);
  179. }
  180. } // namespace testing