gmock.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
  32. "true if and only if Google Mock should report leaked "
  33. "mock objects as failures.");
  34. GMOCK_DEFINE_string_(verbose, testing::internal::kWarningVerbosity,
  35. "Controls how verbose Google Mock's output is."
  36. " Valid values:\n"
  37. " info - prints all messages.\n"
  38. " warning - prints warnings and errors.\n"
  39. " error - prints errors only.");
  40. GMOCK_DEFINE_int32_(default_mock_behavior, 1,
  41. "Controls the default behavior of mocks."
  42. " Valid values:\n"
  43. " 0 - by default, mocks act as NiceMocks.\n"
  44. " 1 - by default, mocks act as NaggyMocks.\n"
  45. " 2 - by default, mocks act as StrictMocks.");
  46. namespace testing {
  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_name,
  55. bool def_optional) {
  56. // str and flag must not be NULL.
  57. if (str == nullptr || flag_name == nullptr) return nullptr;
  58. // The flag must start with "--gmock_".
  59. const std::string flag_name_str = std::string("--gmock_") + flag_name;
  60. const size_t flag_name_len = flag_name_str.length();
  61. if (strncmp(str, flag_name_str.c_str(), flag_name_len) != 0) return nullptr;
  62. // Skips the flag name.
  63. const char* flag_end = str + flag_name_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 ParseGoogleMockFlag(const char* str, const char* flag_name,
  81. bool* value) {
  82. // Gets the value of the flag as a string.
  83. const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, 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 ParseGoogleMockFlag(const char* str, const char* flag_name,
  97. String* value) {
  98. // Gets the value of the flag as a string.
  99. const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, 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 ParseGoogleMockFlag(const char* str, const char* flag_name,
  107. int32_t* value) {
  108. // Gets the value of the flag as a string.
  109. const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, 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_name, value_str,
  114. 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. bool found_gmock_flag = false;
  131. #define GMOCK_INTERNAL_PARSE_FLAG(flag_name) \
  132. if (!found_gmock_flag) { \
  133. auto value = GMOCK_FLAG_GET(flag_name); \
  134. if (ParseGoogleMockFlag(arg, #flag_name, &value)) { \
  135. GMOCK_FLAG_SET(flag_name, value); \
  136. found_gmock_flag = true; \
  137. } \
  138. }
  139. GMOCK_INTERNAL_PARSE_FLAG(catch_leaked_mocks)
  140. GMOCK_INTERNAL_PARSE_FLAG(verbose)
  141. GMOCK_INTERNAL_PARSE_FLAG(default_mock_behavior)
  142. if (found_gmock_flag) {
  143. // Yes. Shift the remainder of the argv list left by one. Note
  144. // that argv has (*argc + 1) elements, the last one always being
  145. // NULL. The following loop moves the trailing NULL element as
  146. // well.
  147. for (int j = i; j != *argc; j++) {
  148. argv[j] = argv[j + 1];
  149. }
  150. // Decrements the argument count.
  151. (*argc)--;
  152. // We also need to decrement the iterator as we just removed
  153. // an element.
  154. i--;
  155. }
  156. }
  157. }
  158. } // namespace internal
  159. // Initializes Google Mock. This must be called before running the
  160. // tests. In particular, it parses a command line for the flags that
  161. // Google Mock recognizes. Whenever a Google Mock flag is seen, it is
  162. // removed from argv, and *argc is decremented.
  163. //
  164. // No value is returned. Instead, the Google Mock flag variables are
  165. // updated.
  166. //
  167. // Since Google Test is needed for Google Mock to work, this function
  168. // also initializes Google Test and parses its flags, if that hasn't
  169. // been done.
  170. GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
  171. internal::InitGoogleMockImpl(argc, argv);
  172. }
  173. // This overloaded version can be used in Windows programs compiled in
  174. // UNICODE mode.
  175. GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
  176. internal::InitGoogleMockImpl(argc, argv);
  177. }
  178. // This overloaded version can be used on Arduino/embedded platforms where
  179. // there is no argc/argv.
  180. GTEST_API_ void InitGoogleMock() {
  181. // Since Arduino doesn't have a command line, fake out the argc/argv arguments
  182. int argc = 1;
  183. const auto arg0 = "dummy";
  184. char* argv0 = const_cast<char*>(arg0);
  185. char** argv = &argv0;
  186. internal::InitGoogleMockImpl(&argc, argv);
  187. }
  188. } // namespace testing