• Home
  • Raw
  • Download

Lines Matching +full:mock +full:- +full:tests

30 // Google Mock - a framework for writing C++ mock classes.
32 // This file tests the built-in actions.
34 #include "gmock/gmock-actions.h"
48 #include "gmock/internal/gmock-port.h"
49 #include "gtest/gtest-spi.h"
51 #include "gtest/internal/gtest-port.h"
91 internal::negation<std::integral_constant<int, -1>>>::value, in TEST()
102 // false-y input".
107 struct MyTrue : std::integral_constant<int, -1> {};
194 // The first overload is callable for const and non-const rvalues and lvalues. in TEST()
223 // non-moveable objects, everything should work fine for non-moveable rsult in TEST()
256 // Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
263 // Tests that BuiltInDefaultValue<T*>::Exists() return true.
270 // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
271 // built-in numeric type.
299 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
300 // built-in numeric type.
324 // Tests that BuiltInDefaultValue<bool>::Get() returns false.
329 // Tests that BuiltInDefaultValue<bool>::Exists() returns true.
334 // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
340 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
346 // Tests that BuiltInDefaultValue<const T>::Get() returns the same
390 // Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
401 // Tests that DefaultValue<T>::IsSet() is false initially.
408 // Tests that DefaultValue<T> can be set and then unset.
433 // Tests that DefaultValue<T>::Get() returns the
458 // Tests that DefaultValue<void>::Get() returns void.
461 // Tests using DefaultValue with a reference type.
463 // Tests that DefaultValue<T&>::IsSet() is false initially.
470 // Tests that DefaultValue<T&>::Exists is false initially.
477 // Tests that DefaultValue<T&> can be set and then unset.
500 // Tests that DefaultValue<T&>::Get() returns the
512 // Tests that ActionInterface can be implemented by defining the
535 // 0-tuple; if F is void(bool, int), then Perform() takes a in TEST()
540 // Tests that Action<F> can be constructed from a pointer to
546 // Tests that Action<F> delegates actual work to ActionInterface<F>.
554 // Tests that Action<F> can be copied.
557 Action<MyGlobalFunction> a2(a1); // Tests the copy constructor. in TEST()
567 a2 = a1; // Tests the assignment operator. in TEST()
578 // Tests that an Action<From> object can be converted to a
603 // const or not. This lets us verify the non-const case.
639 // Tests that MakePolymorphicAction() turns a polymorphic action
646 // Tests that MakePolymorphicAction() works when the implementation
656 // Tests that Return() works as an action for void-returning
663 // Tests that Return(v) returns v.
668 ret = Return(-5); in TEST()
669 EXPECT_EQ(-5, ret.Perform(std::make_tuple())); in TEST()
672 // Tests that Return("string literal") works.
681 // Return(x) should work fine when the mock function's return type is a
682 // reference-like wrapper for decltype(x), as when x is a std::string and the
683 // mock function returns std::string_view.
691 // Set up an action for a mock function that returns the reference wrapper in TEST()
696 // as the mock object is alive), rather than e.g. a reference to the temporary in TEST()
699 MockFunction<Result()> mock; in TEST() local
700 EXPECT_CALL(mock, Call) in TEST()
704 EXPECT_THAT(mock.AsStdFunction()(), in TEST()
707 EXPECT_THAT(mock.AsStdFunction()(), in TEST()
732 EXPECT_THAT([]() -> Out { return In(); }(), Field(&Out::x, 19)); in TEST()
734 // Return should work the same way: if the mock function's return type is Out in TEST()
737 MockFunction<Out()> mock; in TEST() local
738 EXPECT_CALL(mock, Call).WillOnce(Return(In())); in TEST()
739 EXPECT_THAT(mock.AsStdFunction()(), Field(&Out::x, 19)); in TEST()
742 // It should be possible to use Return(R) with a mock function result type U
753 MockFunction<U()> mock; in TEST() local
754 EXPECT_CALL(mock, Call).WillOnce(Return(17)).WillRepeatedly(Return(19)); in TEST()
756 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
757 EXPECT_EQ(19, mock.AsStdFunction()()); in TEST()
760 // Return(x) should not be usable with a mock function result type that's
761 // implicitly convertible from decltype(x) but requires a non-const lvalue
794 // Return should support move-only result types when used with WillOnce. in TEST()
796 MockFunction<std::unique_ptr<int>()> mock; in TEST() local
797 EXPECT_CALL(mock, Call) in TEST()
801 EXPECT_THAT(mock.AsStdFunction()(), Pointee(17)); in TEST()
811 // Tests that Return(v) is covariant.
831 // Tests that the type of the value passed into Return is converted into T
834 // gmock-actions.h for more information.
862 // Tests that ReturnNull() returns NULL in a pointer-returning function.
871 // Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning
881 // Tests that ReturnRef(v) works for reference types.
889 // Tests that ReturnRef(v) is covariant.
906 // Tests that ReturnRef(v) is working with non-temporaries (T&)
921 // Tests that ReturnRef(v) is not working with temporaries (T&&)
923 auto scalar_value = []() -> int { return 123; }; in TEST()
926 auto non_scalar_value = []() -> std::string { return "ABC"; }; in TEST()
933 auto const_non_scalar_value = []() -> const std::string { return "CBA"; }; in TEST()
937 // Tests that ReturnRefOfCopy(v) works for reference types.
950 // Tests that ReturnRefOfCopy(v) is covariant.
961 // Tests that ReturnRoundRobin(v) works with initializer lists
973 // Tests that ReturnRoundRobin(v) works with vectors
986 // Tests that DoDefault() does the default action for the mock method.
1006 // Tests that DoDefault() returns the built-in default value for the
1009 MockClass mock; in TEST() local
1010 EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault()); in TEST()
1011 EXPECT_EQ(0, mock.IntFunc(true)); in TEST()
1014 // Tests that DoDefault() throws (when exceptions are enabled) or aborts
1015 // the process when there is no built-in default value for the return type.
1017 MockClass mock; in TEST() local
1018 EXPECT_CALL(mock, Foo()).WillRepeatedly(DoDefault()); in TEST()
1020 EXPECT_ANY_THROW(mock.Foo()); in TEST()
1022 EXPECT_DEATH_IF_SUPPORTED({ mock.Foo(); }, ""); in TEST()
1026 // Tests that using DoDefault() inside a composite action leads to a
1027 // run-time error.
1032 MockClass mock; in TEST() local
1033 EXPECT_CALL(mock, IntFunc(_)) in TEST()
1037 // EXPECT_DEATH() can only capture stderr, while Google Mock's in TEST()
1040 EXPECT_DEATH_IF_SUPPORTED({ mock.IntFunc(true); }, ""); in TEST()
1043 // Tests that DoDefault() returns the default value set by
1047 MockClass mock; in TEST() local
1048 EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault()); in TEST()
1049 EXPECT_EQ(1, mock.IntFunc(false)); in TEST()
1053 // Tests that DoDefault() does the action specified by ON_CALL().
1055 MockClass mock; in TEST() local
1056 ON_CALL(mock, IntFunc(_)).WillByDefault(Return(2)); in TEST()
1057 EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault()); in TEST()
1058 EXPECT_EQ(2, mock.IntFunc(false)); in TEST()
1061 // Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
1063 MockClass mock; in TEST() local
1066 ON_CALL(mock, IntFunc(_)).WillByDefault(DoDefault()); in TEST()
1071 // Tests that SetArgPointee<N>(v) sets the variable pointed to by
1072 // the N-th (0-based) argument to v.
1091 // Tests that SetArgPointee<N>() accepts a string literal.
1126 // Tests that SetArgPointee<N>() accepts a char pointer.
1166 // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
1167 // the N-th (0-based) argument to v.
1227 // Tests InvokeWithoutArgs(function).
1244 // Tests InvokeWithoutArgs(functor).
1262 // Tests InvokeWithoutArgs(obj_ptr, method).
1270 // Tests using IgnoreResult() on a polymorphic action.
1276 // Tests using IgnoreResult() on a monomorphic action.
1290 // Tests using IgnoreResult() on an action that returns a class type.
1326 // DoAll should support &&-qualified actions when used with WillOnce.
1336 MockFunction<int(int)> mock; in TEST() local
1337 EXPECT_CALL(mock, Call).WillOnce(DoAll(InitialAction{}, FinalAction{})); in TEST()
1338 EXPECT_EQ(19, mock.AsStdFunction()(17)); in TEST()
1342 // mock action itself accepts an rvalue reference or a non-scalar object by
1348 // Mock action accepts by value: the initial action should be fed a const in TEST()
1358 MockFunction<void(Obj)> mock; in TEST() local
1359 EXPECT_CALL(mock, Call) in TEST()
1363 mock.AsStdFunction()(Obj{}); in TEST()
1364 mock.AsStdFunction()(Obj{}); in TEST()
1367 // Mock action accepts by const lvalue reference: both actions should receive in TEST()
1377 MockFunction<void(const Obj&)> mock; in TEST() local
1378 EXPECT_CALL(mock, Call) in TEST()
1383 mock.AsStdFunction()(Obj{}); in TEST()
1384 mock.AsStdFunction()(Obj{}); in TEST()
1387 // Mock action accepts by non-const lvalue reference: both actions should get in TEST()
1388 // a non-const lvalue reference if they want them. in TEST()
1395 MockFunction<void(Obj&)> mock; in TEST() local
1396 EXPECT_CALL(mock, Call) in TEST()
1401 mock.AsStdFunction()(obj); in TEST()
1402 mock.AsStdFunction()(obj); in TEST()
1405 // Mock action accepts by rvalue reference: the initial actions should receive in TEST()
1406 // a non-const lvalue reference if it wants it, and the final action an rvalue in TEST()
1414 MockFunction<void(Obj&&)> mock; in TEST() local
1415 EXPECT_CALL(mock, Call) in TEST()
1419 mock.AsStdFunction()(Obj{}); in TEST()
1420 mock.AsStdFunction()(Obj{}); in TEST()
1423 // &&-qualified initial actions should also be allowed with WillOnce. in TEST()
1429 MockFunction<void(Obj&)> mock; in TEST() local
1430 EXPECT_CALL(mock, Call) in TEST()
1434 mock.AsStdFunction()(obj); in TEST()
1442 MockFunction<void(Obj&&)> mock; in TEST() local
1443 EXPECT_CALL(mock, Call) in TEST()
1446 mock.AsStdFunction()(Obj{}); in TEST()
1450 // DoAll should support being used with type-erased Action objects, both through
1453 // With only type-erased actions. in TEST()
1457 MockFunction<int()> mock; in TEST() local
1458 EXPECT_CALL(mock, Call) in TEST()
1462 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
1464 // With &&-qualified and move-only final action. in TEST()
1473 EXPECT_CALL(mock, Call) in TEST()
1476 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
1480 // Tests using WithArgs and with an action that takes 1 argument.
1483 EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1))); in TEST()
1487 // Tests using WithArgs with an action that takes 2 arguments.
1503 // Tests using WithArgs with an action that takes 10 arguments.
1512 // Tests using WithArgs with an action that is not Invoke().
1516 return std::get<0>(args) - std::get<1>(args); in Perform()
1528 // Tests using WithArgs to pass all original arguments in the original order.
1535 // Tests using WithArgs with repeated arguments.
1542 // Tests using WithArgs with reversed argument order.
1550 // Tests using WithArgs with compatible, but not identical, argument types.
1558 // Tests using WithArgs with an action that returns void.
1567 Action<int&(int&, void*)> aa = WithArgs<0>([](int& a) -> int& { return a; }); in TEST()
1576 MockFunction<Base*(double)> mock; in TEST() local
1577 EXPECT_CALL(mock, Call) in TEST()
1581 EXPECT_EQ(nullptr, mock.AsStdFunction()(1.1)); in TEST()
1582 EXPECT_EQ(nullptr, mock.AsStdFunction()(1.1)); in TEST()
1585 // It should be possible to use an &&-qualified inner action as long as the
1595 MockFunction<int(int, int)> mock; in TEST() local
1596 EXPECT_CALL(mock, Call).WillOnce(WithArg<1>(SomeAction{})); in TEST()
1597 EXPECT_EQ(19, mock.AsStdFunction()(0, 17)); in TEST()
1609 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5); in TEST_F()
1610 EXPECT_EQ(-5, a.Perform(std::make_tuple())); in TEST_F()
1629 // Tests ByRef().
1631 // Tests that the result of ByRef() is copyable.
1652 // Tests using ByRef() on a const value.
1655 // int& ref = ByRef(n); // This shouldn't compile - we have a in TEST()
1661 // Tests using ByRef() on a non-const value.
1674 // Tests explicitly specifying the type when using ByRef().
1680 // ByRef<char>(n); // This shouldn't compile - we have a negative in TEST()
1696 // The following shouldn't compile - we have a negative compilation in TEST()
1703 // Tests that Google Mock prints expression ByRef(x) as a reference to x.
1717 // Tests using ReturnNew() with a unary constructor.
1721 EXPECT_EQ(4000, c->value); in TEST()
1729 EXPECT_EQ(4000, c->value); in TEST()
1737 EXPECT_EQ(4000, c->value); in TEST()
1749 // Tests using ReturnNew() with a 10-argument constructor.
1755 EXPECT_EQ(1234567890, c->value_); in TEST()
1768 MockClass mock; in TEST() local
1770 EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i)))); in TEST()
1771 EXPECT_CALL(mock, MakeVectorUnique()) in TEST()
1774 EXPECT_CALL(mock, MakeUniqueBase()) in TEST()
1777 std::unique_ptr<int> result1 = mock.MakeUnique(); in TEST()
1780 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique(); in TEST()
1785 std::unique_ptr<Base> result2 = mock.MakeUniqueBase(); in TEST()
1791 MockClass mock; in TEST() local
1794 EXPECT_CALL(mock, MakeUnique()) in TEST()
1799 std::unique_ptr<int> result1 = mock.MakeUnique(); in TEST()
1804 MockClass mock; in TEST() local
1809 EXPECT_EQ(42, *mock.MakeUnique()); in TEST()
1811 EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource)); in TEST()
1812 EXPECT_CALL(mock, MakeVectorUnique()) in TEST()
1814 std::unique_ptr<int> result1 = mock.MakeUnique(); in TEST()
1816 std::unique_ptr<int> result2 = mock.MakeUnique(); in TEST()
1820 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique(); in TEST()
1827 MockClass mock; in TEST() local
1830 EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) { in TEST()
1834 // EXPECT_CALL(mock, TakeUnique(_, _)) in TEST()
1837 EXPECT_CALL(mock, TakeUnique(testing::Pointee(7))) in TEST()
1838 .WillOnce(Return(-7)) in TEST()
1840 EXPECT_CALL(mock, TakeUnique(testing::IsNull())) in TEST()
1841 .WillOnce(Return(-1)) in TEST()
1844 EXPECT_EQ(5, mock.TakeUnique(make(5))); in TEST()
1845 EXPECT_EQ(-7, mock.TakeUnique(make(7))); in TEST()
1846 EXPECT_EQ(7, mock.TakeUnique(make(7))); in TEST()
1847 EXPECT_EQ(7, mock.TakeUnique(make(7))); in TEST()
1848 EXPECT_EQ(-1, mock.TakeUnique({})); in TEST()
1852 EXPECT_CALL(mock, TakeUnique(_, _)) in TEST()
1856 EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7))); in TEST()
1860 EXPECT_CALL(mock, TakeUnique(_)).WillOnce([&saved](std::unique_ptr<int> i) { in TEST()
1864 EXPECT_EQ(0, mock.TakeUnique(make(42))); in TEST()
1868 // It should be possible to use callables with an &&-qualified call operator
1870 // contain and manipulate move-only types.
1878 MockFunction<int()> mock; in TEST() local
1879 EXPECT_CALL(mock, Call).WillOnce(Return17()); in TEST()
1881 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
1886 MockFunction<int(int)> mock; in TEST() local
1887 EXPECT_CALL(mock, Call).WillOnce(Return17()); in TEST()
1889 EXPECT_EQ(17, mock.AsStdFunction()(0)); in TEST()
1893 // Edge case: if an action has both a const-qualified and an &&-qualified call
1894 // operator, there should be no "ambiguous call" errors. The &&-qualified
1896 // action beyond one call), and the const-qualified one by WillRepeatedly.
1905 MockFunction<int()> mock; in TEST() local
1906 EXPECT_CALL(mock, Call).WillOnce(ReturnInt()).WillRepeatedly(ReturnInt()); in TEST()
1908 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
1909 EXPECT_EQ(19, mock.AsStdFunction()()); in TEST()
1910 EXPECT_EQ(19, mock.AsStdFunction()()); in TEST()
1915 MockFunction<int(int)> mock; in TEST() local
1916 EXPECT_CALL(mock, Call).WillOnce(ReturnInt()).WillRepeatedly(ReturnInt()); in TEST()
1918 EXPECT_EQ(17, mock.AsStdFunction()(0)); in TEST()
1919 EXPECT_EQ(19, mock.AsStdFunction()(0)); in TEST()
1920 EXPECT_EQ(19, mock.AsStdFunction()(0)); in TEST()
1924 // WillOnce should have no problem coping with a move-only action, whether it is
1925 // &&-qualified or not.
1927 // &&-qualified in TEST()
1939 MockFunction<int()> mock; in TEST() local
1940 EXPECT_CALL(mock, Call).WillOnce(Return17()); in TEST()
1941 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
1944 // Not &&-qualified in TEST()
1956 MockFunction<int()> mock; in TEST() local
1957 EXPECT_CALL(mock, Call).WillOnce(Return17()); in TEST()
1958 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
1962 // It should be possible to use an action that returns a value with a mock
1969 MockFunction<void()> mock; in TEST() local
1970 EXPECT_CALL(mock, Call).WillOnce(ReturnInt()).WillRepeatedly(ReturnInt()); in TEST()
1972 mock.AsStdFunction()(); in TEST()
1973 mock.AsStdFunction()(); in TEST()
1976 // Despite the fanciness around move-only actions and so on, it should still be
1979 MockFunction<int()> mock; in TEST() local
1982 EXPECT_CALL(mock, Call).WillOnce(action); in TEST()
1984 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
2007 MockFunction<int(int)> mock; in TEST() local
2008 EXPECT_CALL(mock, Call) in TEST()
2012 EXPECT_EQ(17, mock.AsStdFunction()(0)); in TEST()
2013 EXPECT_EQ(17, mock.AsStdFunction()(0)); in TEST()
2032 MockFunction<int()> mock; in TEST() local
2033 EXPECT_CALL(mock, Call) in TEST()
2037 EXPECT_EQ(17, mock.AsStdFunction()()); in TEST()
2038 EXPECT_EQ(19, mock.AsStdFunction()()); in TEST()
2041 // Tests for std::function based action.
2130 // Test that basic built-in actions work with move-only arguments.