From 90c90f9250f19c3f3a5c2c0887c1d9e414afe41b Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 17 Jun 2009 22:11:04 +0000 Subject: [PATCH] Switches from Boost TR1 tuple to gtest's TR1 tuple. --- Makefile.am | 12 ++++ README | 81 ++++++++++------------- msvc/gmock_config.vsprops | 7 +- test/gmock-generated-actions_test.cc | 96 +++++++++++++++++++--------- test/gmock-internal-utils_test.cc | 6 +- 5 files changed, 114 insertions(+), 88 deletions(-) diff --git a/Makefile.am b/Makefile.am index 927c4982..196b9271 100644 --- a/Makefile.am +++ b/Makefile.am @@ -145,6 +145,18 @@ test_gmock_no_rtti_test_SOURCES = test/gmock-spec-builders_test.cc \ test_gmock_no_rtti_test_CXXFLAGS = $(AM_CXXFLAGS) -fno-rtti -DGTEST_HAS_RTTI=0 test_gmock_no_rtti_test_LDADD = $(GTEST_LIBS) +# A sanity test for verifying that Google Mock works with Google +# Test's TR1 tuple implementation. We pick +# gmock-spec-builders_test.cc as it exercises all components of Google +# Mock. +TESTS += test/gmock_use_own_tuple_test +check_PROGRAMS += test/gmock_use_own_tuple_test +test_gmock_use_own_tuple_test_SOURCES = test/gmock-spec-builders_test.cc \ + src/gmock-all.cc +test_gmock_use_own_tuple_test_CXXFLAGS = \ + $(AM_CXXFLAGS) -DGTEST_USE_OWN_TR1_TUPLE=1 +test_gmock_use_own_tuple_test_LDADD = $(GTEST_LIBS) + # The following tests depend on the presence of a Python installation and are # keyed off of it. We only add them to the TESTS variable when a Python # interpreter is available. TODO(chandlerc@google.com): While we currently only diff --git a/README b/README index f14279ce..9cbda5d5 100644 --- a/README +++ b/README @@ -40,7 +40,7 @@ testing framework for writing tests. It works with Google Test (http://code.google.com/p/googletest/) out of the box. You can use either the copy of Google Test that comes with Google Mock, or a compatible version you already have. This version of Google Mock -requires Google Test 1.3.0. +requires Google Test 1.4.0. You can also easily configure Google Mock to work with another testing framework of your choice; although it will still need Google Test as @@ -57,8 +57,7 @@ package (as described below): * GNU-compatible Make or "gmake" * POSIX-standard shell * POSIX(-2) Regular Expressions (regex.h) - * gcc 4.0 or newer, or gcc 3.4 or newer with the tr1 tuple library - (from Boost or other vendors). + * gcc 3.4 or newer. Furthermore, if you are building Google Mock from a VCS Checkout (also described below), there are further requirements: @@ -69,12 +68,6 @@ described below), there are further requirements: ### Windows Requirements ### * Microsoft Visual C++ 8.0 SP1 or newer - * An implementation of the tr1 tuple C++ library (You can get it for - free from http://www.boost.org/. We have verified that version - 1.36.0 works. One caveat is this implementation exposes a bug in - Visual C++'s header when exceptions are disabled. - Therefore your project must enable exceptions for this - configuration to work.) ### Mac OS X Requirements ### * Mac OS X 10.4 Tiger or newer @@ -141,6 +134,32 @@ which contains all of the source code. Here are some examples in Linux: tar -xvjf gmock-X.Y.Z.tar.bz2 unzip gmock-X.Y.Z.zip +Choosing a TR1 Tuple Library +---------------------------- +Google Mock uses the C++ Technical Report 1 (TR1) tuple library +heavily. Unfortunately TR1 tuple is not yet widely available with all +compilers. The good news is that Google Test 1.4.0+ implements a +subset of TR1 tuple that's enough for Google Mock's need. Google Mock +will automatically use that implementation when the compiler doesn't +provide TR1 tuple. + +Usually you don't need to care about which tuple library Google Test +and Google Mock use. However, if your project already uses TR1 tuple, +you need to tell Google Test and Google Mock to use the same TR1 tuple +library the rest of your project uses (this requirement is new in +Google Test 1.4.0 and Google Mock 1.2.0, so you may need to take care +of it when upgrading from an earlier version), or the two tuple +implementations will clash. To do that, add + + -DGTEST_USE_OWN_TR1_TUPLE=0 + +to the compiler flags while compiling Google Test, Google Mock, and +your tests. + +If you want to use Boost's TR1 tuple library with Google Mock, please +refer to the Boost website (http://www.boost.org/) for how to obtain +it and set it up. + Building the Source ------------------- ### Linux and Mac OS X (without Xcode) ### @@ -236,46 +255,15 @@ separately. ### Windows ### The msvc/ directory contains VC++ 2005 projects for building Google -Mock and selected tests. In order to build Google Mock you must have -an implementation of TR1 tuple. One library that provides such -implementation is Boost. If you choose to use Boost, download it from -www.boost.org and install it on your system. Note that Boost TR1 tuple -is a header-only library, so the installation only involves unpacking -it to a suitable location - you don't need to compile it or download a -pre-compiled Boost binary. - -Since Boost is quite large, you may prefer to only install the files -actually needed by Google Mock. If so, you can download TR1 tuple -without other parts of Boost from -http://code.google.com/p/googlemock/downloads/list. - -After that you have two options: either set up Boost globally or -modify the Google Mock project to point to your copy of Boost. The -former will let all your tests use the same Boost library while the -latter will allow each of your projects use its own copy. You can also -use a hybrid solution: your project settings will override the -system-wide one. - -For example, if you unpacked boost v1.36.0 into C:\boost: -To set up Boost such that all projects can use it: - * Assuming you are using the Visual Studio 2005 IDE, select Tools | - Options | Projects And Solutions | VC++ Directories. - * In the "Show directories for" drop-down select Include Files. Add - C:\boost\boost_1_36_0\boost\tr1\tr1 and C:\boost\boost_1_36_0 to the - list of directories. - -To configure your project to point to that version of Boost, replace -the value of the BoostDir user macro with C:\boost\boost_1_36_0 in the -msvc/gmock_config.vsprops file. You can use any text editor to edit -that file. +Mock and selected tests. If you want to use a version of Google Test other then the one bundled with Google Mock, change the value of the GTestDir macro in gmock_config.vsprop to point to the new location. -After configuring Boost, just open msvc/gmock.sln and build the library and -tests. If you want to create your own project to use with Google Mock, you'll -have to configure it to use the gmock_config propety sheet. For that: +Open msvc/gmock.sln and build the library and tests. If you want to +create your own project to use with Google Mock, you'll have to +configure it to use the gmock_config propety sheet. For that: * Open the Property Manager window (View | Other Windows | Property Manager) * Right-click on your project and select "Add Existing Property Sheet..." * Navigate to gmock_config.vsprops and select it. @@ -320,11 +308,6 @@ something like the following will do: g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \ path/to/your_test.cc libgmock.a -o your_test -On Windows, you'll also need to add the include path for the boost -headers to the compiler command line. See -http://www.boost.org/doc/libs/1_36_0/doc/html/boost_tr1/usage.html for -how to do it. - Regenerating Source Files ------------------------- Some of Google Mock's source files are generated from templates (not diff --git a/msvc/gmock_config.vsprops b/msvc/gmock_config.vsprops index fa596ef9..a68c32e1 100644 --- a/msvc/gmock_config.vsprops +++ b/msvc/gmock_config.vsprops @@ -6,15 +6,10 @@ > - diff --git a/test/gmock-generated-actions_test.cc b/test/gmock-generated-actions_test.cc index 84e5a413..d0b2ddc9 100644 --- a/test/gmock-generated-actions_test.cc +++ b/test/gmock-generated-actions_test.cc @@ -269,13 +269,19 @@ TEST(InvokeTest, FunctionThatTakes6Arguments) { EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); } +// A helper that turns the type of a C-string literal from const +// char[N] to const char*. +inline const char* CharPtr(const char* s) { return s; } + // Tests using Invoke() with a 7-argument function. TEST(InvokeTest, FunctionThatTakes7Arguments) { Action a = Invoke(Concat7); EXPECT_EQ("1234567", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7")))); } // Tests using Invoke() with a 8-argument function. @@ -284,7 +290,9 @@ TEST(InvokeTest, FunctionThatTakes8Arguments) { const char*, const char*, const char*, const char*)> a = Invoke(Concat8); EXPECT_EQ("12345678", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8")))); } // Tests using Invoke() with a 9-argument function. @@ -293,7 +301,9 @@ TEST(InvokeTest, FunctionThatTakes9Arguments) { const char*, const char*, const char*, const char*, const char*)> a = Invoke(Concat9); EXPECT_EQ("123456789", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9")))); } // Tests using Invoke() with a 10-argument function. @@ -301,15 +311,18 @@ TEST(InvokeTest, FunctionThatTakes10Arguments) { Action a = Invoke(Concat10); - EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6", - "7", "8", "9", "0"))); + EXPECT_EQ("1234567890", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9"), + CharPtr("0")))); } // Tests using Invoke() with functions with parameters declared as Unused. TEST(InvokeTest, FunctionWithUnusedParameters) { Action a1 = Invoke(SumOfFirst2); - EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, "hi"))); + EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi")))); Action a2 = Invoke(SumOfFirst2); @@ -321,7 +334,7 @@ TEST(InvokeTest, MethodWithUnusedParameters) { Foo foo; Action a1 = Invoke(&foo, &Foo::SumOfLast2); - EXPECT_EQ(12, a1.Perform(make_tuple("hi", true, 10, 2))); + EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2))); Action a2 = Invoke(&foo, &Foo::SumOfLast2); @@ -400,7 +413,9 @@ TEST(InvokeMethodTest, MethodThatTakes7Arguments) { const char*, const char*, const char*)> a = Invoke(&foo, &Foo::Concat7); EXPECT_EQ("1234567", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7"))); + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7")))); } // Tests using Invoke() with a 8-argument method. @@ -410,7 +425,9 @@ TEST(InvokeMethodTest, MethodThatTakes8Arguments) { const char*, const char*, const char*, const char*)> a = Invoke(&foo, &Foo::Concat8); EXPECT_EQ("12345678", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8"))); + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8")))); } // Tests using Invoke() with a 9-argument method. @@ -420,7 +437,9 @@ TEST(InvokeMethodTest, MethodThatTakes9Arguments) { const char*, const char*, const char*, const char*, const char*)> a = Invoke(&foo, &Foo::Concat9); EXPECT_EQ("123456789", - a.Perform(make_tuple("1", "2", "3", "4", "5", "6", "7", "8", "9"))); + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9")))); } // Tests using Invoke() with a 10-argument method. @@ -429,8 +448,11 @@ TEST(InvokeMethodTest, MethodThatTakes10Arguments) { Action a = Invoke(&foo, &Foo::Concat10); - EXPECT_EQ("1234567890", a.Perform(make_tuple("1", "2", "3", "4", "5", "6", - "7", "8", "9", "0"))); + EXPECT_EQ("1234567890", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9"), + CharPtr("0")))); } // Tests using Invoke(f) as an action of a compatible type. @@ -665,7 +687,7 @@ TEST(WithArgsTest, TwoArgs) { Action a = WithArgs<0, 2>(Invoke(Binary)); const char s[] = "Hello"; - EXPECT_EQ(s + 2, a.Perform(make_tuple(s, 0.5, 2))); + EXPECT_EQ(s + 2, a.Perform(make_tuple(CharPtr(s), 0.5, 2))); } // Tests using WithArgs with an action that takes 3 arguments. @@ -679,7 +701,8 @@ TEST(WithArgsTest, ThreeArgs) { TEST(WithArgsTest, FourArgs) { Action a = WithArgs<4, 3, 1, 0>(Invoke(Concat4)); - EXPECT_EQ("4310", a.Perform(make_tuple("0", "1", 2.5, "3", "4"))); + EXPECT_EQ("4310", a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), 2.5, + CharPtr("3"), CharPtr("4")))); } // Tests using WithArgs with an action that takes 5 arguments. @@ -687,42 +710,53 @@ TEST(WithArgsTest, FiveArgs) { Action a = WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5)); - EXPECT_EQ("43210", a.Perform(make_tuple("0", "1", "2", "3", "4"))); + EXPECT_EQ("43210", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3"), CharPtr("4")))); } // Tests using WithArgs with an action that takes 6 arguments. TEST(WithArgsTest, SixArgs) { Action a = WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6)); - EXPECT_EQ("012210", a.Perform(make_tuple("0", "1", "2"))); + EXPECT_EQ("012210", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2")))); } // Tests using WithArgs with an action that takes 7 arguments. TEST(WithArgsTest, SevenArgs) { Action a = WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7)); - EXPECT_EQ("0123210", a.Perform(make_tuple("0", "1", "2", "3"))); + EXPECT_EQ("0123210", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3")))); } // Tests using WithArgs with an action that takes 8 arguments. TEST(WithArgsTest, EightArgs) { Action a = WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8)); - EXPECT_EQ("01230123", a.Perform(make_tuple("0", "1", "2", "3"))); + EXPECT_EQ("01230123", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3")))); } // Tests using WithArgs with an action that takes 9 arguments. TEST(WithArgsTest, NineArgs) { Action a = WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9)); - EXPECT_EQ("012312323", a.Perform(make_tuple("0", "1", "2", "3"))); + EXPECT_EQ("012312323", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3")))); } // Tests using WithArgs with an action that takes 10 arguments. TEST(WithArgsTest, TenArgs) { Action a = WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10)); - EXPECT_EQ("0123210123", a.Perform(make_tuple("0", "1", "2", "3"))); + EXPECT_EQ("0123210123", + a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"), + CharPtr("3")))); } // Tests using WithArgs with an action that is not Invoke(). @@ -736,7 +770,7 @@ class SubstractAction : public ActionInterface { // NOLINT TEST(WithArgsTest, NonInvokeAction) { Action a = // NOLINT WithArgs<2, 1>(MakeAction(new SubstractAction)); - EXPECT_EQ(8, a.Perform(make_tuple("hi", 2, 10))); + EXPECT_EQ(8, a.Perform(make_tuple(CharPtr("hi"), 2, 10))); } // Tests using WithArgs to pass all original arguments in the original order. @@ -758,7 +792,7 @@ TEST(WithArgsTest, ReversedArgumentOrder) { Action a = // NOLINT WithArgs<1, 0>(Invoke(Binary)); const char s[] = "Hello"; - EXPECT_EQ(s + 2, a.Perform(make_tuple(2, s))); + EXPECT_EQ(s + 2, a.Perform(make_tuple(2, CharPtr(s)))); } // Tests using WithArgs with compatible, but not identical, argument types. @@ -1123,16 +1157,16 @@ TEST(ActionMacroTest, CanDefineOverloadedActions) { typedef Action MyAction; const MyAction a1 = OverloadedAction(); - EXPECT_STREQ("hello", a1.Perform(make_tuple(false, "world"))); - EXPECT_STREQ("world", a1.Perform(make_tuple(true, "world"))); + EXPECT_STREQ("hello", a1.Perform(make_tuple(false, CharPtr("world")))); + EXPECT_STREQ("world", a1.Perform(make_tuple(true, CharPtr("world")))); const MyAction a2 = OverloadedAction("hi"); - EXPECT_STREQ("hi", a2.Perform(make_tuple(false, "world"))); - EXPECT_STREQ("world", a2.Perform(make_tuple(true, "world"))); + EXPECT_STREQ("hi", a2.Perform(make_tuple(false, CharPtr("world")))); + EXPECT_STREQ("world", a2.Perform(make_tuple(true, CharPtr("world")))); const MyAction a3 = OverloadedAction("hi", "you"); - EXPECT_STREQ("hi", a3.Perform(make_tuple(true, "world"))); - EXPECT_STREQ("you", a3.Perform(make_tuple(false, "world"))); + EXPECT_STREQ("hi", a3.Perform(make_tuple(true, CharPtr("world")))); + EXPECT_STREQ("you", a3.Perform(make_tuple(false, CharPtr("world")))); } // Tests ACTION_Pn where n >= 3. @@ -1224,8 +1258,8 @@ TEST(ActionPnMacroTest, SimpleTypePromotion) { PadArgument(std::string("foo"), 'r'); Action promo = PadArgument("foo", static_cast('r')); - EXPECT_EQ("foobar", no_promo.Perform(make_tuple("ba"))); - EXPECT_EQ("foobar", promo.Perform(make_tuple("ba"))); + EXPECT_EQ("foobar", no_promo.Perform(make_tuple(CharPtr("ba")))); + EXPECT_EQ("foobar", promo.Perform(make_tuple(CharPtr("ba")))); } // Tests that we can partially restrict parameter types using a @@ -1470,7 +1504,7 @@ TEST(DeleteArgActionTest, TenArgs) { const Action a1 = DeleteArg<9>(); EXPECT_FALSE(is_deleted); - a1.Perform(make_tuple(true, 5, 6, "hi", false, 7, 8, 9, 10, t)); + a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t)); EXPECT_TRUE(is_deleted); } diff --git a/test/gmock-internal-utils_test.cc b/test/gmock-internal-utils_test.cc index 912ee357..9ab15af2 100644 --- a/test/gmock-internal-utils_test.cc +++ b/test/gmock-internal-utils_test.cc @@ -812,8 +812,9 @@ TEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) { TEST(NativeArrayTest, ConstructorFromTupleWorks) { int a[3] = { 0, 1, 2 }; + int* const p = a; // Tests with a plain pointer. - NativeArray na(make_tuple(a, 3U), kReference); + NativeArray na(make_tuple(p, 3U), kReference); EXPECT_EQ(a, na.begin()); const linked_ptr b(new char); @@ -935,8 +936,9 @@ TEST(StlContainerViewTest, WorksForDynamicNativeArray) { StlContainerView >::const_reference>(); int a1[3] = { 0, 1, 2 }; + const int* const p1 = a1; NativeArray a2 = StlContainerView >:: - ConstReference(make_tuple(a1, 3)); + ConstReference(make_tuple(p1, 3)); EXPECT_EQ(3, a2.size()); EXPECT_EQ(a1, a2.begin());