gmock.cc 7.7 KB

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