blob: c949dd75c4064d23f11d27f6ceaf864bd966fdab [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, 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// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests the internal utilities.
35
36#include <gmock/internal/gmock-internal-utils.h>
zhanyong.wan16cf4732009-05-14 20:55:30 +000037#include <stdlib.h>
shiqiane35fdd92008-12-10 05:08:54 +000038#include <map>
39#include <string>
40#include <sstream>
41#include <vector>
42#include <gmock/gmock.h>
43#include <gmock/internal/gmock-port.h>
44#include <gtest/gtest.h>
45#include <gtest/gtest-spi.h>
46
zhanyong.wan16cf4732009-05-14 20:55:30 +000047#if GTEST_OS_CYGWIN
48#include <sys/types.h> // For ssize_t. NOLINT
49#endif
50
zhanyong.wan0ea67f82009-08-14 04:50:02 +000051class ProtocolMessage;
52
53namespace proto2 {
54class Message;
55} // namespace proto2
56
shiqiane35fdd92008-12-10 05:08:54 +000057namespace testing {
58namespace internal {
59
60namespace {
61
zhanyong.wanb8243162009-06-04 05:48:20 +000062using ::std::tr1::make_tuple;
shiqiane35fdd92008-12-10 05:08:54 +000063using ::std::tr1::tuple;
64
zhanyong.wance198ff2009-02-12 01:34:27 +000065TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
66 EXPECT_EQ("", ConvertIdentifierNameToWords(""));
67 EXPECT_EQ("", ConvertIdentifierNameToWords("_"));
68 EXPECT_EQ("", ConvertIdentifierNameToWords("__"));
69}
70
71TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) {
72 EXPECT_EQ("1", ConvertIdentifierNameToWords("_1"));
73 EXPECT_EQ("2", ConvertIdentifierNameToWords("2_"));
74 EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_"));
75 EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56"));
76}
77
78TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) {
79 EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord"));
80 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar"));
81 EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_"));
82 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_"));
83 EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar"));
84}
85
86TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) {
87 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar"));
88 EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_"));
89 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_"));
90 EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar"));
91}
92
93TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) {
94 EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123"));
95 EXPECT_EQ("chapter 11 section 1",
96 ConvertIdentifierNameToWords("_Chapter11Section_1_"));
97}
98
shiqiane35fdd92008-12-10 05:08:54 +000099// Tests that CompileAssertTypesEqual compiles when the type arguments are
100// equal.
101TEST(CompileAssertTypesEqual, CompilesWhenTypesAreEqual) {
102 CompileAssertTypesEqual<void, void>();
103 CompileAssertTypesEqual<int*, int*>();
104}
105
106// Tests that RemoveReference does not affect non-reference types.
107TEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) {
108 CompileAssertTypesEqual<int, RemoveReference<int>::type>();
109 CompileAssertTypesEqual<const char, RemoveReference<const char>::type>();
110}
111
112// Tests that RemoveReference removes reference from reference types.
113TEST(RemoveReferenceTest, RemovesReference) {
114 CompileAssertTypesEqual<int, RemoveReference<int&>::type>();
115 CompileAssertTypesEqual<const char, RemoveReference<const char&>::type>();
116}
117
zhanyong.wane0d051e2009-02-19 00:33:37 +0000118// Tests GMOCK_REMOVE_REFERENCE_.
shiqiane35fdd92008-12-10 05:08:54 +0000119
120template <typename T1, typename T2>
121void TestGMockRemoveReference() {
zhanyong.wane0d051e2009-02-19 00:33:37 +0000122 CompileAssertTypesEqual<T1, GMOCK_REMOVE_REFERENCE_(T2)>();
shiqiane35fdd92008-12-10 05:08:54 +0000123}
124
125TEST(RemoveReferenceTest, MacroVersion) {
126 TestGMockRemoveReference<int, int>();
127 TestGMockRemoveReference<const char, const char&>();
128}
129
130
131// Tests that RemoveConst does not affect non-const types.
132TEST(RemoveConstTest, DoesNotAffectNonConstType) {
133 CompileAssertTypesEqual<int, RemoveConst<int>::type>();
134 CompileAssertTypesEqual<char&, RemoveConst<char&>::type>();
135}
136
137// Tests that RemoveConst removes const from const types.
138TEST(RemoveConstTest, RemovesConst) {
139 CompileAssertTypesEqual<int, RemoveConst<const int>::type>();
zhanyong.wanb8243162009-06-04 05:48:20 +0000140 CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>();
141 CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>();
shiqiane35fdd92008-12-10 05:08:54 +0000142}
143
zhanyong.wane0d051e2009-02-19 00:33:37 +0000144// Tests GMOCK_REMOVE_CONST_.
shiqiane35fdd92008-12-10 05:08:54 +0000145
146template <typename T1, typename T2>
147void TestGMockRemoveConst() {
zhanyong.wane0d051e2009-02-19 00:33:37 +0000148 CompileAssertTypesEqual<T1, GMOCK_REMOVE_CONST_(T2)>();
shiqiane35fdd92008-12-10 05:08:54 +0000149}
150
151TEST(RemoveConstTest, MacroVersion) {
152 TestGMockRemoveConst<int, int>();
153 TestGMockRemoveConst<double&, double&>();
154 TestGMockRemoveConst<char, const char>();
155}
156
157// Tests that AddReference does not affect reference types.
158TEST(AddReferenceTest, DoesNotAffectReferenceType) {
159 CompileAssertTypesEqual<int&, AddReference<int&>::type>();
160 CompileAssertTypesEqual<const char&, AddReference<const char&>::type>();
161}
162
163// Tests that AddReference adds reference to non-reference types.
164TEST(AddReferenceTest, AddsReference) {
165 CompileAssertTypesEqual<int&, AddReference<int>::type>();
166 CompileAssertTypesEqual<const char&, AddReference<const char>::type>();
167}
168
zhanyong.wane0d051e2009-02-19 00:33:37 +0000169// Tests GMOCK_ADD_REFERENCE_.
shiqiane35fdd92008-12-10 05:08:54 +0000170
171template <typename T1, typename T2>
172void TestGMockAddReference() {
zhanyong.wane0d051e2009-02-19 00:33:37 +0000173 CompileAssertTypesEqual<T1, GMOCK_ADD_REFERENCE_(T2)>();
shiqiane35fdd92008-12-10 05:08:54 +0000174}
175
176TEST(AddReferenceTest, MacroVersion) {
177 TestGMockAddReference<int&, int>();
178 TestGMockAddReference<const char&, const char&>();
179}
180
zhanyong.wane0d051e2009-02-19 00:33:37 +0000181// Tests GMOCK_REFERENCE_TO_CONST_.
shiqiane35fdd92008-12-10 05:08:54 +0000182
183template <typename T1, typename T2>
184void TestGMockReferenceToConst() {
zhanyong.wane0d051e2009-02-19 00:33:37 +0000185 CompileAssertTypesEqual<T1, GMOCK_REFERENCE_TO_CONST_(T2)>();
shiqiane35fdd92008-12-10 05:08:54 +0000186}
187
188TEST(GMockReferenceToConstTest, Works) {
189 TestGMockReferenceToConst<const char&, char>();
190 TestGMockReferenceToConst<const int&, const int>();
191 TestGMockReferenceToConst<const double&, double>();
192 TestGMockReferenceToConst<const string&, const string&>();
193}
194
195TEST(PointeeOfTest, WorksForSmartPointers) {
196 CompileAssertTypesEqual<const char,
197 PointeeOf<internal::linked_ptr<const char> >::type>();
198}
199
200TEST(PointeeOfTest, WorksForRawPointers) {
201 CompileAssertTypesEqual<int, PointeeOf<int*>::type>();
202 CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>();
203 CompileAssertTypesEqual<void, PointeeOf<void*>::type>();
204}
205
206TEST(GetRawPointerTest, WorksForSmartPointers) {
207 const char* const raw_p4 = new const char('a'); // NOLINT
208 const internal::linked_ptr<const char> p4(raw_p4);
209 EXPECT_EQ(raw_p4, GetRawPointer(p4));
210}
211
212TEST(GetRawPointerTest, WorksForRawPointers) {
213 int* p = NULL;
214 EXPECT_EQ(NULL, GetRawPointer(p));
215 int n = 1;
216 EXPECT_EQ(&n, GetRawPointer(&n));
217}
218
219class Base {};
220class Derived : public Base {};
221
222// Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant.
223TEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) {
zhanyong.wane0d051e2009-02-19 00:33:37 +0000224 GMOCK_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true);
225 GMOCK_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value),
226 const_false);
shiqiane35fdd92008-12-10 05:08:54 +0000227}
228
229// Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can
230// be implicitly converted to T2.
231TEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) {
232 EXPECT_TRUE((ImplicitlyConvertible<int, double>::value));
233 EXPECT_TRUE((ImplicitlyConvertible<double, int>::value));
234 EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value));
235 EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value));
236 EXPECT_TRUE((ImplicitlyConvertible<Derived&, const Base&>::value));
237 EXPECT_TRUE((ImplicitlyConvertible<const Base, Base>::value));
238}
239
240// Tests that ImplicitlyConvertible<T1, T2>::value is false when T1
241// cannot be implicitly converted to T2.
242TEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) {
243 EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value));
244 EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value));
245 EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value));
246 EXPECT_FALSE((ImplicitlyConvertible<Base&, Derived&>::value));
247}
248
zhanyong.wan16cf4732009-05-14 20:55:30 +0000249// Tests KindOf<T>.
250
251TEST(KindOfTest, Bool) {
252 EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT
253}
254
255TEST(KindOfTest, Integer) {
256 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT
257 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT
258 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT
259 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT
260 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT
261 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT
262 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT
263 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT
264 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT
265 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT
266 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64)); // NOLINT
267 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64)); // NOLINT
268 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT
269#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
270 // ssize_t is not defined on Windows and possibly some other OSes.
271 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT
272#endif
273}
274
275TEST(KindOfTest, FloatingPoint) {
276 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT
277 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT
278 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT
279}
280
281TEST(KindOfTest, Other) {
282 EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT
283 EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT
284 EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT
285}
286
287// Tests LosslessArithmeticConvertible<T, U>.
288
289TEST(LosslessArithmeticConvertibleTest, BoolToBool) {
290 EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
291}
292
293TEST(LosslessArithmeticConvertibleTest, BoolToInteger) {
294 EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value));
295 EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value));
296 EXPECT_TRUE(
297 (LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT
298}
299
300TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
301 EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
302 EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
303}
304
305TEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
306 EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
307 EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
308}
309
310TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) {
311 // Unsigned => larger signed is fine.
312 EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value));
313
314 // Unsigned => larger unsigned is fine.
315 EXPECT_TRUE(
316 (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT
317
318 // Signed => unsigned is not fine.
319 EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT
320 EXPECT_FALSE((LosslessArithmeticConvertible<
321 signed char, unsigned int>::value)); // NOLINT
322
323 // Same size and same signedness: fine too.
324 EXPECT_TRUE((LosslessArithmeticConvertible<
325 unsigned char, unsigned char>::value));
326 EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value));
327 EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value));
328 EXPECT_TRUE((LosslessArithmeticConvertible<
329 unsigned long, unsigned long>::value)); // NOLINT
330
331 // Same size, different signedness: not fine.
332 EXPECT_FALSE((LosslessArithmeticConvertible<
333 unsigned char, signed char>::value));
334 EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value));
335 EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value));
336
337 // Larger size => smaller size is not fine.
338 EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT
339 EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value));
340 EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value));
341}
342
343TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) {
344 // Integers cannot be losslessly converted to floating-points, as
345 // the format of the latter is implementation-defined.
346 EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value));
347 EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value));
348 EXPECT_FALSE((LosslessArithmeticConvertible<
349 short, long double>::value)); // NOLINT
350}
351
352TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
353 EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
354 EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
355}
356
357TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) {
358 EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT
359 EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value));
360 EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value));
361}
362
363TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) {
364 // Smaller size => larger size is fine.
365 EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value));
366 EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value));
367 EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value));
368
369 // Same size: fine.
370 EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value));
371 EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value));
372
373 // Larger size => smaller size is not fine.
374 EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value));
375 if (sizeof(double) == sizeof(long double)) { // NOLINT
376 // In some implementations (e.g. MSVC), double and long double
377 // have the same size.
378 EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value));
379 } else {
380 EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value));
381 }
382}
383
shiqiane35fdd92008-12-10 05:08:54 +0000384// Tests that IsAProtocolMessage<T>::value is a compile-time constant.
385TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) {
zhanyong.wane0d051e2009-02-19 00:33:37 +0000386 GMOCK_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value, const_true);
387 GMOCK_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false);
shiqiane35fdd92008-12-10 05:08:54 +0000388}
389
390// Tests that IsAProtocolMessage<T>::value is true when T is
391// ProtocolMessage or a sub-class of it.
392TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) {
zhanyong.wan0ea67f82009-08-14 04:50:02 +0000393 EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value);
shiqiane35fdd92008-12-10 05:08:54 +0000394 EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value);
395#if GMOCK_HAS_PROTOBUF_
396 EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value);
397#endif // GMOCK_HAS_PROTOBUF_
398}
399
400// Tests that IsAProtocolMessage<T>::value is false when T is neither
401// ProtocolMessage nor a sub-class of it.
402TEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) {
403 EXPECT_FALSE(IsAProtocolMessage<int>::value);
404 EXPECT_FALSE(IsAProtocolMessage<const Base>::value);
405}
406
407// Tests IsContainerTest.
408
409class NonContainer {};
410
411TEST(IsContainerTestTest, WorksForNonContainer) {
412 EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0)));
413 EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0)));
414 EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0)));
415}
416
417TEST(IsContainerTestTest, WorksForContainer) {
zhanyong.wan16cf4732009-05-14 20:55:30 +0000418 EXPECT_EQ(sizeof(IsContainer),
419 sizeof(IsContainerTest<std::vector<bool> >(0)));
420 EXPECT_EQ(sizeof(IsContainer),
421 sizeof(IsContainerTest<std::map<int, double> >(0)));
shiqiane35fdd92008-12-10 05:08:54 +0000422}
423
424// Tests the TupleMatches() template function.
425
426TEST(TupleMatchesTest, WorksForSize0) {
427 tuple<> matchers;
428 tuple<> values;
429
430 EXPECT_TRUE(TupleMatches(matchers, values));
431}
432
433TEST(TupleMatchesTest, WorksForSize1) {
434 tuple<Matcher<int> > matchers(Eq(1));
435 tuple<int> values1(1),
436 values2(2);
437
438 EXPECT_TRUE(TupleMatches(matchers, values1));
439 EXPECT_FALSE(TupleMatches(matchers, values2));
440}
441
442TEST(TupleMatchesTest, WorksForSize2) {
443 tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a'));
444 tuple<int, char> values1(1, 'a'),
445 values2(1, 'b'),
446 values3(2, 'a'),
447 values4(2, 'b');
448
449 EXPECT_TRUE(TupleMatches(matchers, values1));
450 EXPECT_FALSE(TupleMatches(matchers, values2));
451 EXPECT_FALSE(TupleMatches(matchers, values3));
452 EXPECT_FALSE(TupleMatches(matchers, values4));
453}
454
455TEST(TupleMatchesTest, WorksForSize5) {
456 tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>, // NOLINT
457 Matcher<string> >
458 matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
459 tuple<int, char, bool, long, string> // NOLINT
460 values1(1, 'a', true, 2L, "hi"),
461 values2(1, 'a', true, 2L, "hello"),
462 values3(2, 'a', true, 2L, "hi");
463
464 EXPECT_TRUE(TupleMatches(matchers, values1));
465 EXPECT_FALSE(TupleMatches(matchers, values2));
466 EXPECT_FALSE(TupleMatches(matchers, values3));
467}
468
469// Tests that Assert(true, ...) succeeds.
470TEST(AssertTest, SucceedsOnTrue) {
471 Assert(true, __FILE__, __LINE__, "This should succeed.");
472 Assert(true, __FILE__, __LINE__); // This should succeed too.
473}
474
zhanyong.wan652540a2009-02-23 23:37:29 +0000475#if GTEST_HAS_DEATH_TEST
shiqiane35fdd92008-12-10 05:08:54 +0000476
477// Tests that Assert(false, ...) generates a fatal failure.
478TEST(AssertTest, FailsFatallyOnFalse) {
479 EXPECT_DEATH({ // NOLINT
480 Assert(false, __FILE__, __LINE__, "This should fail.");
481 }, "");
482
483 EXPECT_DEATH({ // NOLINT
484 Assert(false, __FILE__, __LINE__);
485 }, "");
486}
487
488#endif // GTEST_HAS_DEATH_TEST
489
490// Tests that Expect(true, ...) succeeds.
491TEST(ExpectTest, SucceedsOnTrue) {
492 Expect(true, __FILE__, __LINE__, "This should succeed.");
493 Expect(true, __FILE__, __LINE__); // This should succeed too.
494}
495
496// Tests that Expect(false, ...) generates a non-fatal failure.
497TEST(ExpectTest, FailsNonfatallyOnFalse) {
498 EXPECT_NONFATAL_FAILURE({ // NOLINT
499 Expect(false, __FILE__, __LINE__, "This should fail.");
500 }, "This should fail");
501
502 EXPECT_NONFATAL_FAILURE({ // NOLINT
503 Expect(false, __FILE__, __LINE__);
504 }, "Expectation failed");
505}
506
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000507// Tests LogIsVisible().
508
509class LogIsVisibleTest : public ::testing::Test {
510 protected:
zhanyong.wan81476f22009-06-22 23:30:47 +0000511 virtual void SetUp() {
512 // The code needs to work when both ::string and ::std::string are
513 // defined and the flag is implemented as a
514 // testing::internal::String. In this case, without the call to
515 // c_str(), the compiler will complain that it cannot figure out
516 // whether the String flag should be converted to a ::string or an
517 // ::std::string before being assigned to original_verbose_.
518 original_verbose_ = GMOCK_FLAG(verbose).c_str();
519 }
520
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000521 virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; }
522
523 string original_verbose_;
524};
525
526TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) {
527 GMOCK_FLAG(verbose) = kInfoVerbosity;
528 EXPECT_TRUE(LogIsVisible(INFO));
529 EXPECT_TRUE(LogIsVisible(WARNING));
530}
531
532TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) {
533 GMOCK_FLAG(verbose) = kErrorVerbosity;
534 EXPECT_FALSE(LogIsVisible(INFO));
535 EXPECT_FALSE(LogIsVisible(WARNING));
536}
537
538TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
539 GMOCK_FLAG(verbose) = kWarningVerbosity;
540 EXPECT_FALSE(LogIsVisible(INFO));
541 EXPECT_TRUE(LogIsVisible(WARNING));
542}
543
shiqiane35fdd92008-12-10 05:08:54 +0000544// TODO(wan@google.com): find a way to re-enable these tests.
545#if 0
546
547// Tests the Log() function.
548
549// Verifies that Log() behaves correctly for the given verbosity level
550// and log severity.
551void TestLogWithSeverity(const string& verbosity, LogSeverity severity,
552 bool should_print) {
553 const string old_flag = GMOCK_FLAG(verbose);
554 GMOCK_FLAG(verbose) = verbosity;
555 CaptureTestStdout();
556 Log(severity, "Test log.\n", 0);
557 if (should_print) {
558 EXPECT_PRED2(RE::FullMatch,
559 GetCapturedTestStdout(),
560 severity == WARNING ?
561 "\nGMOCK WARNING:\nTest log\\.\nStack trace:\n[\\s\\S]*" :
562 "\nTest log\\.\nStack trace:\n[\\s\\S]*");
563 } else {
564 EXPECT_EQ("", GetCapturedTestStdout());
565 }
566 GMOCK_FLAG(verbose) = old_flag;
567}
568
569// Tests that when the stack_frames_to_skip parameter is negative,
570// Log() doesn't include the stack trace in the output.
571TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
572 GMOCK_FLAG(verbose) = kInfoVerbosity;
573 CaptureTestStdout();
574 Log(INFO, "Test log.\n", -1);
575 EXPECT_EQ("\nTest log.\n", GetCapturedTestStdout());
576}
577
578// Tests that in opt mode, a positive stack_frames_to_skip argument is
579// treated as 0.
580TEST(LogTest, NoSkippingStackFrameInOptMode) {
581 CaptureTestStdout();
582 Log(WARNING, "Test log.\n", 100);
583 const string log = GetCapturedTestStdout();
584#ifdef NDEBUG
585 // In opt mode, no stack frame should be skipped.
586 EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n"
587 "Test log\\.\n"
588 "Stack trace:\n"
589 ".+"));
590#else
591 // In dbg mode, the stack frames should be skipped.
592 EXPECT_EQ("\nGMOCK WARNING:\n"
593 "Test log.\n"
594 "Stack trace:\n", log);
595#endif // NDEBUG
596}
597
598// Tests that all logs are printed when the value of the
599// --gmock_verbose flag is "info".
600TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) {
601 TestLogWithSeverity(kInfoVerbosity, INFO, true);
602 TestLogWithSeverity(kInfoVerbosity, WARNING, true);
603}
604
605// Tests that only warnings are printed when the value of the
606// --gmock_verbose flag is "warning".
607TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) {
608 TestLogWithSeverity(kWarningVerbosity, INFO, false);
609 TestLogWithSeverity(kWarningVerbosity, WARNING, true);
610}
611
612// Tests that no logs are printed when the value of the
613// --gmock_verbose flag is "error".
614TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) {
615 TestLogWithSeverity(kErrorVerbosity, INFO, false);
616 TestLogWithSeverity(kErrorVerbosity, WARNING, false);
617}
618
619// Tests that only warnings are printed when the value of the
620// --gmock_verbose flag is invalid.
621TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
622 TestLogWithSeverity("invalid", INFO, false);
623 TestLogWithSeverity("invalid", WARNING, true);
624}
625
626#endif // 0
627
628TEST(TypeTraitsTest, true_type) {
629 EXPECT_TRUE(true_type::value);
630}
631
632TEST(TypeTraitsTest, false_type) {
633 EXPECT_FALSE(false_type::value);
634}
635
636TEST(TypeTraitsTest, is_reference) {
637 EXPECT_FALSE(is_reference<int>::value);
638 EXPECT_FALSE(is_reference<char*>::value);
639 EXPECT_TRUE(is_reference<const int&>::value);
640}
641
642TEST(TypeTraitsTest, is_pointer) {
643 EXPECT_FALSE(is_pointer<int>::value);
644 EXPECT_FALSE(is_pointer<char&>::value);
645 EXPECT_TRUE(is_pointer<const int*>::value);
646}
647
648TEST(TypeTraitsTest, type_equals) {
649 EXPECT_FALSE((type_equals<int, const int>::value));
650 EXPECT_FALSE((type_equals<int, int&>::value));
651 EXPECT_FALSE((type_equals<int, double>::value));
652 EXPECT_TRUE((type_equals<char, char>::value));
653}
654
655TEST(TypeTraitsTest, remove_reference) {
656 EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value));
657 EXPECT_TRUE((type_equals<const int,
658 remove_reference<const int&>::type>::value));
659 EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value));
660 EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value));
661}
662
663// TODO(wan@google.com): find a way to re-enable these tests.
664#if 0
665
666// Verifies that Log() behaves correctly for the given verbosity level
667// and log severity.
668string GrabOutput(void(*logger)(), const char* verbosity) {
669 const string saved_flag = GMOCK_FLAG(verbose);
670 GMOCK_FLAG(verbose) = verbosity;
671 CaptureTestStdout();
672 logger();
673 GMOCK_FLAG(verbose) = saved_flag;
674 return GetCapturedTestStdout();
675}
676
677class DummyMock {
678 public:
679 MOCK_METHOD0(TestMethod, void());
680 MOCK_METHOD1(TestMethodArg, void(int dummy));
681};
682
683void ExpectCallLogger() {
684 DummyMock mock;
685 EXPECT_CALL(mock, TestMethod());
686 mock.TestMethod();
687};
688
689// Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
690TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
691 EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity),
692 HasSubstr("EXPECT_CALL(mock, TestMethod())"));
693}
694
695// Verifies that EXPECT_CALL doesn't log
696// if the --gmock_verbose flag is set to "warning".
697TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
698 EXPECT_EQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity));
699}
700
701// Verifies that EXPECT_CALL doesn't log
702// if the --gmock_verbose flag is set to "error".
703TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) {
704 EXPECT_EQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity));
705}
706
707void OnCallLogger() {
708 DummyMock mock;
709 ON_CALL(mock, TestMethod());
710};
711
712// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
713TEST(OnCallTest, LogsWhenVerbosityIsInfo) {
714 EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity),
715 HasSubstr("ON_CALL(mock, TestMethod())"));
716}
717
718// Verifies that ON_CALL doesn't log
719// if the --gmock_verbose flag is set to "warning".
720TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
721 EXPECT_EQ("", GrabOutput(OnCallLogger, kWarningVerbosity));
722}
723
724// Verifies that ON_CALL doesn't log if
725// the --gmock_verbose flag is set to "error".
726TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
727 EXPECT_EQ("", GrabOutput(OnCallLogger, kErrorVerbosity));
728}
729
730void OnCallAnyArgumentLogger() {
731 DummyMock mock;
732 ON_CALL(mock, TestMethodArg(_));
733}
734
735// Verifies that ON_CALL prints provided _ argument.
736TEST(OnCallTest, LogsAnythingArgument) {
737 EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity),
738 HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
739}
740
741#endif // 0
742
zhanyong.wanb8243162009-06-04 05:48:20 +0000743// Tests ArrayEq().
744
745TEST(ArrayEqTest, WorksForDegeneratedArrays) {
746 EXPECT_TRUE(ArrayEq(5, 5L));
747 EXPECT_FALSE(ArrayEq('a', 0));
748}
749
750TEST(ArrayEqTest, WorksForOneDimensionalArrays) {
751 const int a[] = { 0, 1 };
752 long b[] = { 0, 1 };
753 EXPECT_TRUE(ArrayEq(a, b));
754 EXPECT_TRUE(ArrayEq(a, 2, b));
755
756 b[0] = 2;
757 EXPECT_FALSE(ArrayEq(a, b));
758 EXPECT_FALSE(ArrayEq(a, 1, b));
759}
760
761TEST(ArrayEqTest, WorksForTwoDimensionalArrays) {
762 const char a[][3] = { "hi", "lo" };
763 const char b[][3] = { "hi", "lo" };
764 const char c[][3] = { "hi", "li" };
765
766 EXPECT_TRUE(ArrayEq(a, b));
767 EXPECT_TRUE(ArrayEq(a, 2, b));
768
769 EXPECT_FALSE(ArrayEq(a, c));
770 EXPECT_FALSE(ArrayEq(a, 2, c));
771}
772
773// Tests ArrayAwareFind().
774
775TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) {
776 const char a[] = "hello";
777 EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o'));
778 EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x'));
779}
780
781TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) {
782 int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
783 const int b[2] = { 2, 3 };
784 EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b));
785
786 const int c[2] = { 6, 7 };
787 EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c));
788}
789
790// Tests CopyArray().
791
792TEST(CopyArrayTest, WorksForDegeneratedArrays) {
793 int n = 0;
794 CopyArray('a', &n);
795 EXPECT_EQ('a', n);
796}
797
798TEST(CopyArrayTest, WorksForOneDimensionalArrays) {
799 const char a[3] = "hi";
800 int b[3];
801 CopyArray(a, &b);
802 EXPECT_TRUE(ArrayEq(a, b));
803
804 int c[3];
805 CopyArray(a, 3, c);
806 EXPECT_TRUE(ArrayEq(a, c));
807}
808
809TEST(CopyArrayTest, WorksForTwoDimensionalArrays) {
810 const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } };
811 int b[2][3];
812 CopyArray(a, &b);
813 EXPECT_TRUE(ArrayEq(a, b));
814
815 int c[2][3];
816 CopyArray(a, 2, c);
817 EXPECT_TRUE(ArrayEq(a, c));
818}
819
820// Tests NativeArray.
821
822TEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) {
823 const int a[3] = { 0, 1, 2 };
824 NativeArray<int> na(a, kReference);
825 EXPECT_EQ(3, na.size());
826 EXPECT_EQ(a, na.begin());
827}
828
829TEST(NativeArrayTest, ConstructorFromTupleWorks) {
830 int a[3] = { 0, 1, 2 };
zhanyong.wan90c90f92009-06-17 22:11:04 +0000831 int* const p = a;
zhanyong.wanb8243162009-06-04 05:48:20 +0000832 // Tests with a plain pointer.
zhanyong.wan90c90f92009-06-17 22:11:04 +0000833 NativeArray<int> na(make_tuple(p, 3U), kReference);
zhanyong.wanb8243162009-06-04 05:48:20 +0000834 EXPECT_EQ(a, na.begin());
835
836 const linked_ptr<char> b(new char);
837 *b = 'a';
838 // Tests with a smart pointer.
839 NativeArray<char> nb(make_tuple(b, 1), kCopy);
840 EXPECT_NE(b.get(), nb.begin());
841 EXPECT_EQ('a', nb.begin()[0]);
842}
843
844TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) {
845 typedef int Array[2];
846 Array* a = new Array[1];
847 (*a)[0] = 0;
848 (*a)[1] = 1;
849 NativeArray<int> na(*a, kCopy);
850 EXPECT_NE(*a, na.begin());
851 delete[] a;
852 EXPECT_EQ(0, na.begin()[0]);
853 EXPECT_EQ(1, na.begin()[1]);
854
855 // We rely on the heap checker to verify that na deletes the copy of
856 // array.
857}
858
859TEST(NativeArrayTest, TypeMembersAreCorrect) {
860 StaticAssertTypeEq<char, NativeArray<char>::value_type>();
861 StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>();
862
863 StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>();
864 StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>();
865}
866
867TEST(NativeArrayTest, MethodsWork) {
868 const int a[] = { 0, 1, 2 };
869 NativeArray<int> na(a, kCopy);
870 ASSERT_EQ(3, na.size());
871 EXPECT_EQ(3, na.end() - na.begin());
872
873 NativeArray<int>::const_iterator it = na.begin();
874 EXPECT_EQ(0, *it);
875 ++it;
876 EXPECT_EQ(1, *it);
877 it++;
878 EXPECT_EQ(2, *it);
879 ++it;
880 EXPECT_EQ(na.end(), it);
881
882 EXPECT_THAT(na, Eq(na));
883
884 NativeArray<int> na2(a, kReference);
885 EXPECT_THAT(na, Eq(na2));
886
887 const int b1[] = { 0, 1, 1 };
888 const int b2[] = { 0, 1, 2, 3 };
889 EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, kReference))));
890 EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, kCopy))));
891}
892
893TEST(NativeArrayTest, WorksForTwoDimensionalArray) {
894 const char a[2][3] = { "hi", "lo" };
895 NativeArray<char[3]> na(a, kReference);
896 ASSERT_EQ(2, na.size());
897 EXPECT_EQ(a, na.begin());
898}
899
900// Tests StlContainerView.
901
902TEST(StlContainerViewTest, WorksForStlContainer) {
903 StaticAssertTypeEq<std::vector<int>,
904 StlContainerView<std::vector<int> >::type>();
905 StaticAssertTypeEq<const std::vector<double>&,
906 StlContainerView<std::vector<double> >::const_reference>();
907
908 typedef std::vector<char> Chars;
909 Chars v1;
910 const Chars& v2(StlContainerView<Chars>::ConstReference(v1));
911 EXPECT_EQ(&v1, &v2);
912
913 v1.push_back('a');
914 Chars v3 = StlContainerView<Chars>::Copy(v1);
915 EXPECT_THAT(v3, Eq(v3));
916}
917
918TEST(StlContainerViewTest, WorksForStaticNativeArray) {
919 StaticAssertTypeEq<NativeArray<int>,
920 StlContainerView<int[3]>::type>();
921 StaticAssertTypeEq<NativeArray<double>,
922 StlContainerView<const double[4]>::type>();
923 StaticAssertTypeEq<NativeArray<char[3]>,
924 StlContainerView<const char[2][3]>::type>();
925
926 StaticAssertTypeEq<const NativeArray<int>,
927 StlContainerView<int[2]>::const_reference>();
928
929 int a1[3] = { 0, 1, 2 };
930 NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
931 EXPECT_EQ(3, a2.size());
932 EXPECT_EQ(a1, a2.begin());
933
934 const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1);
935 ASSERT_EQ(3, a3.size());
936 EXPECT_EQ(0, a3.begin()[0]);
937 EXPECT_EQ(1, a3.begin()[1]);
938 EXPECT_EQ(2, a3.begin()[2]);
939
940 // Makes sure a1 and a3 aren't aliases.
941 a1[0] = 3;
942 EXPECT_EQ(0, a3.begin()[0]);
943}
944
945TEST(StlContainerViewTest, WorksForDynamicNativeArray) {
946 StaticAssertTypeEq<NativeArray<int>,
947 StlContainerView<tuple<const int*, size_t> >::type>();
948 StaticAssertTypeEq<NativeArray<double>,
949 StlContainerView<tuple<linked_ptr<double>, int> >::type>();
950
951 StaticAssertTypeEq<const NativeArray<int>,
952 StlContainerView<tuple<const int*, int> >::const_reference>();
953
954 int a1[3] = { 0, 1, 2 };
zhanyong.wan90c90f92009-06-17 22:11:04 +0000955 const int* const p1 = a1;
zhanyong.wanb8243162009-06-04 05:48:20 +0000956 NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >::
zhanyong.wan90c90f92009-06-17 22:11:04 +0000957 ConstReference(make_tuple(p1, 3));
zhanyong.wanb8243162009-06-04 05:48:20 +0000958 EXPECT_EQ(3, a2.size());
959 EXPECT_EQ(a1, a2.begin());
960
961 const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >::
zhanyong.wan2661c682009-06-09 05:42:12 +0000962 Copy(make_tuple(static_cast<int*>(a1), 3));
zhanyong.wanb8243162009-06-04 05:48:20 +0000963 ASSERT_EQ(3, a3.size());
964 EXPECT_EQ(0, a3.begin()[0]);
965 EXPECT_EQ(1, a3.begin()[1]);
966 EXPECT_EQ(2, a3.begin()[2]);
967
968 // Makes sure a1 and a3 aren't aliases.
969 a1[0] = 3;
970 EXPECT_EQ(0, a3.begin()[0]);
971}
972
shiqiane35fdd92008-12-10 05:08:54 +0000973} // namespace
974} // namespace internal
975} // namespace testing