This source file includes following definitions.
- ReturnSomething
- ReturnNothingConstly
- OutputParam
- ReturnSecond
- ACTION
- ACTION
- TEST
- TEST
- TEST
- TEST
- TEST
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::AnyOf;
using testing::Eq;
using testing::Return;
using testing::SetArgumentPointee;
using testing::WithArg;
using testing::_;
namespace {
class SampleClass {
public:
SampleClass() {}
virtual ~SampleClass() {}
virtual int ReturnSomething() {
return -1;
}
virtual void ReturnNothingConstly() const {
}
virtual void OutputParam(int* a) {
}
virtual int ReturnSecond(int a, int b) {
return b;
}
};
class MockSampleClass : public SampleClass {
public:
MOCK_METHOD0(ReturnSomething, int());
MOCK_CONST_METHOD0(ReturnNothingConstly, void());
MOCK_METHOD1(OutputParam, void(int* a));
MOCK_METHOD2(ReturnSecond, int(int a, int b));
};
ACTION(ReturnVal) {
return arg0;
}
ACTION(ReturnSecond) {
return arg1;
}
TEST(GmockTest, SimpleMatchAndActions) {
MockSampleClass mock;
EXPECT_CALL(mock, ReturnSomething())
.WillOnce(Return(1))
.WillOnce(Return(2))
.WillOnce(Return(3));
EXPECT_EQ(1, mock.ReturnSomething());
EXPECT_EQ(2, mock.ReturnSomething());
EXPECT_EQ(3, mock.ReturnSomething());
EXPECT_CALL(mock, ReturnNothingConstly()).Times(2);
mock.ReturnNothingConstly();
mock.ReturnNothingConstly();
}
TEST(GmockTest, AssignArgument) {
MockSampleClass mock;
EXPECT_CALL(mock, OutputParam(_))
.WillRepeatedly(SetArgumentPointee<0>(5));
int arg = 0;
mock.OutputParam(&arg);
EXPECT_EQ(5, arg);
}
TEST(GmockTest, SideEffects) {
MockSampleClass mock;
EXPECT_CALL(mock, OutputParam(_))
.WillRepeatedly(SetArgumentPointee<0>(5));
int arg = 0;
mock.OutputParam(&arg);
EXPECT_EQ(5, arg);
}
TEST(GmockTest, CustomAction_ReturnSecond) {
MockSampleClass mock;
EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5))))
.WillRepeatedly(ReturnSecond());
EXPECT_EQ(4, mock.ReturnSecond(-1, 4));
EXPECT_EQ(5, mock.ReturnSecond(0, 5));
EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4));
EXPECT_EQ(4, mock.ReturnSecond(112358, 4));
EXPECT_EQ(5, mock.ReturnSecond(1337, 5));
}
TEST(GmockTest, CustomAction_ReturnVal) {
MockSampleClass mock;
EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5))))
.WillRepeatedly(WithArg<1>(ReturnVal()));
EXPECT_EQ(4, mock.ReturnSecond(-1, 4));
EXPECT_EQ(5, mock.ReturnSecond(0, 5));
EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4));
EXPECT_EQ(4, mock.ReturnSecond(112358, 4));
EXPECT_EQ(5, mock.ReturnSecond(1337, 5));
}
}