/testlibs/gmock/src/gmock.cc

https://github.com/deltaforge/nebu-app-hadoop · C++ · 182 lines · 67 code · 27 blank · 88 comment · 26 complexity · 2694f9e7359d8aeee34475a48d218887 MD5 · raw file

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