testing/gtest/gmock/src/gmock.cc

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 // Copyright 2008, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // Author: wan@google.com (Zhanyong Wan)
michael@0 31
michael@0 32 #include "gmock/gmock.h"
michael@0 33 #include "gmock/internal/gmock-port.h"
michael@0 34
michael@0 35 namespace testing {
michael@0 36
michael@0 37 // TODO(wan@google.com): support using environment variables to
michael@0 38 // control the flag values, like what Google Test does.
michael@0 39
michael@0 40 GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
michael@0 41 "true iff Google Mock should report leaked mock objects "
michael@0 42 "as failures.");
michael@0 43
michael@0 44 GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity,
michael@0 45 "Controls how verbose Google Mock's output is."
michael@0 46 " Valid values:\n"
michael@0 47 " info - prints all messages.\n"
michael@0 48 " warning - prints warnings and errors.\n"
michael@0 49 " error - prints errors only.");
michael@0 50
michael@0 51 namespace internal {
michael@0 52
michael@0 53 // Parses a string as a command line flag. The string should have the
michael@0 54 // format "--gmock_flag=value". When def_optional is true, the
michael@0 55 // "=value" part can be omitted.
michael@0 56 //
michael@0 57 // Returns the value of the flag, or NULL if the parsing failed.
michael@0 58 static const char* ParseGoogleMockFlagValue(const char* str,
michael@0 59 const char* flag,
michael@0 60 bool def_optional) {
michael@0 61 // str and flag must not be NULL.
michael@0 62 if (str == NULL || flag == NULL) return NULL;
michael@0 63
michael@0 64 // The flag must start with "--gmock_".
michael@0 65 const String flag_str = String::Format("--gmock_%s", flag);
michael@0 66 const size_t flag_len = flag_str.length();
michael@0 67 if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
michael@0 68
michael@0 69 // Skips the flag name.
michael@0 70 const char* flag_end = str + flag_len;
michael@0 71
michael@0 72 // When def_optional is true, it's OK to not have a "=value" part.
michael@0 73 if (def_optional && (flag_end[0] == '\0')) {
michael@0 74 return flag_end;
michael@0 75 }
michael@0 76
michael@0 77 // If def_optional is true and there are more characters after the
michael@0 78 // flag name, or if def_optional is false, there must be a '=' after
michael@0 79 // the flag name.
michael@0 80 if (flag_end[0] != '=') return NULL;
michael@0 81
michael@0 82 // Returns the string after "=".
michael@0 83 return flag_end + 1;
michael@0 84 }
michael@0 85
michael@0 86 // Parses a string for a Google Mock bool flag, in the form of
michael@0 87 // "--gmock_flag=value".
michael@0 88 //
michael@0 89 // On success, stores the value of the flag in *value, and returns
michael@0 90 // true. On failure, returns false without changing *value.
michael@0 91 static bool ParseGoogleMockBoolFlag(const char* str, const char* flag,
michael@0 92 bool* value) {
michael@0 93 // Gets the value of the flag as a string.
michael@0 94 const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
michael@0 95
michael@0 96 // Aborts if the parsing failed.
michael@0 97 if (value_str == NULL) return false;
michael@0 98
michael@0 99 // Converts the string value to a bool.
michael@0 100 *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
michael@0 101 return true;
michael@0 102 }
michael@0 103
michael@0 104 // Parses a string for a Google Mock string flag, in the form of
michael@0 105 // "--gmock_flag=value".
michael@0 106 //
michael@0 107 // On success, stores the value of the flag in *value, and returns
michael@0 108 // true. On failure, returns false without changing *value.
michael@0 109 static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
michael@0 110 String* value) {
michael@0 111 // Gets the value of the flag as a string.
michael@0 112 const char* const value_str = ParseGoogleMockFlagValue(str, flag, false);
michael@0 113
michael@0 114 // Aborts if the parsing failed.
michael@0 115 if (value_str == NULL) return false;
michael@0 116
michael@0 117 // Sets *value to the value of the flag.
michael@0 118 *value = value_str;
michael@0 119 return true;
michael@0 120 }
michael@0 121
michael@0 122 // The internal implementation of InitGoogleMock().
michael@0 123 //
michael@0 124 // The type parameter CharType can be instantiated to either char or
michael@0 125 // wchar_t.
michael@0 126 template <typename CharType>
michael@0 127 void InitGoogleMockImpl(int* argc, CharType** argv) {
michael@0 128 // Makes sure Google Test is initialized. InitGoogleTest() is
michael@0 129 // idempotent, so it's fine if the user has already called it.
michael@0 130 InitGoogleTest(argc, argv);
michael@0 131 if (*argc <= 0) return;
michael@0 132
michael@0 133 for (int i = 1; i != *argc; i++) {
michael@0 134 const String arg_string = StreamableToString(argv[i]);
michael@0 135 const char* const arg = arg_string.c_str();
michael@0 136
michael@0 137 // Do we see a Google Mock flag?
michael@0 138 if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks",
michael@0 139 &GMOCK_FLAG(catch_leaked_mocks)) ||
michael@0 140 ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose))) {
michael@0 141 // Yes. Shift the remainder of the argv list left by one. Note
michael@0 142 // that argv has (*argc + 1) elements, the last one always being
michael@0 143 // NULL. The following loop moves the trailing NULL element as
michael@0 144 // well.
michael@0 145 for (int j = i; j != *argc; j++) {
michael@0 146 argv[j] = argv[j + 1];
michael@0 147 }
michael@0 148
michael@0 149 // Decrements the argument count.
michael@0 150 (*argc)--;
michael@0 151
michael@0 152 // We also need to decrement the iterator as we just removed
michael@0 153 // an element.
michael@0 154 i--;
michael@0 155 }
michael@0 156 }
michael@0 157 }
michael@0 158
michael@0 159 } // namespace internal
michael@0 160
michael@0 161 // Initializes Google Mock. This must be called before running the
michael@0 162 // tests. In particular, it parses a command line for the flags that
michael@0 163 // Google Mock recognizes. Whenever a Google Mock flag is seen, it is
michael@0 164 // removed from argv, and *argc is decremented.
michael@0 165 //
michael@0 166 // No value is returned. Instead, the Google Mock flag variables are
michael@0 167 // updated.
michael@0 168 //
michael@0 169 // Since Google Test is needed for Google Mock to work, this function
michael@0 170 // also initializes Google Test and parses its flags, if that hasn't
michael@0 171 // been done.
michael@0 172 void InitGoogleMock(int* argc, char** argv) {
michael@0 173 internal::InitGoogleMockImpl(argc, argv);
michael@0 174 }
michael@0 175
michael@0 176 // This overloaded version can be used in Windows programs compiled in
michael@0 177 // UNICODE mode.
michael@0 178 void InitGoogleMock(int* argc, wchar_t** argv) {
michael@0 179 internal::InitGoogleMockImpl(argc, argv);
michael@0 180 }
michael@0 181
michael@0 182 } // namespace testing

mercurial