diff options
author | jgm <jgm@google.com> | 2012-11-15 15:50:36 +0000 |
---|---|---|
committer | jgm <jgm@google.com> | 2012-11-15 15:50:36 +0000 |
commit | 38513a8bb154f0b6d0a4088814fe92552696d465 (patch) | |
tree | 95e50c814602bcfb0b62cc06f5843feefa9b1a1e /test | |
parent | ada23475e27babd85fb9c13250243f6acfd3ffd8 (diff) | |
download | googletest-38513a8bb154f0b6d0a4088814fe92552696d465.tar.gz googletest-38513a8bb154f0b6d0a4088814fe92552696d465.tar.bz2 googletest-38513a8bb154f0b6d0a4088814fe92552696d465.zip |
Unfortunately, the svn repo is a bit out of date. This commit contains 8
changes that haven't made it to svn. The descriptions of each change are listed
below.
- Fixes some python shebang lines.
- Add ElementsAreArray overloads to gmock. ElementsAreArray now makes a copy of
its input elements before the conversion to a Matcher. ElementsAreArray can
now take a vector as input. ElementsAreArray can now take an iterator pair as
input.
- Templatize MatchAndExplain to allow independent string types for the matcher
and matchee. I also templatized the ConstCharPointer version of
MatchAndExplain to avoid calls with "char*" from using the new templated
MatchAndExplain.
- Fixes the bug where the constructor of the return type of ElementsAre() saves
a reference instead of a copy of the arguments.
- Extends ElementsAre() to accept arrays whose sizes aren't known.
- Switches gTest's internal FilePath class from testing::internal::String to
std::string. testing::internal::String was introduced when gTest couldn't
depend on std::string. It's now deprecated.
- Switches gTest & gMock from using testing::internal::String objects to
std::string. Some static methods of String are still in use. We may be able
to remove some but not all of them. In particular, String::Format() should
eventually be removed as it truncates the result at 4096 characters, often
causing problems.
Diffstat (limited to 'test')
-rw-r--r-- | test/gmock-generated-matchers_test.cc | 97 | ||||
-rw-r--r-- | test/gmock-internal-utils_test.cc | 18 | ||||
-rw-r--r-- | test/gmock-matchers_test.cc | 1 | ||||
-rw-r--r-- | test/gmock-nice-strict_test.cc | 4 | ||||
-rw-r--r-- | test/gmock-spec-builders_test.cc | 29 |
5 files changed, 115 insertions, 34 deletions
diff --git a/test/gmock-generated-matchers_test.cc b/test/gmock-generated-matchers_test.cc index b35c4505..4b7e6e05 100644 --- a/test/gmock-generated-matchers_test.cc +++ b/test/gmock-generated-matchers_test.cc @@ -77,6 +77,7 @@ using testing::Ref; using testing::StaticAssertTypeEq; using testing::StrEq; using testing::Value; +using testing::internal::ElementsAreArrayMatcher; using testing::internal::string; // Returns the description of the given matcher. @@ -527,6 +528,51 @@ TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) { ElementsAre('l', 'o', '\0'))); } +TEST(ElementsAreTest, AcceptsStringLiteral) { + string array[] = { "hi", "one", "two" }; + EXPECT_THAT(array, ElementsAre("hi", "one", "two")); + EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too"))); +} + +#ifndef _MSC_VER + +// The following test passes a value of type const char[] to a +// function template that expects const T&. Some versions of MSVC +// generates a compiler error C2665 for that. We believe it's a bug +// in MSVC. Therefore this test is #if-ed out for MSVC. + +// Declared here with the size unknown. Defined AFTER the following test. +extern const char kHi[]; + +TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) { + // The size of kHi is not known in this test, but ElementsAre() should + // still accept it. + + string array1[] = { "hi" }; + EXPECT_THAT(array1, ElementsAre(kHi)); + + string array2[] = { "ho" }; + EXPECT_THAT(array2, Not(ElementsAre(kHi))); +} + +const char kHi[] = "hi"; + +#endif // _MSC_VER + +TEST(ElementsAreTest, MakesCopyOfArguments) { + int x = 1; + int y = 2; + // This should make a copy of x and y. + ::testing::internal::ElementsAreMatcher2<int, int> polymorphic_matcher = + ElementsAre(x, y); + // Changing x and y now shouldn't affect the meaning of the above matcher. + x = y = 0; + const int array1[] = { 1, 2 }; + EXPECT_THAT(array1, polymorphic_matcher); + const int array2[] = { 0, 0 }; + EXPECT_THAT(array2, Not(polymorphic_matcher)); +} + // Tests for ElementsAreArray(). Since ElementsAreArray() shares most // of the implementation with ElementsAre(), we don't test it as // thoroughly here. @@ -576,6 +622,39 @@ TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); } +TEST(ElementsAreArrayTest, CanBeCreatedWithVector) { + const int a[] = { 1, 2, 3 }; + vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); + const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a)); + EXPECT_THAT(test_vector, ElementsAreArray(expected)); + test_vector.push_back(4); + EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); +} + +TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) { + const int a[] = { 1, 2, 3 }; + const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) }; + vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); + const vector<Matcher<int> > expected( + kMatchers, kMatchers + GMOCK_ARRAY_SIZE_(kMatchers)); + EXPECT_THAT(test_vector, ElementsAreArray(expected)); + test_vector.push_back(4); + EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); +} + +TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) { + const int a[] = { 1, 2, 3 }; + const vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); + const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a)); + EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end())); + // Pointers are iterators, too. + EXPECT_THAT(test_vector, ElementsAreArray(a, a + GMOCK_ARRAY_SIZE_(a))); + // The empty range of NULL pointers should also be okay. + int* const null_int = NULL; + EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int))); + EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int)); +} + // Since ElementsAre() and ElementsAreArray() share much of the // implementation, we only do a sanity test for native arrays here. TEST(ElementsAreArrayTest, WorksWithNativeArray) { @@ -587,6 +666,22 @@ TEST(ElementsAreArrayTest, WorksWithNativeArray) { EXPECT_THAT(a, Not(ElementsAreArray(b, 1))); } +TEST(ElementsAreArrayTest, SourceLifeSpan) { + const int a[] = { 1, 2, 3 }; + vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a)); + vector<int> expect(a, a + GMOCK_ARRAY_SIZE_(a)); + ElementsAreArrayMatcher<int> matcher_maker = + ElementsAreArray(expect.begin(), expect.end()); + EXPECT_THAT(test_vector, matcher_maker); + // Changing in place the values that initialized matcher_maker should not + // affect matcher_maker anymore. It should have made its own copy of them. + typedef vector<int>::iterator Iter; + for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; } + EXPECT_THAT(test_vector, matcher_maker); + test_vector.push_back(3); + EXPECT_THAT(test_vector, Not(matcher_maker)); +} + // Tests for the MATCHER*() macro family. // Tests that a simple MATCHER() definition works. @@ -1017,7 +1112,7 @@ TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { TEST(ContainsTest, ExplainsMatchResultCorrectly) { const int a[2] = { 1, 2 }; - Matcher<const int(&)[2]> m = Contains(2); + Matcher<const int (&)[2]> m = Contains(2); EXPECT_EQ("whose element #1 matches", Explain(m, a)); m = Contains(3); diff --git a/test/gmock-internal-utils_test.cc b/test/gmock-internal-utils_test.cc index d53282eb..3c78f647 100644 --- a/test/gmock-internal-utils_test.cc +++ b/test/gmock-internal-utils_test.cc @@ -343,13 +343,7 @@ TEST(ExpectTest, FailsNonfatallyOnFalse) { class LogIsVisibleTest : public ::testing::Test { protected: virtual void SetUp() { - // The code needs to work when both ::string and ::std::string are - // defined and the flag is implemented as a - // testing::internal::String. In this case, without the call to - // c_str(), the compiler will complain that it cannot figure out - // whether the String flag should be converted to a ::string or an - // ::std::string before being assigned to original_verbose_. - original_verbose_ = GMOCK_FLAG(verbose).c_str(); + original_verbose_ = GMOCK_FLAG(verbose); } virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } @@ -415,7 +409,7 @@ TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) { TEST(LogTest, NoSkippingStackFrameInOptMode) { CaptureStdout(); Log(kWarning, "Test log.\n", 100); - const String log = GetCapturedStdout(); + const string log = GetCapturedStdout(); # if defined(NDEBUG) && GTEST_GOOGLE3_MODE_ @@ -502,7 +496,7 @@ TEST(TypeTraitsTest, remove_reference) { // Verifies that Log() behaves correctly for the given verbosity level // and log severity. -String GrabOutput(void(*logger)(), const char* verbosity) { +std::string GrabOutput(void(*logger)(), const char* verbosity) { const string saved_flag = GMOCK_FLAG(verbose); GMOCK_FLAG(verbose) = verbosity; CaptureStdout(); @@ -525,7 +519,7 @@ void ExpectCallLogger() { // Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info". TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) { - EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity), + EXPECT_THAT(std::string(GrabOutput(ExpectCallLogger, kInfoVerbosity)), HasSubstr("EXPECT_CALL(mock, TestMethod())")); } @@ -548,7 +542,7 @@ void OnCallLogger() { // Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info". TEST(OnCallTest, LogsWhenVerbosityIsInfo) { - EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity), + EXPECT_THAT(std::string(GrabOutput(OnCallLogger, kInfoVerbosity)), HasSubstr("ON_CALL(mock, TestMethod())")); } @@ -571,7 +565,7 @@ void OnCallAnyArgumentLogger() { // Verifies that ON_CALL prints provided _ argument. TEST(OnCallTest, LogsAnythingArgument) { - EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity), + EXPECT_THAT(std::string(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity)), HasSubstr("ON_CALL(mock, TestMethodArg(_)")); } diff --git a/test/gmock-matchers_test.cc b/test/gmock-matchers_test.cc index 6e5d5c31..de3f8d73 100644 --- a/test/gmock-matchers_test.cc +++ b/test/gmock-matchers_test.cc @@ -131,7 +131,6 @@ using testing::internal::IsReadableTypeName; using testing::internal::JoinAsTuple; using testing::internal::RE; using testing::internal::StreamMatchResultListener; -using testing::internal::String; using testing::internal::StringMatchResultListener; using testing::internal::Strings; using testing::internal::linked_ptr; diff --git a/test/gmock-nice-strict_test.cc b/test/gmock-nice-strict_test.cc index e3344180..315a8221 100644 --- a/test/gmock-nice-strict_test.cc +++ b/test/gmock-nice-strict_test.cc @@ -141,12 +141,12 @@ TEST(NiceMockTest, InfoForUninterestingCall) { GMOCK_FLAG(verbose) = "info"; CaptureStdout(); nice_foo.DoThis(); - EXPECT_THAT(GetCapturedStdout(), + EXPECT_THAT(std::string(GetCapturedStdout()), HasSubstr("Uninteresting mock function call")); CaptureStdout(); nice_foo.DoThat(true); - EXPECT_THAT(GetCapturedStdout(), + EXPECT_THAT(std::string(GetCapturedStdout()), HasSubstr("Uninteresting mock function call")); GMOCK_FLAG(verbose) = saved_flag; } diff --git a/test/gmock-spec-builders_test.cc b/test/gmock-spec-builders_test.cc index 9177b322..0e2e0090 100644 --- a/test/gmock-spec-builders_test.cc +++ b/test/gmock-spec-builders_test.cc @@ -94,7 +94,6 @@ using testing::internal::FormatFileLocation; using testing::internal::kErrorVerbosity; using testing::internal::kInfoVerbosity; using testing::internal::kWarningVerbosity; -using testing::internal::String; using testing::internal::linked_ptr; using testing::internal::string; @@ -632,7 +631,7 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) { b.DoB(1); b.DoB(2); } - const String output = GetCapturedStdout(); + const std::string output = GetCapturedStdout(); EXPECT_PRED_FORMAT2( IsSubstring, "Too many actions specified in EXPECT_CALL(b, DoB())...\n" @@ -674,7 +673,7 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { CaptureStdout(); b.DoB(); - const String output = GetCapturedStdout(); + const std::string output = GetCapturedStdout(); EXPECT_PRED_FORMAT2( IsSubstring, "Too few actions specified in EXPECT_CALL(b, DoB())...\n" @@ -869,13 +868,13 @@ TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) { // expectation has no action clause at all. EXPECT_EQ(1, b.DoB()); EXPECT_EQ(2, b.DoB()); - const String output1 = GetCapturedStdout(); + const std::string output1 = GetCapturedStdout(); EXPECT_STREQ("", output1.c_str()); CaptureStdout(); EXPECT_EQ(0, b.DoB()); EXPECT_EQ(0, b.DoB()); - const String output2 = GetCapturedStdout(); + const std::string output2 = GetCapturedStdout(); EXPECT_THAT(output2.c_str(), HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n" "Called 3 times, but only 2 WillOnce()s are specified" @@ -895,7 +894,7 @@ TEST(FunctionMockerTest, ReportsExpectCallLocationForExhausedActions) { CaptureStdout(); EXPECT_EQ(0, b.DoB()); - const String output = GetCapturedStdout(); + const std::string output = GetCapturedStdout(); // The warning message should contain the call location. EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output); } @@ -1873,14 +1872,8 @@ class MockC { class VerboseFlagPreservingFixture : public testing::Test { protected: - // The code needs to work when both ::string and ::std::string are defined - // and the flag is implemented as a testing::internal::String. In this - // case, without the call to c_str(), the compiler will complain that it - // cannot figure out what overload of string constructor to use. - // TODO(vladl@google.com): Use internal::string instead of String for - // string flags in Google Test. VerboseFlagPreservingFixture() - : saved_verbose_flag_(GMOCK_FLAG(verbose).c_str()) {} + : saved_verbose_flag_(GMOCK_FLAG(verbose)) {} ~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; } @@ -1898,7 +1891,7 @@ TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { MockC c; CaptureStdout(); c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); - const String output = GetCapturedStdout(); + const std::string output = GetCapturedStdout(); EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output); EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output); @@ -1915,7 +1908,7 @@ TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { // stack trace. CaptureStdout(); c.NonVoidMethod(); - const String output2 = GetCapturedStdout(); + const std::string output2 = GetCapturedStdout(); EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2); # endif // NDEBUG @@ -1928,7 +1921,7 @@ TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) { MockB b; CaptureStdout(); b.DoB(); - const String output1 = GetCapturedStdout(); + const std::string output1 = GetCapturedStdout(); EXPECT_PRED_FORMAT2( IsSubstring, "Uninteresting mock function call - returning default value.\n" @@ -1940,7 +1933,7 @@ TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) { MockC c; CaptureStdout(); c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); - const String output2 = GetCapturedStdout(); + const std::string output2 = GetCapturedStdout(); EXPECT_THAT(output2.c_str(), ContainsRegex( "Uninteresting mock function call - returning directly\\.\n" @@ -1958,7 +1951,7 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture { // should_print is true, the output should match the given regex and // contain the given function name in the stack trace. When it's // false, the output should be empty.) - void VerifyOutput(const String& output, bool should_print, + void VerifyOutput(const std::string& output, bool should_print, const string& expected_substring, const string& function_name) { if (should_print) { |