]>
Commit | Line | Data |
---|---|---|
31f18b77 FG |
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: vladl@google.com (Vlad Losev) | |
31 | // | |
32 | // Tests for Google Test itself. This file verifies that the parameter | |
33 | // generators objects produce correct parameter sequences and that | |
34 | // Google Test runtime instantiates correct tests from those sequences. | |
35 | ||
36 | #include "gtest/gtest.h" | |
37 | ||
31f18b77 FG |
38 | # include <algorithm> |
39 | # include <iostream> | |
40 | # include <list> | |
41 | # include <sstream> | |
42 | # include <string> | |
43 | # include <vector> | |
44 | ||
31f18b77 | 45 | # include "src/gtest-internal-inl.h" // for UnitTestOptions |
31f18b77 FG |
46 | # include "test/gtest-param-test_test.h" |
47 | ||
48 | using ::std::vector; | |
49 | using ::std::sort; | |
50 | ||
51 | using ::testing::AddGlobalTestEnvironment; | |
52 | using ::testing::Bool; | |
53 | using ::testing::Message; | |
54 | using ::testing::Range; | |
55 | using ::testing::TestWithParam; | |
56 | using ::testing::Values; | |
57 | using ::testing::ValuesIn; | |
58 | ||
59 | # if GTEST_HAS_COMBINE | |
60 | using ::testing::Combine; | |
61 | using ::testing::get; | |
62 | using ::testing::make_tuple; | |
63 | using ::testing::tuple; | |
64 | # endif // GTEST_HAS_COMBINE | |
65 | ||
66 | using ::testing::internal::ParamGenerator; | |
67 | using ::testing::internal::UnitTestOptions; | |
68 | ||
69 | // Prints a value to a string. | |
70 | // | |
71 | // TODO(wan@google.com): remove PrintValue() when we move matchers and | |
72 | // EXPECT_THAT() from Google Mock to Google Test. At that time, we | |
73 | // can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as | |
74 | // EXPECT_THAT() and the matchers know how to print tuples. | |
75 | template <typename T> | |
76 | ::std::string PrintValue(const T& value) { | |
77 | ::std::stringstream stream; | |
78 | stream << value; | |
79 | return stream.str(); | |
80 | } | |
81 | ||
82 | # if GTEST_HAS_COMBINE | |
83 | ||
84 | // These overloads allow printing tuples in our tests. We cannot | |
85 | // define an operator<< for tuples, as that definition needs to be in | |
86 | // the std namespace in order to be picked up by Google Test via | |
87 | // Argument-Dependent Lookup, yet defining anything in the std | |
88 | // namespace in non-STL code is undefined behavior. | |
89 | ||
90 | template <typename T1, typename T2> | |
91 | ::std::string PrintValue(const tuple<T1, T2>& value) { | |
92 | ::std::stringstream stream; | |
93 | stream << "(" << get<0>(value) << ", " << get<1>(value) << ")"; | |
94 | return stream.str(); | |
95 | } | |
96 | ||
97 | template <typename T1, typename T2, typename T3> | |
98 | ::std::string PrintValue(const tuple<T1, T2, T3>& value) { | |
99 | ::std::stringstream stream; | |
100 | stream << "(" << get<0>(value) << ", " << get<1>(value) | |
101 | << ", "<< get<2>(value) << ")"; | |
102 | return stream.str(); | |
103 | } | |
104 | ||
105 | template <typename T1, typename T2, typename T3, typename T4, typename T5, | |
106 | typename T6, typename T7, typename T8, typename T9, typename T10> | |
107 | ::std::string PrintValue( | |
108 | const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) { | |
109 | ::std::stringstream stream; | |
110 | stream << "(" << get<0>(value) << ", " << get<1>(value) | |
111 | << ", "<< get<2>(value) << ", " << get<3>(value) | |
112 | << ", "<< get<4>(value) << ", " << get<5>(value) | |
113 | << ", "<< get<6>(value) << ", " << get<7>(value) | |
114 | << ", "<< get<8>(value) << ", " << get<9>(value) << ")"; | |
115 | return stream.str(); | |
116 | } | |
117 | ||
118 | # endif // GTEST_HAS_COMBINE | |
119 | ||
120 | // Verifies that a sequence generated by the generator and accessed | |
121 | // via the iterator object matches the expected one using Google Test | |
122 | // assertions. | |
123 | template <typename T, size_t N> | |
124 | void VerifyGenerator(const ParamGenerator<T>& generator, | |
125 | const T (&expected_values)[N]) { | |
126 | typename ParamGenerator<T>::iterator it = generator.begin(); | |
127 | for (size_t i = 0; i < N; ++i) { | |
128 | ASSERT_FALSE(it == generator.end()) | |
129 | << "At element " << i << " when accessing via an iterator " | |
130 | << "created with the copy constructor.\n"; | |
131 | // We cannot use EXPECT_EQ() here as the values may be tuples, | |
132 | // which don't support <<. | |
133 | EXPECT_TRUE(expected_values[i] == *it) | |
134 | << "where i is " << i | |
135 | << ", expected_values[i] is " << PrintValue(expected_values[i]) | |
136 | << ", *it is " << PrintValue(*it) | |
137 | << ", and 'it' is an iterator created with the copy constructor.\n"; | |
1e59de90 | 138 | ++it; |
31f18b77 FG |
139 | } |
140 | EXPECT_TRUE(it == generator.end()) | |
141 | << "At the presumed end of sequence when accessing via an iterator " | |
142 | << "created with the copy constructor.\n"; | |
143 | ||
144 | // Test the iterator assignment. The following lines verify that | |
145 | // the sequence accessed via an iterator initialized via the | |
146 | // assignment operator (as opposed to a copy constructor) matches | |
147 | // just the same. | |
148 | it = generator.begin(); | |
149 | for (size_t i = 0; i < N; ++i) { | |
150 | ASSERT_FALSE(it == generator.end()) | |
151 | << "At element " << i << " when accessing via an iterator " | |
152 | << "created with the assignment operator.\n"; | |
153 | EXPECT_TRUE(expected_values[i] == *it) | |
154 | << "where i is " << i | |
155 | << ", expected_values[i] is " << PrintValue(expected_values[i]) | |
156 | << ", *it is " << PrintValue(*it) | |
157 | << ", and 'it' is an iterator created with the copy constructor.\n"; | |
1e59de90 | 158 | ++it; |
31f18b77 FG |
159 | } |
160 | EXPECT_TRUE(it == generator.end()) | |
161 | << "At the presumed end of sequence when accessing via an iterator " | |
162 | << "created with the assignment operator.\n"; | |
163 | } | |
164 | ||
165 | template <typename T> | |
166 | void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) { | |
167 | typename ParamGenerator<T>::iterator it = generator.begin(); | |
168 | EXPECT_TRUE(it == generator.end()); | |
169 | ||
170 | it = generator.begin(); | |
171 | EXPECT_TRUE(it == generator.end()); | |
172 | } | |
173 | ||
174 | // Generator tests. They test that each of the provided generator functions | |
175 | // generates an expected sequence of values. The general test pattern | |
176 | // instantiates a generator using one of the generator functions, | |
177 | // checks the sequence produced by the generator using its iterator API, | |
178 | // and then resets the iterator back to the beginning of the sequence | |
179 | // and checks the sequence again. | |
180 | ||
181 | // Tests that iterators produced by generator functions conform to the | |
182 | // ForwardIterator concept. | |
183 | TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) { | |
184 | const ParamGenerator<int> gen = Range(0, 10); | |
185 | ParamGenerator<int>::iterator it = gen.begin(); | |
186 | ||
187 | // Verifies that iterator initialization works as expected. | |
188 | ParamGenerator<int>::iterator it2 = it; | |
189 | EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the " | |
190 | << "element same as its source points to"; | |
191 | ||
192 | // Verifies that iterator assignment works as expected. | |
1e59de90 | 193 | ++it; |
31f18b77 FG |
194 | EXPECT_FALSE(*it == *it2); |
195 | it2 = it; | |
196 | EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the " | |
197 | << "element same as its source points to"; | |
198 | ||
199 | // Verifies that prefix operator++() returns *this. | |
200 | EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be " | |
201 | << "refer to the original object"; | |
202 | ||
203 | // Verifies that the result of the postfix operator++ points to the value | |
204 | // pointed to by the original iterator. | |
205 | int original_value = *it; // Have to compute it outside of macro call to be | |
206 | // unaffected by the parameter evaluation order. | |
207 | EXPECT_EQ(original_value, *(it++)); | |
208 | ||
209 | // Verifies that prefix and postfix operator++() advance an iterator | |
210 | // all the same. | |
211 | it2 = it; | |
1e59de90 | 212 | ++it; |
31f18b77 FG |
213 | ++it2; |
214 | EXPECT_TRUE(*it == *it2); | |
215 | } | |
216 | ||
217 | // Tests that Range() generates the expected sequence. | |
218 | TEST(RangeTest, IntRangeWithDefaultStep) { | |
219 | const ParamGenerator<int> gen = Range(0, 3); | |
220 | const int expected_values[] = {0, 1, 2}; | |
221 | VerifyGenerator(gen, expected_values); | |
222 | } | |
223 | ||
224 | // Edge case. Tests that Range() generates the single element sequence | |
225 | // as expected when provided with range limits that are equal. | |
226 | TEST(RangeTest, IntRangeSingleValue) { | |
227 | const ParamGenerator<int> gen = Range(0, 1); | |
228 | const int expected_values[] = {0}; | |
229 | VerifyGenerator(gen, expected_values); | |
230 | } | |
231 | ||
232 | // Edge case. Tests that Range() with generates empty sequence when | |
233 | // supplied with an empty range. | |
234 | TEST(RangeTest, IntRangeEmpty) { | |
235 | const ParamGenerator<int> gen = Range(0, 0); | |
236 | VerifyGeneratorIsEmpty(gen); | |
237 | } | |
238 | ||
239 | // Tests that Range() with custom step (greater then one) generates | |
240 | // the expected sequence. | |
241 | TEST(RangeTest, IntRangeWithCustomStep) { | |
242 | const ParamGenerator<int> gen = Range(0, 9, 3); | |
243 | const int expected_values[] = {0, 3, 6}; | |
244 | VerifyGenerator(gen, expected_values); | |
245 | } | |
246 | ||
247 | // Tests that Range() with custom step (greater then one) generates | |
248 | // the expected sequence when the last element does not fall on the | |
249 | // upper range limit. Sequences generated by Range() must not have | |
250 | // elements beyond the range limits. | |
251 | TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) { | |
252 | const ParamGenerator<int> gen = Range(0, 4, 3); | |
253 | const int expected_values[] = {0, 3}; | |
254 | VerifyGenerator(gen, expected_values); | |
255 | } | |
256 | ||
257 | // Verifies that Range works with user-defined types that define | |
258 | // copy constructor, operator=(), operator+(), and operator<(). | |
259 | class DogAdder { | |
260 | public: | |
261 | explicit DogAdder(const char* a_value) : value_(a_value) {} | |
262 | DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {} | |
263 | ||
264 | DogAdder operator=(const DogAdder& other) { | |
265 | if (this != &other) | |
266 | value_ = other.value_; | |
267 | return *this; | |
268 | } | |
269 | DogAdder operator+(const DogAdder& other) const { | |
270 | Message msg; | |
271 | msg << value_.c_str() << other.value_.c_str(); | |
272 | return DogAdder(msg.GetString().c_str()); | |
273 | } | |
274 | bool operator<(const DogAdder& other) const { | |
275 | return value_ < other.value_; | |
276 | } | |
277 | const std::string& value() const { return value_; } | |
278 | ||
279 | private: | |
280 | std::string value_; | |
281 | }; | |
282 | ||
283 | TEST(RangeTest, WorksWithACustomType) { | |
284 | const ParamGenerator<DogAdder> gen = | |
285 | Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog")); | |
286 | ParamGenerator<DogAdder>::iterator it = gen.begin(); | |
287 | ||
288 | ASSERT_FALSE(it == gen.end()); | |
289 | EXPECT_STREQ("cat", it->value().c_str()); | |
290 | ||
291 | ASSERT_FALSE(++it == gen.end()); | |
292 | EXPECT_STREQ("catdog", it->value().c_str()); | |
293 | ||
294 | EXPECT_TRUE(++it == gen.end()); | |
295 | } | |
296 | ||
297 | class IntWrapper { | |
298 | public: | |
299 | explicit IntWrapper(int a_value) : value_(a_value) {} | |
300 | IntWrapper(const IntWrapper& other) : value_(other.value_) {} | |
301 | ||
302 | IntWrapper operator=(const IntWrapper& other) { | |
303 | value_ = other.value_; | |
304 | return *this; | |
305 | } | |
306 | // operator+() adds a different type. | |
307 | IntWrapper operator+(int other) const { return IntWrapper(value_ + other); } | |
308 | bool operator<(const IntWrapper& other) const { | |
309 | return value_ < other.value_; | |
310 | } | |
311 | int value() const { return value_; } | |
312 | ||
313 | private: | |
314 | int value_; | |
315 | }; | |
316 | ||
317 | TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) { | |
318 | const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2)); | |
319 | ParamGenerator<IntWrapper>::iterator it = gen.begin(); | |
320 | ||
321 | ASSERT_FALSE(it == gen.end()); | |
322 | EXPECT_EQ(0, it->value()); | |
323 | ||
324 | ASSERT_FALSE(++it == gen.end()); | |
325 | EXPECT_EQ(1, it->value()); | |
326 | ||
327 | EXPECT_TRUE(++it == gen.end()); | |
328 | } | |
329 | ||
330 | // Tests that ValuesIn() with an array parameter generates | |
331 | // the expected sequence. | |
332 | TEST(ValuesInTest, ValuesInArray) { | |
333 | int array[] = {3, 5, 8}; | |
334 | const ParamGenerator<int> gen = ValuesIn(array); | |
335 | VerifyGenerator(gen, array); | |
336 | } | |
337 | ||
338 | // Tests that ValuesIn() with a const array parameter generates | |
339 | // the expected sequence. | |
340 | TEST(ValuesInTest, ValuesInConstArray) { | |
341 | const int array[] = {3, 5, 8}; | |
342 | const ParamGenerator<int> gen = ValuesIn(array); | |
343 | VerifyGenerator(gen, array); | |
344 | } | |
345 | ||
346 | // Edge case. Tests that ValuesIn() with an array parameter containing a | |
347 | // single element generates the single element sequence. | |
348 | TEST(ValuesInTest, ValuesInSingleElementArray) { | |
349 | int array[] = {42}; | |
350 | const ParamGenerator<int> gen = ValuesIn(array); | |
351 | VerifyGenerator(gen, array); | |
352 | } | |
353 | ||
354 | // Tests that ValuesIn() generates the expected sequence for an STL | |
355 | // container (vector). | |
356 | TEST(ValuesInTest, ValuesInVector) { | |
357 | typedef ::std::vector<int> ContainerType; | |
358 | ContainerType values; | |
359 | values.push_back(3); | |
360 | values.push_back(5); | |
361 | values.push_back(8); | |
362 | const ParamGenerator<int> gen = ValuesIn(values); | |
363 | ||
364 | const int expected_values[] = {3, 5, 8}; | |
365 | VerifyGenerator(gen, expected_values); | |
366 | } | |
367 | ||
368 | // Tests that ValuesIn() generates the expected sequence. | |
369 | TEST(ValuesInTest, ValuesInIteratorRange) { | |
370 | typedef ::std::vector<int> ContainerType; | |
371 | ContainerType values; | |
372 | values.push_back(3); | |
373 | values.push_back(5); | |
374 | values.push_back(8); | |
375 | const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end()); | |
376 | ||
377 | const int expected_values[] = {3, 5, 8}; | |
378 | VerifyGenerator(gen, expected_values); | |
379 | } | |
380 | ||
381 | // Edge case. Tests that ValuesIn() provided with an iterator range specifying a | |
382 | // single value generates a single-element sequence. | |
383 | TEST(ValuesInTest, ValuesInSingleElementIteratorRange) { | |
384 | typedef ::std::vector<int> ContainerType; | |
385 | ContainerType values; | |
386 | values.push_back(42); | |
387 | const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end()); | |
388 | ||
389 | const int expected_values[] = {42}; | |
390 | VerifyGenerator(gen, expected_values); | |
391 | } | |
392 | ||
393 | // Edge case. Tests that ValuesIn() provided with an empty iterator range | |
394 | // generates an empty sequence. | |
395 | TEST(ValuesInTest, ValuesInEmptyIteratorRange) { | |
396 | typedef ::std::vector<int> ContainerType; | |
397 | ContainerType values; | |
398 | const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end()); | |
399 | ||
400 | VerifyGeneratorIsEmpty(gen); | |
401 | } | |
402 | ||
403 | // Tests that the Values() generates the expected sequence. | |
404 | TEST(ValuesTest, ValuesWorks) { | |
405 | const ParamGenerator<int> gen = Values(3, 5, 8); | |
406 | ||
407 | const int expected_values[] = {3, 5, 8}; | |
408 | VerifyGenerator(gen, expected_values); | |
409 | } | |
410 | ||
411 | // Tests that Values() generates the expected sequences from elements of | |
412 | // different types convertible to ParamGenerator's parameter type. | |
413 | TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) { | |
414 | const ParamGenerator<double> gen = Values(3, 5.0f, 8.0); | |
415 | ||
416 | const double expected_values[] = {3.0, 5.0, 8.0}; | |
417 | VerifyGenerator(gen, expected_values); | |
418 | } | |
419 | ||
420 | TEST(ValuesTest, ValuesWorksForMaxLengthList) { | |
421 | const ParamGenerator<int> gen = Values( | |
422 | 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, | |
423 | 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, | |
424 | 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, | |
425 | 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, | |
426 | 410, 420, 430, 440, 450, 460, 470, 480, 490, 500); | |
427 | ||
428 | const int expected_values[] = { | |
429 | 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, | |
430 | 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, | |
431 | 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, | |
432 | 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, | |
433 | 410, 420, 430, 440, 450, 460, 470, 480, 490, 500}; | |
434 | VerifyGenerator(gen, expected_values); | |
435 | } | |
436 | ||
437 | // Edge case test. Tests that single-parameter Values() generates the sequence | |
438 | // with the single value. | |
439 | TEST(ValuesTest, ValuesWithSingleParameter) { | |
440 | const ParamGenerator<int> gen = Values(42); | |
441 | ||
442 | const int expected_values[] = {42}; | |
443 | VerifyGenerator(gen, expected_values); | |
444 | } | |
445 | ||
446 | // Tests that Bool() generates sequence (false, true). | |
447 | TEST(BoolTest, BoolWorks) { | |
448 | const ParamGenerator<bool> gen = Bool(); | |
449 | ||
450 | const bool expected_values[] = {false, true}; | |
451 | VerifyGenerator(gen, expected_values); | |
452 | } | |
453 | ||
454 | # if GTEST_HAS_COMBINE | |
455 | ||
456 | // Tests that Combine() with two parameters generates the expected sequence. | |
457 | TEST(CombineTest, CombineWithTwoParameters) { | |
458 | const char* foo = "foo"; | |
459 | const char* bar = "bar"; | |
460 | const ParamGenerator<tuple<const char*, int> > gen = | |
461 | Combine(Values(foo, bar), Values(3, 4)); | |
462 | ||
463 | tuple<const char*, int> expected_values[] = { | |
464 | make_tuple(foo, 3), make_tuple(foo, 4), | |
465 | make_tuple(bar, 3), make_tuple(bar, 4)}; | |
466 | VerifyGenerator(gen, expected_values); | |
467 | } | |
468 | ||
469 | // Tests that Combine() with three parameters generates the expected sequence. | |
470 | TEST(CombineTest, CombineWithThreeParameters) { | |
471 | const ParamGenerator<tuple<int, int, int> > gen = Combine(Values(0, 1), | |
472 | Values(3, 4), | |
473 | Values(5, 6)); | |
474 | tuple<int, int, int> expected_values[] = { | |
475 | make_tuple(0, 3, 5), make_tuple(0, 3, 6), | |
476 | make_tuple(0, 4, 5), make_tuple(0, 4, 6), | |
477 | make_tuple(1, 3, 5), make_tuple(1, 3, 6), | |
478 | make_tuple(1, 4, 5), make_tuple(1, 4, 6)}; | |
479 | VerifyGenerator(gen, expected_values); | |
480 | } | |
481 | ||
482 | // Tests that the Combine() with the first parameter generating a single value | |
483 | // sequence generates a sequence with the number of elements equal to the | |
484 | // number of elements in the sequence generated by the second parameter. | |
485 | TEST(CombineTest, CombineWithFirstParameterSingleValue) { | |
486 | const ParamGenerator<tuple<int, int> > gen = Combine(Values(42), | |
487 | Values(0, 1)); | |
488 | ||
489 | tuple<int, int> expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)}; | |
490 | VerifyGenerator(gen, expected_values); | |
491 | } | |
492 | ||
493 | // Tests that the Combine() with the second parameter generating a single value | |
494 | // sequence generates a sequence with the number of elements equal to the | |
495 | // number of elements in the sequence generated by the first parameter. | |
496 | TEST(CombineTest, CombineWithSecondParameterSingleValue) { | |
497 | const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1), | |
498 | Values(42)); | |
499 | ||
500 | tuple<int, int> expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)}; | |
501 | VerifyGenerator(gen, expected_values); | |
502 | } | |
503 | ||
504 | // Tests that when the first parameter produces an empty sequence, | |
505 | // Combine() produces an empty sequence, too. | |
506 | TEST(CombineTest, CombineWithFirstParameterEmptyRange) { | |
507 | const ParamGenerator<tuple<int, int> > gen = Combine(Range(0, 0), | |
508 | Values(0, 1)); | |
509 | VerifyGeneratorIsEmpty(gen); | |
510 | } | |
511 | ||
512 | // Tests that when the second parameter produces an empty sequence, | |
513 | // Combine() produces an empty sequence, too. | |
514 | TEST(CombineTest, CombineWithSecondParameterEmptyRange) { | |
515 | const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1), | |
516 | Range(1, 1)); | |
517 | VerifyGeneratorIsEmpty(gen); | |
518 | } | |
519 | ||
520 | // Edge case. Tests that combine works with the maximum number | |
521 | // of parameters supported by Google Test (currently 10). | |
522 | TEST(CombineTest, CombineWithMaxNumberOfParameters) { | |
523 | const char* foo = "foo"; | |
524 | const char* bar = "bar"; | |
525 | const ParamGenerator<tuple<const char*, int, int, int, int, int, int, int, | |
526 | int, int> > gen = Combine(Values(foo, bar), | |
527 | Values(1), Values(2), | |
528 | Values(3), Values(4), | |
529 | Values(5), Values(6), | |
530 | Values(7), Values(8), | |
531 | Values(9)); | |
532 | ||
533 | tuple<const char*, int, int, int, int, int, int, int, int, int> | |
534 | expected_values[] = {make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9), | |
535 | make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)}; | |
536 | VerifyGenerator(gen, expected_values); | |
537 | } | |
538 | ||
1e59de90 TL |
539 | #if GTEST_LANG_CXX11 |
540 | ||
541 | class NonDefaultConstructAssignString { | |
542 | public: | |
543 | NonDefaultConstructAssignString(const std::string& s) : str_(s) {} | |
544 | ||
545 | const std::string& str() const { return str_; } | |
546 | ||
547 | private: | |
548 | std::string str_; | |
549 | ||
550 | // Not default constructible | |
551 | NonDefaultConstructAssignString(); | |
552 | // Not assignable | |
553 | void operator=(const NonDefaultConstructAssignString&); | |
554 | }; | |
555 | ||
556 | TEST(CombineTest, NonDefaultConstructAssign) { | |
557 | const ParamGenerator<tuple<int, NonDefaultConstructAssignString> > gen = | |
558 | Combine(Values(0, 1), Values(NonDefaultConstructAssignString("A"), | |
559 | NonDefaultConstructAssignString("B"))); | |
560 | ||
561 | ParamGenerator<tuple<int, NonDefaultConstructAssignString> >::iterator it = | |
562 | gen.begin(); | |
563 | ||
564 | EXPECT_EQ(0, std::get<0>(*it)); | |
565 | EXPECT_EQ("A", std::get<1>(*it).str()); | |
566 | ++it; | |
567 | ||
568 | EXPECT_EQ(0, std::get<0>(*it)); | |
569 | EXPECT_EQ("B", std::get<1>(*it).str()); | |
570 | ++it; | |
571 | ||
572 | EXPECT_EQ(1, std::get<0>(*it)); | |
573 | EXPECT_EQ("A", std::get<1>(*it).str()); | |
574 | ++it; | |
575 | ||
576 | EXPECT_EQ(1, std::get<0>(*it)); | |
577 | EXPECT_EQ("B", std::get<1>(*it).str()); | |
578 | ++it; | |
579 | ||
580 | EXPECT_TRUE(it == gen.end()); | |
581 | } | |
582 | ||
583 | #endif // GTEST_LANG_CXX11 | |
31f18b77 FG |
584 | # endif // GTEST_HAS_COMBINE |
585 | ||
586 | // Tests that an generator produces correct sequence after being | |
587 | // assigned from another generator. | |
588 | TEST(ParamGeneratorTest, AssignmentWorks) { | |
589 | ParamGenerator<int> gen = Values(1, 2); | |
590 | const ParamGenerator<int> gen2 = Values(3, 4); | |
591 | gen = gen2; | |
592 | ||
593 | const int expected_values[] = {3, 4}; | |
594 | VerifyGenerator(gen, expected_values); | |
595 | } | |
596 | ||
597 | // This test verifies that the tests are expanded and run as specified: | |
598 | // one test per element from the sequence produced by the generator | |
599 | // specified in INSTANTIATE_TEST_CASE_P. It also verifies that the test's | |
600 | // fixture constructor, SetUp(), and TearDown() have run and have been | |
601 | // supplied with the correct parameters. | |
602 | ||
603 | // The use of environment object allows detection of the case where no test | |
604 | // case functionality is run at all. In this case TestCaseTearDown will not | |
605 | // be able to detect missing tests, naturally. | |
606 | template <int kExpectedCalls> | |
607 | class TestGenerationEnvironment : public ::testing::Environment { | |
608 | public: | |
609 | static TestGenerationEnvironment* Instance() { | |
610 | static TestGenerationEnvironment* instance = new TestGenerationEnvironment; | |
611 | return instance; | |
612 | } | |
613 | ||
614 | void FixtureConstructorExecuted() { fixture_constructor_count_++; } | |
615 | void SetUpExecuted() { set_up_count_++; } | |
616 | void TearDownExecuted() { tear_down_count_++; } | |
617 | void TestBodyExecuted() { test_body_count_++; } | |
618 | ||
619 | virtual void TearDown() { | |
620 | // If all MultipleTestGenerationTest tests have been de-selected | |
621 | // by the filter flag, the following checks make no sense. | |
622 | bool perform_check = false; | |
623 | ||
624 | for (int i = 0; i < kExpectedCalls; ++i) { | |
625 | Message msg; | |
626 | msg << "TestsExpandedAndRun/" << i; | |
627 | if (UnitTestOptions::FilterMatchesTest( | |
628 | "TestExpansionModule/MultipleTestGenerationTest", | |
629 | msg.GetString().c_str())) { | |
630 | perform_check = true; | |
631 | } | |
632 | } | |
633 | if (perform_check) { | |
634 | EXPECT_EQ(kExpectedCalls, fixture_constructor_count_) | |
635 | << "Fixture constructor of ParamTestGenerationTest test case " | |
636 | << "has not been run as expected."; | |
637 | EXPECT_EQ(kExpectedCalls, set_up_count_) | |
638 | << "Fixture SetUp method of ParamTestGenerationTest test case " | |
639 | << "has not been run as expected."; | |
640 | EXPECT_EQ(kExpectedCalls, tear_down_count_) | |
641 | << "Fixture TearDown method of ParamTestGenerationTest test case " | |
642 | << "has not been run as expected."; | |
643 | EXPECT_EQ(kExpectedCalls, test_body_count_) | |
644 | << "Test in ParamTestGenerationTest test case " | |
645 | << "has not been run as expected."; | |
646 | } | |
647 | } | |
648 | ||
649 | private: | |
650 | TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0), | |
651 | tear_down_count_(0), test_body_count_(0) {} | |
652 | ||
653 | int fixture_constructor_count_; | |
654 | int set_up_count_; | |
655 | int tear_down_count_; | |
656 | int test_body_count_; | |
657 | ||
658 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment); | |
659 | }; | |
660 | ||
661 | const int test_generation_params[] = {36, 42, 72}; | |
662 | ||
663 | class TestGenerationTest : public TestWithParam<int> { | |
664 | public: | |
665 | enum { | |
666 | PARAMETER_COUNT = | |
667 | sizeof(test_generation_params)/sizeof(test_generation_params[0]) | |
668 | }; | |
669 | ||
670 | typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment; | |
671 | ||
672 | TestGenerationTest() { | |
673 | Environment::Instance()->FixtureConstructorExecuted(); | |
674 | current_parameter_ = GetParam(); | |
675 | } | |
676 | virtual void SetUp() { | |
677 | Environment::Instance()->SetUpExecuted(); | |
678 | EXPECT_EQ(current_parameter_, GetParam()); | |
679 | } | |
680 | virtual void TearDown() { | |
681 | Environment::Instance()->TearDownExecuted(); | |
682 | EXPECT_EQ(current_parameter_, GetParam()); | |
683 | } | |
684 | ||
685 | static void SetUpTestCase() { | |
686 | bool all_tests_in_test_case_selected = true; | |
687 | ||
688 | for (int i = 0; i < PARAMETER_COUNT; ++i) { | |
689 | Message test_name; | |
690 | test_name << "TestsExpandedAndRun/" << i; | |
691 | if ( !UnitTestOptions::FilterMatchesTest( | |
692 | "TestExpansionModule/MultipleTestGenerationTest", | |
693 | test_name.GetString())) { | |
694 | all_tests_in_test_case_selected = false; | |
695 | } | |
696 | } | |
697 | EXPECT_TRUE(all_tests_in_test_case_selected) | |
698 | << "When running the TestGenerationTest test case all of its tests\n" | |
699 | << "must be selected by the filter flag for the test case to pass.\n" | |
700 | << "If not all of them are enabled, we can't reliably conclude\n" | |
701 | << "that the correct number of tests have been generated."; | |
702 | ||
703 | collected_parameters_.clear(); | |
704 | } | |
705 | ||
706 | static void TearDownTestCase() { | |
707 | vector<int> expected_values(test_generation_params, | |
708 | test_generation_params + PARAMETER_COUNT); | |
709 | // Test execution order is not guaranteed by Google Test, | |
710 | // so the order of values in collected_parameters_ can be | |
711 | // different and we have to sort to compare. | |
712 | sort(expected_values.begin(), expected_values.end()); | |
713 | sort(collected_parameters_.begin(), collected_parameters_.end()); | |
714 | ||
715 | EXPECT_TRUE(collected_parameters_ == expected_values); | |
716 | } | |
717 | ||
718 | protected: | |
719 | int current_parameter_; | |
720 | static vector<int> collected_parameters_; | |
721 | ||
722 | private: | |
723 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest); | |
724 | }; | |
725 | vector<int> TestGenerationTest::collected_parameters_; | |
726 | ||
727 | TEST_P(TestGenerationTest, TestsExpandedAndRun) { | |
728 | Environment::Instance()->TestBodyExecuted(); | |
729 | EXPECT_EQ(current_parameter_, GetParam()); | |
730 | collected_parameters_.push_back(GetParam()); | |
731 | } | |
732 | INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest, | |
733 | ValuesIn(test_generation_params)); | |
734 | ||
735 | // This test verifies that the element sequence (third parameter of | |
736 | // INSTANTIATE_TEST_CASE_P) is evaluated in InitGoogleTest() and neither at | |
737 | // the call site of INSTANTIATE_TEST_CASE_P nor in RUN_ALL_TESTS(). For | |
738 | // that, we declare param_value_ to be a static member of | |
739 | // GeneratorEvaluationTest and initialize it to 0. We set it to 1 in | |
740 | // main(), just before invocation of InitGoogleTest(). After calling | |
741 | // InitGoogleTest(), we set the value to 2. If the sequence is evaluated | |
742 | // before or after InitGoogleTest, INSTANTIATE_TEST_CASE_P will create a | |
743 | // test with parameter other than 1, and the test body will fail the | |
744 | // assertion. | |
745 | class GeneratorEvaluationTest : public TestWithParam<int> { | |
746 | public: | |
747 | static int param_value() { return param_value_; } | |
748 | static void set_param_value(int param_value) { param_value_ = param_value; } | |
749 | ||
750 | private: | |
751 | static int param_value_; | |
752 | }; | |
753 | int GeneratorEvaluationTest::param_value_ = 0; | |
754 | ||
755 | TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) { | |
756 | EXPECT_EQ(1, GetParam()); | |
757 | } | |
758 | INSTANTIATE_TEST_CASE_P(GenEvalModule, | |
759 | GeneratorEvaluationTest, | |
760 | Values(GeneratorEvaluationTest::param_value())); | |
761 | ||
762 | // Tests that generators defined in a different translation unit are | |
763 | // functional. Generator extern_gen is defined in gtest-param-test_test2.cc. | |
764 | extern ParamGenerator<int> extern_gen; | |
765 | class ExternalGeneratorTest : public TestWithParam<int> {}; | |
766 | TEST_P(ExternalGeneratorTest, ExternalGenerator) { | |
767 | // Sequence produced by extern_gen contains only a single value | |
768 | // which we verify here. | |
769 | EXPECT_EQ(GetParam(), 33); | |
770 | } | |
771 | INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule, | |
772 | ExternalGeneratorTest, | |
773 | extern_gen); | |
774 | ||
775 | // Tests that a parameterized test case can be defined in one translation | |
776 | // unit and instantiated in another. This test will be instantiated in | |
777 | // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is | |
778 | // defined in gtest-param-test_test.h. | |
779 | TEST_P(ExternalInstantiationTest, IsMultipleOf33) { | |
780 | EXPECT_EQ(0, GetParam() % 33); | |
781 | } | |
782 | ||
783 | // Tests that a parameterized test case can be instantiated with multiple | |
784 | // generators. | |
785 | class MultipleInstantiationTest : public TestWithParam<int> {}; | |
786 | TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) { | |
787 | } | |
788 | INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2)); | |
789 | INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5)); | |
790 | ||
791 | // Tests that a parameterized test case can be instantiated | |
792 | // in multiple translation units. This test will be instantiated | |
793 | // here and in gtest-param-test_test2.cc. | |
794 | // InstantiationInMultipleTranslationUnitsTest fixture class | |
795 | // is defined in gtest-param-test_test.h. | |
796 | TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) { | |
797 | EXPECT_EQ(0, GetParam() % 42); | |
798 | } | |
799 | INSTANTIATE_TEST_CASE_P(Sequence1, | |
800 | InstantiationInMultipleTranslaionUnitsTest, | |
801 | Values(42, 42*2)); | |
802 | ||
803 | // Tests that each iteration of parameterized test runs in a separate test | |
804 | // object. | |
805 | class SeparateInstanceTest : public TestWithParam<int> { | |
806 | public: | |
807 | SeparateInstanceTest() : count_(0) {} | |
808 | ||
809 | static void TearDownTestCase() { | |
810 | EXPECT_GE(global_count_, 2) | |
811 | << "If some (but not all) SeparateInstanceTest tests have been " | |
812 | << "filtered out this test will fail. Make sure that all " | |
813 | << "GeneratorEvaluationTest are selected or de-selected together " | |
814 | << "by the test filter."; | |
815 | } | |
816 | ||
817 | protected: | |
818 | int count_; | |
819 | static int global_count_; | |
820 | }; | |
821 | int SeparateInstanceTest::global_count_ = 0; | |
822 | ||
823 | TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) { | |
824 | EXPECT_EQ(0, count_++); | |
825 | global_count_++; | |
826 | } | |
827 | INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4)); | |
828 | ||
829 | // Tests that all instantiations of a test have named appropriately. Test | |
830 | // defined with TEST_P(TestCaseName, TestName) and instantiated with | |
831 | // INSTANTIATE_TEST_CASE_P(SequenceName, TestCaseName, generator) must be named | |
832 | // SequenceName/TestCaseName.TestName/i, where i is the 0-based index of the | |
833 | // sequence element used to instantiate the test. | |
834 | class NamingTest : public TestWithParam<int> {}; | |
835 | ||
836 | TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) { | |
837 | const ::testing::TestInfo* const test_info = | |
838 | ::testing::UnitTest::GetInstance()->current_test_info(); | |
839 | ||
840 | EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name()); | |
841 | ||
842 | Message index_stream; | |
843 | index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam(); | |
844 | EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name()); | |
845 | ||
846 | EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param()); | |
847 | } | |
848 | ||
849 | INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5)); | |
850 | ||
1e59de90 TL |
851 | // Tests that macros in test names are expanded correctly. |
852 | class MacroNamingTest : public TestWithParam<int> {}; | |
853 | ||
854 | #define PREFIX_WITH_FOO(test_name) Foo##test_name | |
855 | #define PREFIX_WITH_MACRO(test_name) Macro##test_name | |
856 | ||
857 | TEST_P(PREFIX_WITH_MACRO(NamingTest), PREFIX_WITH_FOO(SomeTestName)) { | |
858 | const ::testing::TestInfo* const test_info = | |
859 | ::testing::UnitTest::GetInstance()->current_test_info(); | |
860 | ||
861 | EXPECT_STREQ("FortyTwo/MacroNamingTest", test_info->test_case_name()); | |
862 | EXPECT_STREQ("FooSomeTestName", test_info->name()); | |
863 | } | |
864 | ||
865 | INSTANTIATE_TEST_CASE_P(FortyTwo, MacroNamingTest, Values(42)); | |
866 | ||
867 | // Tests the same thing for non-parametrized tests. | |
868 | class MacroNamingTestNonParametrized : public ::testing::Test {}; | |
869 | ||
870 | TEST_F(PREFIX_WITH_MACRO(NamingTestNonParametrized), | |
871 | PREFIX_WITH_FOO(SomeTestName)) { | |
872 | const ::testing::TestInfo* const test_info = | |
873 | ::testing::UnitTest::GetInstance()->current_test_info(); | |
874 | ||
875 | EXPECT_STREQ("MacroNamingTestNonParametrized", test_info->test_case_name()); | |
876 | EXPECT_STREQ("FooSomeTestName", test_info->name()); | |
877 | } | |
878 | ||
31f18b77 FG |
879 | // Tests that user supplied custom parameter names are working correctly. |
880 | // Runs the test with a builtin helper method which uses PrintToString, | |
881 | // as well as a custom function and custom functor to ensure all possible | |
882 | // uses work correctly. | |
883 | class CustomFunctorNamingTest : public TestWithParam<std::string> {}; | |
884 | TEST_P(CustomFunctorNamingTest, CustomTestNames) {} | |
885 | ||
886 | struct CustomParamNameFunctor { | |
1e59de90 TL |
887 | std::string operator()(const ::testing::TestParamInfo<std::string>& inf) { |
888 | return inf.param; | |
31f18b77 FG |
889 | } |
890 | }; | |
891 | ||
892 | INSTANTIATE_TEST_CASE_P(CustomParamNameFunctor, | |
893 | CustomFunctorNamingTest, | |
894 | Values(std::string("FunctorName")), | |
895 | CustomParamNameFunctor()); | |
896 | ||
897 | INSTANTIATE_TEST_CASE_P(AllAllowedCharacters, | |
898 | CustomFunctorNamingTest, | |
899 | Values("abcdefghijklmnopqrstuvwxyz", | |
900 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | |
901 | "01234567890_"), | |
902 | CustomParamNameFunctor()); | |
903 | ||
904 | inline std::string CustomParamNameFunction( | |
1e59de90 TL |
905 | const ::testing::TestParamInfo<std::string>& inf) { |
906 | return inf.param; | |
31f18b77 FG |
907 | } |
908 | ||
909 | class CustomFunctionNamingTest : public TestWithParam<std::string> {}; | |
910 | TEST_P(CustomFunctionNamingTest, CustomTestNames) {} | |
911 | ||
912 | INSTANTIATE_TEST_CASE_P(CustomParamNameFunction, | |
913 | CustomFunctionNamingTest, | |
914 | Values(std::string("FunctionName")), | |
915 | CustomParamNameFunction); | |
916 | ||
917 | #if GTEST_LANG_CXX11 | |
918 | ||
919 | // Test custom naming with a lambda | |
920 | ||
921 | class CustomLambdaNamingTest : public TestWithParam<std::string> {}; | |
922 | TEST_P(CustomLambdaNamingTest, CustomTestNames) {} | |
923 | ||
1e59de90 | 924 | INSTANTIATE_TEST_CASE_P(CustomParamNameLambda, CustomLambdaNamingTest, |
31f18b77 | 925 | Values(std::string("LambdaName")), |
1e59de90 TL |
926 | [](const ::testing::TestParamInfo<std::string>& inf) { |
927 | return inf.param; | |
31f18b77 FG |
928 | }); |
929 | ||
930 | #endif // GTEST_LANG_CXX11 | |
931 | ||
932 | TEST(CustomNamingTest, CheckNameRegistry) { | |
933 | ::testing::UnitTest* unit_test = ::testing::UnitTest::GetInstance(); | |
934 | std::set<std::string> test_names; | |
935 | for (int case_num = 0; | |
936 | case_num < unit_test->total_test_case_count(); | |
937 | ++case_num) { | |
938 | const ::testing::TestCase* test_case = unit_test->GetTestCase(case_num); | |
939 | for (int test_num = 0; | |
940 | test_num < test_case->total_test_count(); | |
941 | ++test_num) { | |
942 | const ::testing::TestInfo* test_info = test_case->GetTestInfo(test_num); | |
943 | test_names.insert(std::string(test_info->name())); | |
944 | } | |
945 | } | |
946 | EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctorName")); | |
947 | EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionName")); | |
948 | #if GTEST_LANG_CXX11 | |
949 | EXPECT_EQ(1u, test_names.count("CustomTestNames/LambdaName")); | |
950 | #endif // GTEST_LANG_CXX11 | |
951 | } | |
952 | ||
953 | // Test a numeric name to ensure PrintToStringParamName works correctly. | |
954 | ||
955 | class CustomIntegerNamingTest : public TestWithParam<int> {}; | |
956 | ||
957 | TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) { | |
958 | const ::testing::TestInfo* const test_info = | |
959 | ::testing::UnitTest::GetInstance()->current_test_info(); | |
960 | Message test_name_stream; | |
961 | test_name_stream << "TestsReportCorrectNames/" << GetParam(); | |
962 | EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | |
963 | } | |
964 | ||
965 | INSTANTIATE_TEST_CASE_P(PrintToString, | |
966 | CustomIntegerNamingTest, | |
967 | Range(0, 5), | |
968 | ::testing::PrintToStringParamName()); | |
969 | ||
970 | // Test a custom struct with PrintToString. | |
971 | ||
972 | struct CustomStruct { | |
973 | explicit CustomStruct(int value) : x(value) {} | |
974 | int x; | |
975 | }; | |
976 | ||
977 | std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) { | |
978 | stream << val.x; | |
979 | return stream; | |
980 | } | |
981 | ||
982 | class CustomStructNamingTest : public TestWithParam<CustomStruct> {}; | |
983 | ||
984 | TEST_P(CustomStructNamingTest, TestsReportCorrectNames) { | |
985 | const ::testing::TestInfo* const test_info = | |
986 | ::testing::UnitTest::GetInstance()->current_test_info(); | |
987 | Message test_name_stream; | |
988 | test_name_stream << "TestsReportCorrectNames/" << GetParam(); | |
989 | EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | |
990 | } | |
991 | ||
992 | INSTANTIATE_TEST_CASE_P(PrintToString, | |
993 | CustomStructNamingTest, | |
994 | Values(CustomStruct(0), CustomStruct(1)), | |
995 | ::testing::PrintToStringParamName()); | |
996 | ||
997 | // Test that using a stateful parameter naming function works as expected. | |
998 | ||
999 | struct StatefulNamingFunctor { | |
1000 | StatefulNamingFunctor() : sum(0) {} | |
1001 | std::string operator()(const ::testing::TestParamInfo<int>& info) { | |
1002 | int value = info.param + sum; | |
1003 | sum += info.param; | |
1004 | return ::testing::PrintToString(value); | |
1005 | } | |
1006 | int sum; | |
1007 | }; | |
1008 | ||
1009 | class StatefulNamingTest : public ::testing::TestWithParam<int> { | |
1010 | protected: | |
1011 | StatefulNamingTest() : sum_(0) {} | |
1012 | int sum_; | |
1013 | }; | |
1014 | ||
1015 | TEST_P(StatefulNamingTest, TestsReportCorrectNames) { | |
1016 | const ::testing::TestInfo* const test_info = | |
1017 | ::testing::UnitTest::GetInstance()->current_test_info(); | |
1018 | sum_ += GetParam(); | |
1019 | Message test_name_stream; | |
1020 | test_name_stream << "TestsReportCorrectNames/" << sum_; | |
1021 | EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | |
1022 | } | |
1023 | ||
1024 | INSTANTIATE_TEST_CASE_P(StatefulNamingFunctor, | |
1025 | StatefulNamingTest, | |
1026 | Range(0, 5), | |
1027 | StatefulNamingFunctor()); | |
1028 | ||
1029 | // Class that cannot be streamed into an ostream. It needs to be copyable | |
1030 | // (and, in case of MSVC, also assignable) in order to be a test parameter | |
1031 | // type. Its default copy constructor and assignment operator do exactly | |
1032 | // what we need. | |
1033 | class Unstreamable { | |
1034 | public: | |
1035 | explicit Unstreamable(int value) : value_(value) {} | |
1036 | ||
1037 | private: | |
1038 | int value_; | |
1039 | }; | |
1040 | ||
1041 | class CommentTest : public TestWithParam<Unstreamable> {}; | |
1042 | ||
1043 | TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) { | |
1044 | const ::testing::TestInfo* const test_info = | |
1045 | ::testing::UnitTest::GetInstance()->current_test_info(); | |
1046 | ||
1047 | EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param()); | |
1048 | } | |
1049 | ||
1050 | INSTANTIATE_TEST_CASE_P(InstantiationWithComments, | |
1051 | CommentTest, | |
1052 | Values(Unstreamable(1))); | |
1053 | ||
1054 | // Verify that we can create a hierarchy of test fixtures, where the base | |
1055 | // class fixture is not parameterized and the derived class is. In this case | |
1056 | // ParameterizedDerivedTest inherits from NonParameterizedBaseTest. We | |
1057 | // perform simple tests on both. | |
1058 | class NonParameterizedBaseTest : public ::testing::Test { | |
1059 | public: | |
1060 | NonParameterizedBaseTest() : n_(17) { } | |
1061 | protected: | |
1062 | int n_; | |
1063 | }; | |
1064 | ||
1065 | class ParameterizedDerivedTest : public NonParameterizedBaseTest, | |
1066 | public ::testing::WithParamInterface<int> { | |
1067 | protected: | |
1068 | ParameterizedDerivedTest() : count_(0) { } | |
1069 | int count_; | |
1070 | static int global_count_; | |
1071 | }; | |
1072 | ||
1073 | int ParameterizedDerivedTest::global_count_ = 0; | |
1074 | ||
1075 | TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) { | |
1076 | EXPECT_EQ(17, n_); | |
1077 | } | |
1078 | ||
1079 | TEST_P(ParameterizedDerivedTest, SeesSequence) { | |
1080 | EXPECT_EQ(17, n_); | |
1081 | EXPECT_EQ(0, count_++); | |
1082 | EXPECT_EQ(GetParam(), global_count_++); | |
1083 | } | |
1084 | ||
1085 | class ParameterizedDeathTest : public ::testing::TestWithParam<int> { }; | |
1086 | ||
1087 | TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) { | |
1088 | EXPECT_DEATH_IF_SUPPORTED(GetParam(), | |
1089 | ".* value-parameterized test .*"); | |
1090 | } | |
1091 | ||
1092 | INSTANTIATE_TEST_CASE_P(RangeZeroToFive, ParameterizedDerivedTest, Range(0, 5)); | |
1093 | ||
31f18b77 FG |
1094 | |
1095 | int main(int argc, char **argv) { | |
31f18b77 FG |
1096 | // Used in TestGenerationTest test case. |
1097 | AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance()); | |
1098 | // Used in GeneratorEvaluationTest test case. Tests that the updated value | |
1099 | // will be picked up for instantiating tests in GeneratorEvaluationTest. | |
1100 | GeneratorEvaluationTest::set_param_value(1); | |
31f18b77 FG |
1101 | |
1102 | ::testing::InitGoogleTest(&argc, argv); | |
1103 | ||
31f18b77 FG |
1104 | // Used in GeneratorEvaluationTest test case. Tests that value updated |
1105 | // here will NOT be used for instantiating tests in | |
1106 | // GeneratorEvaluationTest. | |
1107 | GeneratorEvaluationTest::set_param_value(2); | |
31f18b77 FG |
1108 | |
1109 | return RUN_ALL_TESTS(); | |
1110 | } |