gmock_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2009 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. //
  5. // This test is a simple sanity check to make sure gmock is able to build/link
  6. // correctly. It just instantiates a mock object and runs through a couple of
  7. // the basic mock features.
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. // Gmock matchers and actions that we use below.
  11. using testing::AnyOf;
  12. using testing::Eq;
  13. using testing::Return;
  14. using testing::SetArgPointee;
  15. using testing::WithArg;
  16. using testing::_;
  17. namespace {
  18. // Simple class that we can mock out the behavior for. Everything is virtual
  19. // for easy mocking.
  20. class SampleClass {
  21. public:
  22. SampleClass() = default;
  23. virtual ~SampleClass() = default;
  24. virtual int ReturnSomething() {
  25. return -1;
  26. }
  27. virtual void ReturnNothingConstly() const {
  28. }
  29. virtual void OutputParam(int* a) {
  30. }
  31. virtual int ReturnSecond(int a, int b) {
  32. return b;
  33. }
  34. };
  35. // Declare a mock for the class.
  36. class MockSampleClass : public SampleClass {
  37. public:
  38. MOCK_METHOD(int, ReturnSomething, ());
  39. MOCK_METHOD(void, ReturnNothingConstly, (), (const));
  40. MOCK_METHOD(void, OutputParam, (int* a));
  41. MOCK_METHOD(int, ReturnSecond, (int a, int b));
  42. };
  43. // Create a couple of custom actions. Custom actions can be used for adding
  44. // more complex behavior into your mock...though if you start needing these, ask
  45. // if you're asking your mock to do too much.
  46. ACTION(ReturnVal) {
  47. // Return the first argument received.
  48. return arg0;
  49. }
  50. ACTION(ReturnSecond) {
  51. // Returns the second argument. This basically implemetns ReturnSecond.
  52. return arg1;
  53. }
  54. TEST(GmockTest, SimpleMatchAndActions) {
  55. // Basic test of some simple gmock matchers, actions, and cardinality
  56. // expectations.
  57. MockSampleClass mock;
  58. EXPECT_CALL(mock, ReturnSomething())
  59. .WillOnce(Return(1))
  60. .WillOnce(Return(2))
  61. .WillOnce(Return(3));
  62. EXPECT_EQ(1, mock.ReturnSomething());
  63. EXPECT_EQ(2, mock.ReturnSomething());
  64. EXPECT_EQ(3, mock.ReturnSomething());
  65. EXPECT_CALL(mock, ReturnNothingConstly()).Times(2);
  66. mock.ReturnNothingConstly();
  67. mock.ReturnNothingConstly();
  68. }
  69. TEST(GmockTest, AssignArgument) {
  70. // Capture an argument for examination.
  71. MockSampleClass mock;
  72. EXPECT_CALL(mock, OutputParam(_)).WillRepeatedly(SetArgPointee<0>(5));
  73. int arg = 0;
  74. mock.OutputParam(&arg);
  75. EXPECT_EQ(5, arg);
  76. }
  77. TEST(GmockTest, SideEffects) {
  78. // Capture an argument for examination.
  79. MockSampleClass mock;
  80. EXPECT_CALL(mock, OutputParam(_)).WillRepeatedly(SetArgPointee<0>(5));
  81. int arg = 0;
  82. mock.OutputParam(&arg);
  83. EXPECT_EQ(5, arg);
  84. }
  85. TEST(GmockTest, CustomAction_ReturnSecond) {
  86. // Test a mock of the ReturnSecond behavior using an action that provides an
  87. // alternate implementation of the function. Danger here though, this is
  88. // starting to add too much behavior of the mock, which means the mock
  89. // implementation might start to have bugs itself.
  90. MockSampleClass mock;
  91. EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5))))
  92. .WillRepeatedly(ReturnSecond());
  93. EXPECT_EQ(4, mock.ReturnSecond(-1, 4));
  94. EXPECT_EQ(5, mock.ReturnSecond(0, 5));
  95. EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4));
  96. EXPECT_EQ(4, mock.ReturnSecond(112358, 4));
  97. EXPECT_EQ(5, mock.ReturnSecond(1337, 5));
  98. }
  99. TEST(GmockTest, CustomAction_ReturnVal) {
  100. // Alternate implemention of ReturnSecond using a more general custom action,
  101. // and a WithArg adapter to bridge the interfaces.
  102. MockSampleClass mock;
  103. EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5))))
  104. .WillRepeatedly(WithArg<1>(ReturnVal()));
  105. EXPECT_EQ(4, mock.ReturnSecond(-1, 4));
  106. EXPECT_EQ(5, mock.ReturnSecond(0, 5));
  107. EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4));
  108. EXPECT_EQ(4, mock.ReturnSecond(112358, 4));
  109. EXPECT_EQ(5, mock.ReturnSecond(1337, 5));
  110. }
  111. } // namespace