// Copyright 2007, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // Author: wan@google.com (Zhanyong Wan) // Google Mock - a framework for writing C++ mock classes. // // This file tests the built-in actions. #include "gmock/gmock-actions.h" #include #include #include #include #include "gmock/gmock.h" #include "gmock/internal/gmock-port.h" #include "gtest/gtest.h" #include "gtest/gtest-spi.h" namespace { // This list should be kept sorted. using testing::Action; using testing::ActionInterface; using testing::Assign; using testing::ByMove; using testing::ByRef; using testing::DefaultValue; using testing::DoDefault; using testing::IgnoreResult; using testing::Invoke; using testing::InvokeWithoutArgs; using testing::MakePolymorphicAction; using testing::Ne; using testing::PolymorphicAction; using testing::Return; using testing::ReturnNull; using testing::ReturnRef; using testing::ReturnRefOfCopy; using testing::SetArgPointee; using testing::SetArgumentPointee; using testing::_; using testing::get; using testing::internal::BuiltInDefaultValue; using testing::internal::Int64; using testing::internal::UInt64; using testing::make_tuple; using testing::tuple; using testing::tuple_element; #if !GTEST_OS_WINDOWS_MOBILE using testing::SetErrnoAndReturn; #endif #if GTEST_HAS_PROTOBUF_ using testing::internal::TestMessage; #endif // GTEST_HAS_PROTOBUF_ // Tests that BuiltInDefaultValue::Get() returns NULL. TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) { EXPECT_TRUE(BuiltInDefaultValue::Get() == NULL); EXPECT_TRUE(BuiltInDefaultValue::Get() == NULL); EXPECT_TRUE(BuiltInDefaultValue::Get() == NULL); } // Tests that BuiltInDefaultValue::Exists() return true. TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) { EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); } // Tests that BuiltInDefaultValue::Get() returns 0 when T is a // built-in numeric type. TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { EXPECT_EQ(0U, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); #if GMOCK_HAS_SIGNED_WCHAR_T_ EXPECT_EQ(0U, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); #endif #if GMOCK_WCHAR_T_IS_NATIVE_ EXPECT_EQ(0, BuiltInDefaultValue::Get()); #endif EXPECT_EQ(0U, BuiltInDefaultValue::Get()); // NOLINT EXPECT_EQ(0, BuiltInDefaultValue::Get()); // NOLINT EXPECT_EQ(0, BuiltInDefaultValue::Get()); // NOLINT EXPECT_EQ(0U, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); EXPECT_EQ(0U, BuiltInDefaultValue::Get()); // NOLINT EXPECT_EQ(0, BuiltInDefaultValue::Get()); // NOLINT EXPECT_EQ(0, BuiltInDefaultValue::Get()); // NOLINT EXPECT_EQ(0U, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); } // Tests that BuiltInDefaultValue::Exists() returns true when T is a // built-in numeric type. TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); #if GMOCK_HAS_SIGNED_WCHAR_T_ EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); #endif #if GMOCK_WCHAR_T_IS_NATIVE_ EXPECT_TRUE(BuiltInDefaultValue::Exists()); #endif EXPECT_TRUE(BuiltInDefaultValue::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue::Exists()); // NOLINT EXPECT_TRUE(BuiltInDef
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "ch.h"
#include "hal.h"
#include "ch_test.h"

