Skip to content

Commit 4c4e340

Browse files
committed
[GMock] Support template classes
1 parent b345e64 commit 4c4e340

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

include/GUnit/GMock.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,20 @@ inline auto ByRef(NiceGMock<T> &x) {
400400

401401
#undef EXPECT_CALL
402402
#define __GMOCK_EXPECT_CALL_0(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
403-
#define __GMOCK_EXPECT_CALL_1(obj, call) \
404-
if (::testing::detail::gmock_ready) \
405-
((obj).gmock_call<__GMOCK_QNAME call>(__GUNIT_CAT(__GMOCK_OVERLOAD_CAST_IMPL_, __GMOCK_OVERLOAD_CALL call)(obj, call) & \
406-
std::decay_t<decltype(obj)>::type::__GMOCK_NAME call __GMOCK_CALL call)) \
403+
#define __GMOCK_EXPECT_CALL_1(obj, call) \
404+
if (::testing::detail::gmock_ready) \
405+
((obj).template gmock_call<__GMOCK_QNAME call>( \
406+
__GUNIT_CAT(__GMOCK_OVERLOAD_CAST_IMPL_, __GMOCK_OVERLOAD_CALL call)(obj, call) & \
407+
std::decay_t<decltype(obj)>::type::__GMOCK_NAME call __GMOCK_CALL call)) \
407408
.InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
408409
#define EXPECT_CALL(obj, call) __GUNIT_CAT(__GMOCK_EXPECT_CALL_, __GUNIT_IBP(call))(obj, call)
409410

410411
#undef ON_CALL
411412
#define __GMOCK_ON_CALL_0(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)
412-
#define __GMOCK_ON_CALL_1(obj, call) \
413-
if (::testing::detail::gmock_ready) \
414-
((obj).gmock_call<__GMOCK_QNAME call>(__GUNIT_CAT(__GMOCK_OVERLOAD_CAST_IMPL_, __GMOCK_OVERLOAD_CALL call)(obj, call) & \
415-
std::decay_t<decltype(obj)>::type::__GMOCK_NAME call __GMOCK_CALL call)) \
413+
#define __GMOCK_ON_CALL_1(obj, call) \
414+
if (::testing::detail::gmock_ready) \
415+
((obj).template gmock_call<__GMOCK_QNAME call>( \
416+
__GUNIT_CAT(__GMOCK_OVERLOAD_CAST_IMPL_, __GMOCK_OVERLOAD_CALL call)(obj, call) & \
417+
std::decay_t<decltype(obj)>::type::__GMOCK_NAME call __GMOCK_CALL call)) \
416418
.InternalDefaultActionSetAt(__FILE__, __LINE__, #obj, #call)
417419
#define ON_CALL(obj, call) __GUNIT_CAT(__GMOCK_ON_CALL_, __GUNIT_IBP(call))(obj, call)

include/GUnit/GTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class GTestAutoRegister {
6868
public:
6969
GTestAutoRegister() {
7070
ScopedVisibility _;
71-
//static constexpr auto has_tests = std::is_same<decltype(T{}.test()), void>::value;
72-
//static_assert(not has_tests, "At least one SHOULD/test is required!");
71+
// static constexpr auto has_tests = std::is_same<decltype(T{}.test()), void>::value;
72+
// static_assert(not has_tests, "At least one SHOULD/test is required!");
7373
T{}.test();
7474
}
7575
};

test/GMock.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,28 @@ TEST(GMock, ShouldHandleON_CALLWithOverloadMethods) {
624624
ON_CALL(m, (f, int(int))(42)).WillByDefault(Return(87));
625625
EXPECT_EQ(87, static_cast<interface_overload_ret&>(m).f(42));
626626
}
627+
628+
template <class I>
629+
class GMockT : public testing::Test {
630+
protected:
631+
testing::StrictGMock<I> m;
632+
633+
template <class T>
634+
testing::StrictGMock<T>& mock() {
635+
return m;
636+
}
637+
638+
void SetUp() override {
639+
using namespace testing;
640+
EXPECT_CALL(mock<interface>(), (foo)(42)).Times(1);
641+
EXPECT_CALL(mock<interface>(), (foo)(12)).Times(0);
642+
EXPECT_CALL(mock<interface>(), (bar)(_, "str"));
643+
}
644+
};
645+
646+
using GMockTest = GMockT<interface>;
647+
648+
TEST_F(GMockTest, ShouldHandleTemplatedClass) {
649+
example sut{0, static_cast<interface&>(m)};
650+
sut.update();
651+
}

test/GTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ TEST_F(Test, ShouldOverrideSutAndMocks) {
414414
using namespace testing;
415415
std::tie(sut, mocks) = make<SUT, NaggyGMock>(123);
416416
EXPECT_EQ(123, sut->get_data());
417-
418417
EXPECT_CALL(mock<interface>(), (foo)(42)).Times(1);
419418
EXPECT_CALL(mock<interface>(), (bar)(_, "str"));
420419

0 commit comments

Comments
 (0)