-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathGTest.cpp
More file actions
129 lines (100 loc) · 2.74 KB
/
GTest.cpp
File metadata and controls
129 lines (100 loc) · 2.74 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// Copyright (c) 2016-2017 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "GUnit/GTest.h"
#include "GUnit/GMake.h"
struct interface {
virtual ~interface() = default;
virtual void foo(int) const = 0;
virtual void bar(int, const std::string&) const = 0;
};
class example {
public:
example(int data, interface& i) : data(data), i(i) {}
void update() {
i.foo(42);
i.bar(1, "str");
}
auto get_data() const { return data; }
private:
int data = {};
interface& i;
};
class example_no_data {
public:
explicit example_no_data(interface& i) : i(i) {}
void update() {
i.foo(42);
i.bar(1, "str");
}
private:
interface& i;
};
////////////////////////////////////////////////////////////////////////////////
GTEST("Simple Test", "[True/False should be True/False]") {
EXPECT_TRUE(true);
EXPECT_FALSE(false);
}
GTEST("Vector test") {
std::vector<int> sut{};
EXPECT_TRUE(sut.empty());
SHOULD("increase the size after a push back") {
sut.push_back(42);
EXPECT_EQ(1u, sut.size());
}
SHOULD("increase the size after a emplace back") {
sut.emplace_back(42);
EXPECT_EQ(1u, sut.size());
}
DISABLED_SHOULD("disabled should") {}
SHOULD("do nothing") {}
}
class VectorTest : public testing::Test {
protected:
void SetUp() override { sut.push_back(42); }
std::vector<int> sut;
};
GTEST(VectorTest, "[Using Test]") {
EXPECT_EQ(1u, sut.size()); // from VectorTest::SetUp
sut.push_back(77); // SetUp
SHOULD("increase the size after a emplace back") {
EXPECT_EQ(2u, sut.size());
sut.push_back(21);
EXPECT_EQ(3u, sut.size());
}
}
GTEST(example) {
// SetUp - will be run for each SHOULD section and it will create sut and
// mocks if possible
using namespace testing;
std::tie(sut, mocks) = make<SUT, StrictGMock>(42);
SHOULD("make simple example") {
EXPECT_EQ(42, sut->get_data());
EXPECT_CALL(mock<interface>(), (foo)(42)).Times(1);
EXPECT_CALL(mock<interface>(), (bar)(_, "str"));
sut->update();
}
SHOULD("override example") {
std::tie(sut, mocks) = make<SUT, NaggyGMock>(77);
EXPECT_EQ(77, sut->get_data());
EXPECT_CALL(mock<interface>(), (foo)(42)).Times(1);
EXPECT_CALL(mock<interface>(), (bar)(_, "str"));
sut->update();
}
// TearDown
}
GTEST(example_no_data) {
// SetUp - will be run for each SHOULD section and it will create sut and
// mocks if possible
using namespace testing;
SHOULD("make simple example") {
EXPECT_CALL(mock<interface>(), (foo)(42)).Times(1);
EXPECT_CALL(mock<interface>(), (bar)(_, "str"));
sut->update();
}
// TearDown
}