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