aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp
blob: 97f19050e42922c8f6970b94174c416bb53b2acd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <library/cpp/testing/gmock_in_unittest/gmock.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/string.h>

// Set this variable to true if you want to see failures
/////////////////////////////////////////////////////////
static const bool fail = false;
/////////////////////////////////////////////////////////

class ITestIface {
public:
    virtual ~ITestIface() {
    }

    virtual void Func1() = 0;

    virtual int Func2(const TString&) const = 0;
};

class TTestMock: public ITestIface {
public:
    MOCK_METHOD(void, Func1, (), (override));
    MOCK_METHOD(int, Func2, (const TString&), (const, override));
};

using namespace testing;

Y_UNIT_TEST_SUITE(TExampleGMockTest) {
    Y_UNIT_TEST(TSimpleTest) {
        TTestMock mock;
        EXPECT_CALL(mock, Func1())
            .Times(AtLeast(1));

        if (!fail) {
            mock.Func1();
        }
    }

    Y_UNIT_TEST(TNonExpectedCallTest) {
        TTestMock mock;
        EXPECT_CALL(mock, Func1())
            .Times(AtMost(1));
        mock.Func1();
        if (fail) {
            mock.Func1();
        }
    }

    Y_UNIT_TEST(TReturnValuesTest) {
        TTestMock mock;
        EXPECT_CALL(mock, Func2(TString("1")))
            .WillOnce(Return(1))
            .WillRepeatedly(Return(42));

        EXPECT_CALL(mock, Func2(TString("hello")))
            .WillOnce(Return(-1));

        UNIT_ASSERT_VALUES_EQUAL(mock.Func2("hello"), -1);

        UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 1);
        UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42);
        UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42);
        UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42);
        UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42);

        if (fail) {
            UNIT_ASSERT_VALUES_EQUAL(mock.Func2("hello"), -1); // expected to return -1 only once
        }
    }

    Y_UNIT_TEST(TStrictCallSequenceTest) {
        TTestMock mock;
        {
            InSequence seq;
            EXPECT_CALL(mock, Func1())
                .Times(1);
            EXPECT_CALL(mock, Func2(_))
                .Times(2)
                .WillOnce(Return(1))
                .WillOnce(Return(2));
            EXPECT_CALL(mock, Func1());
        }
        mock.Func1();
        UNIT_ASSERT_VALUES_EQUAL(mock.Func2("sample"), 1);
        if (fail) {
            mock.Func1();
        }
        UNIT_ASSERT_VALUES_EQUAL(mock.Func2(""), 2);
        if (!fail) {
            mock.Func1();
        }
    }

    Y_UNIT_TEST(TUninterestingMethodIsFailureTest) {
        StrictMock<TTestMock> mock;
        EXPECT_CALL(mock, Func1())
            .Times(1);
        mock.Func1();
        if (fail) {
            mock.Func1();
        }
    }
}