/*
 * This is a periodic thread that does absolutely nothing except flashing LEDs.
 */
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {

  (void)arg;

  chRegSetThreadName("blinker");
  while (true) {
    palClearPad(GPIOC, GPIOC_LED1);
    chThdSleepMilliseconds(250);
    palSetPad(GPIOC, GPIOC_LED1);
    palClearPad(GPIOC, GPIOC_LED2);
    chThdSleepMilliseconds(250);
    palSetPad(GPIOC, GPIOC_LED2);
    palClearPad(GPIOC, GPIOC_LED3);
    chThdSleepMilliseconds(250);
    palSetPad(GPIOC, GPIOC_LED3);
    palClearPad(GPIOC, GPIOC_LED4);
    chThdSleepMilliseconds(250);
    palSetPad(GPIOC, GPIOC_LED4);
  }
}

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Activates the serial driver 2 using the default configuration, pins
   * are pre-configured in board.h.
   */
  sdStart(&SD2, NULL);

  /*
   * Creates the example thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

  /*
   * Normal main() thread activity, in this demo it does nothing except
   * sleeping in a loop and check the button state, when the button is
   * pressed the test procedure is launched.
   */
  while (true) {
    if (palReadPad(GPIOA, GPIOA_WKUP_BUTTON))
      test_execute((BaseSequentialStream *)&SD2);
    chThdSleepMilliseconds(500);
  }
}
ullaryFunction(); EXPECT_EQ(0, a1.Perform(make_tuple())); Action a2 = ReturnZeroFromNullaryFunction(); EXPECT_TRUE(a2.Perform(make_tuple()) == NULL); } // Tests that Return() works as an action for void-returning // functions. TEST(ReturnTest, WorksForVoid) { const Action ret = Return(); // NOLINT return ret.Perform(make_tuple(1)); } // Tests that Return(v) returns v. TEST(ReturnTest, ReturnsGivenValue) { Action ret = Return(1); // NOLINT EXPECT_EQ(1, ret.Perform(make_tuple())); ret = Return(-5); EXPECT_EQ(-5, ret.Perform(make_tuple())); } // Tests that Return("string literal") works. TEST(ReturnTest, AcceptsStringLiteral) { Action a1 = Return("Hello"); EXPECT_STREQ("Hello", a1.Perform(make_tuple())); Action a2 = Return("world"); EXPECT_EQ("world", a2.Perform(make_tuple())); } // Test struct which wraps a vector of integers. Used in // 'SupportsWrapperReturnType' test. struct IntegerVectorWrapper { std::vector * v; IntegerVectorWrapper(std::vector& _v) : v(&_v) {} // NOLINT }; // Tests that Return() works when return type is a wrapper type. TEST(ReturnTest, SupportsWrapperReturnType) { // Initialize vector of integers. std::vector v; for (int i = 0; i < 5; ++i) v.push_back(i); // Return() called with 'v' as argument. The Action will return the same data // as 'v' (copy) but it will be wrapped in an IntegerVectorWrapper. Action a = Return(v); const std::vector& result = *(a.Perform(make_tuple()).v); EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4)); } // Tests that Return(v) is covaraint. struct Base { bool operator==(const Base&) { return true; } }; struct Derived : public Base { bool operator==(const Derived&) { return true; } }; TEST(ReturnTest, IsCovariant) { Base base; Derived derived; Action ret = Return(&base); EXPECT_EQ(&base, ret.Perform(make_tuple())); ret = Return(&derived); EXPECT_EQ(&derived, ret.Perform(make_tuple())); } // Tests that the type of the value passed into Return is converted into T // when the action is cast to Action rather than when the action is // performed. See comments on testing::internal::ReturnAction in // gmock-actions.h for more information. class FromType { public: explicit FromType(bool* is_converted) : converted_(is_converted) {} bool* converted() const { return converted_; } private: bool* const converted_; GTEST_DISALLOW_ASSIGN_(FromType); }; class ToType { public: // Must allow implicit conversion due to use in ImplicitCast_. ToType(const FromType& x) { *x.converted() = true; } // NOLINT }; TEST(ReturnTest, ConvertsArgumentWhenConverted) { bool converted = false; FromType x(&converted); Action action(Return(x)); EXPECT_TRUE(converted) << "Return must convert its argument in its own " << "conversion operator."; converted = false; action.Perform(tuple<>()); EXPECT_FALSE(converted) << "Action must NOT convert its argument " << "when performed."; } class DestinationType {}; class SourceType { public: // Note: a non-const typecast operator. operator DestinationType() { return DestinationType(); } }; TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) { SourceType s; Action action(Return(s)); } // Tests that ReturnNull() returns NULL in a pointer-returning function. TEST(ReturnNullTest, WorksInPointerReturningFunction) { const Action a1 = ReturnNull(); EXPECT_TRUE(a1.Perform(make_tuple()) == NULL); const Action a2 = ReturnNull(); // NOLINT EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL); } #if GTEST_HAS_STD_UNIQUE_PTR_ // Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning // functions. TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) { const Action()> a1 = ReturnNull(); EXPECT_TRUE(a1.Perform(make_tuple()) == nullptr); const Action(std::string)> a2 = ReturnNull(); EXPECT_TRUE(a2.Perform(make_tuple("foo")) == nullptr); } #endif // GTEST_HAS_STD_UNIQUE_PTR_ // Tests that ReturnRef(v) works for reference types. TEST(ReturnRefTest, WorksForReference) { const int n = 0; const Action ret = ReturnRef(n); // NOLINT EXPECT_EQ(&n, &ret.Perform(make_tuple(true))); } // Tests that ReturnRef(v) is covariant. TEST(ReturnRefTest, IsCovariant) { Base base; Derived derived; Action a = ReturnRef(base); EXPECT_EQ(&base, &a.Perform(make_tuple())); a = ReturnRef(derived); EXPECT_EQ(&derived, &a.Perform(make_tuple())); } // Tests that ReturnRefOfCopy(v) works for reference types. TEST(ReturnRefOfCopyTest, WorksForReference) { int n = 42; const Action ret = ReturnRefOfCopy(n); EXPECT_NE(&n, &ret.Perform(make_tuple())); EXPECT_EQ(42, ret.Perform(make_tuple())); n = 43; EXPECT_NE(&n, &ret.Perform(make_tuple())); EXPECT_EQ(42, ret.Perform(make_tuple())); } // Tests that ReturnRefOfCopy(v) is covariant. TEST(ReturnRefOfCopyTest, IsCovariant) { Base base; Derived derived; Action a = ReturnRefOfCopy(base); EXPECT_NE(&base, &a.Perform(make_tuple())); a = ReturnRefOfCopy(derived); EXPECT_NE(&derived, &a.Perform(make_tuple())); } // Tests that DoDefault() does the default action for the mock method. class MockClass { public: MockClass() {} MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT MOCK_METHOD0(Foo, MyNonDefaultConstructible()); #if GTEST_HAS_STD_UNIQUE_PTR_ MOCK_METHOD0(MakeUnique, std::unique_ptr()); MOCK_METHOD0(MakeUniqueBase, std::unique_ptr()); MOCK_METHOD0(MakeVectorUnique, std::vector>()); #endif private: GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass); }; // Tests that DoDefault() returns the built-in default value for the // return type by default. TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) { MockClass mock; EXPECT_CALL(mock, IntFunc(_)) .WillOnce(DoDefault()); EXPECT_EQ(0, mock.IntFunc(true)); } // Tests that DoDefault() throws (when exceptions are enabled) or aborts // the process when there is no built-in default value for the return type. TEST(DoDefaultDeathTest, DiesForUnknowType) { MockClass mock; EXPECT_CALL(mock, Foo()) .WillRepeatedly(DoDefault()); #if GTEST_HAS_EXCEPTIONS EXPECT_ANY_THROW(mock.Foo()); #else EXPECT_DEATH_IF_SUPPORTED({ mock.Foo(); }, ""); #endif } // Tests that using DoDefault() inside a composite action leads to a // run-time error. void VoidFunc(bool /* flag */) {} TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { MockClass mock; EXPECT_CALL(mock, IntFunc(_)) .WillRepeatedly(DoAll(Invoke(VoidFunc), DoDefault())); // Ideally we should verify the error message as well. Sadly, // EXPECT_DEATH() can only capture stderr, while Google Mock's // errors are printed on stdout. Therefore we have to settle for // not verifying the message. EXPECT_DEATH_IF_SUPPORTED({ mock.IntFunc(true); }, ""); } // Tests that DoDefault() returns the default value set by // DefaultValue::Set() when it's not overriden by an ON_CALL(). TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { DefaultValue::Set(1); MockClass mock; EXPECT_CALL(mock, IntFunc(_)) .WillOnce(DoDefault()); EXPECT_EQ(1, mock.IntFunc(false)); DefaultValue::Clear(); } // Tests that DoDefault() does the action specified by ON_CALL(). TEST(DoDefaultTest, DoesWhatOnCallSpecifies) { MockClass mock; ON_CALL(mock, IntFunc(_)) .WillByDefault(Return(2)); EXPECT_CALL(mock, IntFunc(_)) .WillOnce(DoDefault()); EXPECT_EQ(2, mock.IntFunc(false)); } // Tests that using DoDefault() in ON_CALL() leads to a run-time failure. TEST(DoDefaultTest, CannotBeUsedInOnCall) { MockClass mock; EXPECT_NONFATAL_FAILURE({ // NOLINT ON_CALL(mock, IntFunc(_)) .WillByDefault(DoDefault()); }, "DoDefault() cannot be used in ON_CALL()"); } // Tests that SetArgPointee(v) sets the variable pointed to by // the N-th (0-based) argument to v. TEST(SetArgPointeeTest, SetsTheNthPointee) { typedef void MyFunction(bool, int*, char*); Action a = SetArgPointee<1>(2); int n = 0; char ch = '\0'; a.Perform(make_tuple(true, &n, &ch)); EXPECT_EQ(2, n); EXPECT_EQ('\0', ch); a = SetArgPointee<2>('a'); n = 0; ch = '\0'; a.Perform(make_tuple(true, &n, &ch)); EXPECT_EQ(0, n); EXPECT_EQ('a', ch); } #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN) // Tests that SetArgPointee() accepts a string literal. // GCC prior to v4.0 and the Symbian compiler do not support this. TEST(SetArgPointeeTest, AcceptsStringLiteral) { typedef void MyFunction(std::string*, const char**); Action a = SetArgPointee<0>("hi"); std::string str; const char* ptr = NULL; a.Perform(make_tuple(&str, &ptr)); EXPECT_EQ("hi", str); EXPECT_TRUE(ptr == NULL); a = SetArgPointee<1>("world"); str = ""; a.Perform(make_tuple(&str, &ptr)); EXPECT_EQ("", str); EXPECT_STREQ("world", ptr); } TEST(SetArgPointeeTest, AcceptsWideStringLiteral) { typedef void MyFunction(const wchar_t**); Action a = SetArgPointee<0>(L"world"); const wchar_t* ptr = NULL; a.Perform(make_tuple(&ptr)); EXPECT_STREQ(L"world", ptr); # if GTEST_HAS_STD_WSTRING typedef void MyStringFunction(std::wstring*); Action a2 = SetArgPointee<0>(L"world"); std::wstring str = L""; a2.Perform(make_tuple(&str)); EXPECT_EQ(L"world", str); # endif } #endif // Tests that SetArgPointee() accepts a char pointer. TEST(SetArgPointeeTest, AcceptsCharPointer) { typedef void MyFunction(bool, std::string*, const char**); const char* const hi = "hi"; Action a = SetArgPointee<1>(hi); std::string str; const char* ptr = NULL; a.Perform(make_tuple(true, &str, &ptr)); EXPECT_EQ("hi", str); EXPECT_TRUE(ptr == NULL); char world_array[] = "world"; char* const world = world_array; a = SetArgPointee<2>(world); str = ""; a.Perform(make_tuple(true, &str, &ptr)); EXPECT_EQ("", str); EXPECT_EQ(world, ptr); } TEST(SetArgPointeeTest, AcceptsWideCharPointer) { typedef void MyFunction(bool, const wchar_t**); const wchar_t* const hi = L"hi"; Action a = SetArgPointee<1>(hi); const wchar_t* ptr = NULL; a.Perform(make_tuple(true, &ptr)); EXPECT_EQ(hi, ptr); # if GTEST_HAS_STD_WSTRING typedef void MyStringFunction(bool, std::wstring*); wchar_t world_array[] = L"world"; wchar_t* const world = world_array; Action a2 = SetArgPointee<1>(world); std::wstring str; a2.Perform(make_tuple(true, &str)); EXPECT_EQ(world_array, str); # endif } #if GTEST_HAS_PROTOBUF_ // Tests that SetArgPointee(proto_buffer) sets the v1 protobuf // variable pointed to by the N-th (0-based) argument to proto_buffer. TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) { TestMessage* const msg = new TestMessage; msg->set_member("yes"); TestMessage orig_msg; orig_msg.CopyFrom(*msg); Action a = SetArgPointee<1>(*msg); // SetArgPointee(proto_buffer) makes a copy of proto_buffer // s.t. the action works even when the original proto_buffer has // died. We ensure this behavior by deleting msg before using the // action. delete msg; TestMessage dest; EXPECT_FALSE(orig_msg.Equals(dest)); a.Perform(make_tuple(true, &dest)); EXPECT_TRUE(orig_msg.Equals(dest)); } // Tests that SetArgPointee(proto_buffer) sets the // ::ProtocolMessage variable pointed to by the N-th (0-based) // argument to proto_buffer. TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) { TestMessage* const msg = new TestMessage; msg->set_member("yes"); TestMessage orig_msg; orig_msg.CopyFrom(*msg); Action a = SetArgPointee<1>(*msg); // SetArgPointee(proto_buffer) makes a copy of proto_buffer // s.t. the action works even when the original proto_buffer has // died. We ensure this behavior by deleting msg before using the // action. delete msg; TestMessage dest; ::ProtocolMessage* const dest_base = &dest; EXPECT_FALSE(orig_msg.Equals(dest)); a.Perform(make_tuple(true, dest_base)); EXPECT_TRUE(orig_msg.Equals(dest)); } // Tests that SetArgPointee(proto2_buffer) sets the v2 // protobuf variable pointed to by the N-th (0-based) argument to // proto2_buffer. TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) { using testing::internal::FooMessage; FooMessage* const msg = new FooMessage; msg->set_int_field(2); msg->set_string_field("hi"); FooMessage orig_msg; orig_msg.CopyFrom(*msg); Action a = SetArgPointee<1>(*msg); // SetArgPointee(proto2_buffer) makes a copy of // proto2_buffer s.t. the action works even when the original // proto2_buffer has died. We ensure this behavior by deleting msg // before using the action. delete msg; FooMessage dest; dest.set_int_field(0); a.Perform(make_tuple(true, &dest)); EXPECT_EQ(2, dest.int_field()); EXPECT_EQ("hi", dest.string_field()); } // Tests that SetArgPointee(proto2_buffer) sets the // proto2::Message variable pointed to by the N-th (0-based) argument // to proto2_buffer. TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { using testing::internal::FooMessage; FooMessage* const msg = new FooMessage; msg->set_int_field(2); msg->set_string_field("hi"); FooMessage orig_msg; orig_msg.CopyFrom(*msg); Action a = SetArgPointee<1>(*msg); // SetArgPointee(proto2_buffer) makes a copy of // proto2_buffer s.t. the action works even when the original // proto2_buffer has died. We ensure this behavior by deleting msg // before using the action. delete msg; FooMessage dest; dest.set_int_field(0); ::proto2::Message* const dest_base = &dest; a.Perform(make_tuple(true, dest_base)); EXPECT_EQ(2, dest.int_field()); EXPECT_EQ("hi", dest.string_field()); } #endif // GTEST_HAS_PROTOBUF_ // Tests that SetArgumentPointee(v) sets the variable pointed to by // the N-th (0-based) argument to v. TEST(SetArgumentPointeeTest, SetsTheNthPointee) { typedef void MyFunction(bool, int*, char*); Action a = SetArgumentPointee<1>(2); int n = 0; char ch = '\0'; a.Perform(make_tuple(true, &n, &ch)); EXPECT_EQ(2, n); EXPECT_EQ('\0', ch); a = SetArgumentPointee<2>('a'); n = 0; ch = '\0'; a.Perform(make_tuple(true, &n, &ch)); EXPECT_EQ(0, n); EXPECT_EQ('a', ch); } #if GTEST_HAS_PROTOBUF_ // Tests that SetArgumentPointee(proto_buffer) sets the v1 protobuf // variable pointed to by the N-th (0-based) argument to proto_buffer. TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) { TestMessage* const msg = new TestMessage; msg->set_member("yes"); TestMessage orig_msg; orig_msg.CopyFrom(*msg); Action a = SetArgumentPointee<1>(*msg); // SetArgumentPointee(proto_buffer) makes a copy of proto_buffer // s.t. the action works even when the original proto_buffer has // died. We ensure this behavior by deleting msg before using the // action. delete msg; TestMessage dest; EXPECT_FALSE(orig_msg.Equals(dest)); a.Perform(make_tuple(true, &dest)); EXPECT_TRUE(orig_msg.Equals(dest)); } // Tests that SetArgumentPointee(proto_buffer) sets the // ::ProtocolMessage variable pointed to by the N-th (0-based) // argument to proto_buffer. TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) { TestMessage* const msg = new TestMessage; msg->set_member("yes"); TestMessage orig_msg; orig_msg.CopyFrom(*msg); Action a = SetArgumentPointee<1>(*msg); // SetArgumentPointee(proto_buffer) makes a copy of proto_buffer // s.t. the action works even when the original proto_buffer has // died. We ensure this behavior by deleting msg before using the // action. delete msg; TestMessage dest; ::ProtocolMessage* const dest_base = &dest; EXPECT_FALSE(orig_msg.Equals(dest)); a.Perform(make_tuple(true, dest_base)); EXPECT_TRUE(orig_msg.Equals(dest)); } // Tests that SetArgumentPointee(proto2_buffer) sets the v2 // protobuf variable pointed to by the N-th (0-based) argument to // proto2_buffer. TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) { using testing::internal::FooMessage; FooMessage* const msg = new FooMessage; msg->set_int_field(2); msg->set_string_field("hi"); FooMessage orig_msg; orig_msg.CopyFrom(*msg); Action a = SetArgumentPointee<1>(*msg); // SetArgumentPointee(proto2_buffer) makes a copy of // proto2_buffer s.t. the action works even when the original // proto2_buffer has died. We ensure this behavior by deleting msg // before using the action. delete msg; FooMessage dest; dest.set_int_field(0); a.Perform(make_tuple(true, &dest)); EXPECT_EQ(2, dest.int_field()); EXPECT_EQ("hi", dest.string_field()); } // Tests that SetArgumentPointee(proto2_buffer) sets the // proto2::Message variable pointed to by the N-th (0-based) argument // to proto2_buffer. TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { using testing::internal::FooMessage; FooMessage* const msg = new FooMessage; msg->set_int_field(2); msg->set_string_field("hi"); FooMessage orig_msg; orig_msg.CopyFrom(*msg); Action a = SetArgumentPointee<1>(*msg); // SetArgumentPointee(proto2_buffer) makes a copy of // proto2_buffer s.t. the action works even when the original // proto2_buffer has died. We ensure this behavior by deleting msg // before using the action. delete msg; FooMessage dest; dest.set_int_field(0); ::proto2::Message* const dest_base = &dest; a.Perform(make_tuple(true, dest_base)); EXPECT_EQ(2, dest.int_field()); EXPECT_EQ("hi", dest.string_field()); } #endif // GTEST_HAS_PROTOBUF_ // Sample functions and functors for testing Invoke() and etc. int Nullary() { return 1; } class NullaryFunctor { public: int operator()() { return 2; } }; bool g_done = false; void VoidNullary() { g_done = true; } class VoidNullaryFunctor { public: void operator()() { g_done = true; } }; class Foo { public: Foo() : value_(123) {} int Nullary() const { return value_; } private: int value_; }; // Tests InvokeWithoutArgs(function). TEST(InvokeWithoutArgsTest, Function) { // As an action that takes one argument. Action a = InvokeWithoutArgs(Nullary); // NOLINT EXPECT_EQ(1, a.Perform(make_tuple(2))); // As an action that takes two arguments. Action a2 = InvokeWithoutArgs(Nullary); // NOLINT EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5))); // As an action that returns void. Action a3 = InvokeWithoutArgs(VoidNullary); // NOLINT g_done = false; a3.Perform(make_tuple(1)); EXPECT_TRUE(g_done); } // Tests InvokeWithoutArgs(functor). TEST(InvokeWithoutArgsTest, Functor) { // As an action that takes no argument. Action a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT EXPECT_EQ(2, a.Perform(make_tuple())); // As an action that takes three arguments. Action a2 = // NOLINT InvokeWithoutArgs(NullaryFunctor()); EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a'))); // As an action that returns void. Action a3 = InvokeWithoutArgs(VoidNullaryFunctor()); g_done = false; a3.Perform(make_tuple()); EXPECT_TRUE(g_done); } // Tests InvokeWithoutArgs(obj_ptr, method). TEST(InvokeWithoutArgsTest, Method) { Foo foo; Action a = // NOLINT InvokeWithoutArgs(&foo, &Foo::Nullary); EXPECT_EQ(123, a.Perform(make_tuple(true, 'a'))); } // Tests using IgnoreResult() on a polymorphic action. TEST(IgnoreResultTest, PolymorphicAction) { Action a = IgnoreResult(Return(5)); // NOLINT a.Perform(make_tuple(1)); } // Tests using IgnoreResult() on a monomorphic action. int ReturnOne() { g_done = true; return 1; } TEST(IgnoreResultTest, MonomorphicAction) { g_done = false; Action a = IgnoreResult(Invoke(ReturnOne)); a.Perform(make_tuple()); EXPECT_TRUE(g_done); } // Tests using IgnoreResult() on an action that returns a class type. MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) { g_done = true; return MyNonDefaultConstructible(42); } TEST(IgnoreResultTest, ActionReturningClass) { g_done = false; Action a = IgnoreResult(Invoke(ReturnMyNonDefaultConstructible)); // NOLINT a.Perform(make_tuple(2)); EXPECT_TRUE(g_done); } TEST(AssignTest, Int) { int x = 0; Action a = Assign(&x, 5); a.Perform(make_tuple(0)); EXPECT_EQ(5, x); } TEST(AssignTest, String) { ::std::string x; Action a = Assign(&x, "Hello, world"); a.Perform(make_tuple()); EXPECT_EQ("Hello, world", x); } TEST(AssignTest, CompatibleTypes) { double x = 0; Action a = Assign(&x, 5); a.Perform(make_tuple(0)); EXPECT_DOUBLE_EQ(5, x); } #if !GTEST_OS_WINDOWS_MOBILE class SetErrnoAndReturnTest : public testing::Test { protected: virtual void SetUp() { errno = 0; } virtual void TearDown() { errno = 0; } }; TEST_F(SetErrnoAndReturnTest, Int) { Action a = SetErrnoAndReturn(ENOTTY, -5); EXPECT_EQ(-5, a.Perform(make_tuple())); EXPECT_EQ(ENOTTY, errno); } TEST_F(SetErrnoAndReturnTest, Ptr) { int x; Action a = SetErrnoAndReturn(ENOTTY, &x); EXPECT_EQ(&x, a.Perform(make_tuple())); EXPECT_EQ(ENOTTY, errno); } TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { Action a = SetErrnoAndReturn(EINVAL, 5); EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple())); EXPECT_EQ(EINVAL, errno); } #endif // !GTEST_OS_WINDOWS_MOBILE // Tests ByRef(). // Tests that ReferenceWrapper is copyable. TEST(ByRefTest, IsCopyable) { const std::string s1 = "Hi"; const std::string s2 = "Hello"; ::testing::internal::ReferenceWrapper ref_wrapper = ByRef(s1); const std::string& r1 = ref_wrapper; EXPECT_EQ(&s1, &r1); // Assigns a new value to ref_wrapper. ref_wrapper = ByRef(s2); const std::string& r2 = ref_wrapper; EXPECT_EQ(&s2, &r2); ::testing::internal::ReferenceWrapper ref_wrapper1 = ByRef(s1); // Copies ref_wrapper1 to ref_wrapper. ref_wrapper = ref_wrapper1; const std::string& r3 = ref_wrapper; EXPECT_EQ(&s1, &r3); } // Tests using ByRef() on a const value. TEST(ByRefTest, ConstValue) { const int n = 0; // int& ref = ByRef(n); // This shouldn't compile - we have a // negative compilation test to catch it. const int& const_ref = ByRef(n); EXPECT_EQ(&n, &const_ref); } // Tests using ByRef() on a non-const value. TEST(ByRefTest, NonConstValue) { int n = 0; // ByRef(n) can be used as either an int&, int& ref = ByRef(n); EXPECT_EQ(&n, &ref); // or a const int&. const int& const_ref = ByRef(n); EXPECT_EQ(&n, &const_ref); } // Tests explicitly specifying the type when using ByRef(). TEST(ByRefTest, ExplicitType) { int n = 0; const int& r1 = ByRef(n); EXPECT_EQ(&n, &r1); // ByRef(n); // This shouldn't compile - we have a negative // compilation test to catch it. Derived d; Derived& r2 = ByRef(d); EXPECT_EQ(&d, &r2); const Derived& r3 = ByRef(d); EXPECT_EQ(&d, &r3); Base& r4 = ByRef(d); EXPECT_EQ(&d, &r4); const Base& r5 = ByRef(d); EXPECT_EQ(&d, &r5); // The following shouldn't compile - we have a negative compilation // test for it. // // Base b; // ByRef(b); } // Tests that Google Mock prints expression ByRef(x) as a reference to x. TEST(ByRefTest, PrintsCorrectly) { int n = 42; ::std::stringstream expected, actual; testing::internal::UniversalPrinter::Print(n, &expected); testing::internal::UniversalPrint(ByRef(n), &actual); EXPECT_EQ(expected.str(), actual.str()); } #if GTEST_HAS_STD_UNIQUE_PTR_ std::unique_ptr UniquePtrSource() { return std::unique_ptr(new int(19)); } std::vector> VectorUniquePtrSource() { std::vector> out; out.emplace_back(new int(7)); return out; } TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) { MockClass mock; std::unique_ptr i(new int(19)); EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i)))); EXPECT_CALL(mock, MakeVectorUnique()) .WillOnce(Return(ByMove(VectorUniquePtrSource()))); Derived* d = new Derived; EXPECT_CALL(mock, MakeUniqueBase()) .WillOnce(Return(ByMove(std::unique_ptr(d)))); std::unique_ptr result1 = mock.MakeUnique(); EXPECT_EQ(19, *result1); std::vector> vresult = mock.MakeVectorUnique(); EXPECT_EQ(1u, vresult.size()); EXPECT_NE(nullptr, vresult[0]); EXPECT_EQ(7, *vresult[0]); std::unique_ptr result2 = mock.MakeUniqueBase(); EXPECT_EQ(d, result2.get()); } TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) { testing::MockFunction mock_function; MockClass mock; std::unique_ptr i(new int(19)); EXPECT_CALL(mock_function, Call()); EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll( InvokeWithoutArgs(&mock_function, &testing::MockFunction::Call), Return(ByMove(std::move(i))))); std::unique_ptr result1 = mock.MakeUnique(); EXPECT_EQ(19, *result1); } TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) { MockClass mock; // Check default value DefaultValue>::SetFactory([] { return std::unique_ptr(new int(42)); }); EXPECT_EQ(42, *mock.MakeUnique()); EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource)); EXPECT_CALL(mock, MakeVectorUnique()) .WillRepeatedly(Invoke(VectorUniquePtrSource)); std::unique_ptr result1 = mock.MakeUnique(); EXPECT_EQ(19, *result1); std::unique_ptr result2 = mock.MakeUnique(); EXPECT_EQ(19, *result2); EXPECT_NE(result1, result2); std::vector> vresult = mock.MakeVectorUnique(); EXPECT_EQ(1u, vresult.size()); EXPECT_NE(nullptr, vresult[0]); EXPECT_EQ(7, *vresult[0]); } #endif // GTEST_HAS_STD_UNIQUE_PTR_ } // Unnamed namespace