Skip to content

Commit 30a0589

Browse files
committed
Add back tests from previous implementation.
1 parent 0c12557 commit 30a0589

File tree

6 files changed

+237
-30
lines changed

6 files changed

+237
-30
lines changed

tests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ add_executable(FakeIt_tests
1010
functional.cpp
1111
gcc_stubbing_multiple_values_tests.cpp
1212
gcc_type_info_tests.cpp
13+
inherited_funcs_tests.cpp
1314
miscellaneous_tests.cpp
1415
move_only_return_tests.cpp
15-
moving_mocks_around.cpp
16+
moving_mocks_around_tests.cpp
1617
msc_stubbing_multiple_values_tests.cpp
1718
msc_type_info_tests.cpp
1819
overloadded_methods_tests.cpp

tests/inherited_funcs_tests.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2014 Eran Pe'er.
3+
*
4+
* This program is made available under the terms of the MIT License.
5+
*
6+
* Created on Mar 10, 2014
7+
*/
8+
9+
#include "tpunit++.hpp"
10+
#include "fakeit.hpp"
11+
12+
using namespace fakeit;
13+
14+
struct InheritedFuncsTests : tpunit::TestFixture
15+
{
16+
17+
InheritedFuncsTests() :
18+
TestFixture(
19+
TEST(InheritedFuncsTests::mock_base_overloaded_functions),
20+
TEST(InheritedFuncsTests::mock_base_and_child_overloaded_functions)
21+
)
22+
{
23+
}
24+
25+
class BaseInterface
26+
{
27+
public:
28+
virtual ~BaseInterface() = default;
29+
30+
virtual double nonOverloadedMethod() = 0;
31+
virtual double overloadedMethod() = 0;
32+
virtual double overloadedMethod() const = 0;
33+
virtual double overloadedInChildMethod() = 0;
34+
};
35+
36+
class Interface : public BaseInterface
37+
{
38+
public:
39+
~Interface() override = default;
40+
using BaseInterface::overloadedInChildMethod;
41+
virtual double overloadedInChildMethod() const = 0;
42+
};
43+
44+
void mock_base_overloaded_functions()
45+
{
46+
Mock<Interface> mock;
47+
48+
When(Method(mock, nonOverloadedMethod)).Return(1.5);
49+
When(OverloadedMethod(mock, overloadedMethod, double())).Return(2.5);
50+
When(ConstOverloadedMethod(mock, overloadedMethod, double())).Return(3.5);
51+
52+
Interface& interface = mock.get();
53+
const Interface& constInterface = mock.get();
54+
55+
EXPECT_EQUAL(interface.nonOverloadedMethod(), 1.5);
56+
EXPECT_EQUAL(interface.overloadedMethod(), 2.5);
57+
EXPECT_EQUAL(constInterface.overloadedMethod(), 3.5);
58+
59+
Verify(Method(mock, nonOverloadedMethod)).Exactly(1);
60+
Verify(OverloadedMethod(mock, overloadedMethod, double())).Exactly(1);
61+
Verify(ConstOverloadedMethod(mock, overloadedMethod, double())).Exactly(1);
62+
}
63+
64+
void mock_base_and_child_overloaded_functions()
65+
{
66+
Mock<Interface> mock;
67+
68+
When(OverloadedMethod(mock, overloadedInChildMethod, double())).Return(4.5);
69+
When(ConstOverloadedMethod(mock, overloadedInChildMethod, double())).Return(5.5);
70+
71+
Interface& interface = mock.get();
72+
const Interface& constInterface = mock.get();
73+
74+
EXPECT_EQUAL(interface.overloadedInChildMethod(), 4.5);
75+
EXPECT_EQUAL(constInterface.overloadedInChildMethod(), 5.5);
76+
77+
Verify(OverloadedMethod(mock, overloadedInChildMethod, double())).Exactly(1);
78+
Verify(ConstOverloadedMethod(mock, overloadedInChildMethod, double())).Exactly(1);
79+
}
80+
81+
} __InheritedFuncsTests;
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
using namespace fakeit;
1313

14-
struct MovingMocksAround : tpunit::TestFixture
14+
struct MovingMocksAroundTests : tpunit::TestFixture
1515
{
1616

17-
MovingMocksAround() :
17+
MovingMocksAroundTests() :
1818
TestFixture(
19-
TEST(MovingMocksAround::move_mock),
20-
TEST(MovingMocksAround::move_mock_then_delete),
21-
TEST(MovingMocksAround::create_mock_from_function),
22-
TEST(MovingMocksAround::create_multiple_mocks_from_function)
19+
TEST(MovingMocksAroundTests::move_mock),
20+
TEST(MovingMocksAroundTests::move_mock_then_delete),
21+
TEST(MovingMocksAroundTests::create_mock_from_function),
22+
TEST(MovingMocksAroundTests::create_multiple_mocks_from_function)
2323
)
2424
{
2525
}
@@ -98,4 +98,4 @@ struct MovingMocksAround : tpunit::TestFixture
9898
Verify(Method(mock2, function).Using(paramString2)).Exactly(1);
9999
}
100100

101-
} __MovingMocksAround;
101+
} __MovingMocksAroundTests;
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
#include "multiple_translation_units_stub.h"
22

3+
namespace multiple_tu {
34

4-
void stubFunc2(fakeit::Mock<SomeInterface>& mock)
5-
{
6-
fakeit::When(Method(mock, func2)).AlwaysReturn<std::string>("String");
7-
}
5+
void stubFunc(fakeit::Mock<SomeInterface>& mock)
6+
{
7+
fakeit::When(Method(mock, func)).Return(5);
8+
}
9+
10+
void stubFunc2(fakeit::Mock<SomeInterface>& mock)
11+
{
12+
fakeit::When(Method(mock, func2)).Return("String");
13+
}
14+
15+
void stubMoreFunc(fakeit::Mock<SomeInterface>& mock)
16+
{
17+
fakeit::When(Method(mock, func).Using(1)).Return(10);
18+
fakeit::When(Method(mock, func).Using(2)).Return(20);
19+
}
20+
21+
void stubMoreFunc2(fakeit::Mock<SomeInterface>& mock)
22+
{
23+
fakeit::When(Method(mock, func2).Using(1)).Return("String1");
24+
fakeit::When(Method(mock, func2).Using(2)).Return("String2");
25+
}
826

9-
void stubFunc(fakeit::Mock<SomeInterface>& mock)
10-
{
11-
fakeit::When(Method(mock, func)).AlwaysReturn<int>(3);
1227
}

tests/multiple_translation_units_stub.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44

55
#include "fakeit.hpp"
66

7-
struct SomeInterface {
8-
virtual int func() = 0;
9-
virtual std::string func2() = 0;
10-
};
7+
namespace multiple_tu {
118

12-
void stubFunc2(fakeit::Mock<SomeInterface>& mock);
13-
void stubFunc(fakeit::Mock<SomeInterface>& mock);
9+
struct SomeInterface {
10+
virtual int func(int) = 0;
11+
virtual std::string func2(int) = 0;
12+
};
13+
14+
void stubFunc(fakeit::Mock<SomeInterface>& mock);
15+
void stubFunc2(fakeit::Mock<SomeInterface>& mock);
16+
void stubMoreFunc(fakeit::Mock<SomeInterface>& mock);
17+
void stubMoreFunc2(fakeit::Mock<SomeInterface>& mock);
18+
19+
}

tests/multiple_translation_units_stub_test.cpp

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,126 @@
33
#include "multiple_translation_units_stub.h"
44

55
using namespace fakeit;
6+
using namespace multiple_tu;
67

78
struct MultipleTranslationUnitsStub : tpunit::TestFixture {
8-
MultipleTranslationUnitsStub()
9-
: tpunit::TestFixture(
10-
TEST(MultipleTranslationUnitsStub::NoCollidingIds)
11-
)
12-
{}
139

14-
void NoCollidingIds() {
10+
MultipleTranslationUnitsStub() :
11+
tpunit::TestFixture(
12+
TEST(MultipleTranslationUnitsStub::NoCollidingIdsBasics),
13+
TEST(MultipleTranslationUnitsStub::NoCollidingIdsAlternateBasics),
14+
TEST(MultipleTranslationUnitsStub::NoCollidingIdsMoreFunctionsMocked),
15+
TEST(MultipleTranslationUnitsStub::NoCollidingIdsWhenOverridingMocks)
16+
)
17+
{
18+
}
19+
20+
void NoCollidingIdsBasics()
21+
{
22+
Mock<SomeInterface> mock;
23+
24+
stubFunc(mock);
25+
When(Method(mock, func2)).Return("Something");
26+
27+
SomeInterface &i = mock.get();
28+
29+
// Uncatchable write access violation if ids collide.
30+
EXPECT_EQUAL(i.func(5), 5);
31+
EXPECT_EQUAL(i.func2(5), "Something");
32+
33+
Verify(Method(mock, func).Using(5)).Exactly(1);
34+
Verify(Method(mock, func2).Using(5)).Exactly(1);
35+
}
36+
37+
void NoCollidingIdsAlternateBasics()
38+
{
39+
Mock<SomeInterface> mock;
40+
41+
When(Method(mock, func)).Return(100);
42+
stubFunc2(mock);
43+
44+
SomeInterface &i = mock.get();
45+
46+
// Uncatchable write access violation if ids collide.
47+
EXPECT_EQUAL(i.func(5), 100);
48+
EXPECT_EQUAL(i.func2(5), "String");
49+
50+
Verify(Method(mock, func).Using(5)).Exactly(1);
51+
Verify(Method(mock, func2).Using(5)).Exactly(1);
52+
}
53+
54+
void NoCollidingIdsMoreFunctionsMocked()
55+
{
1556
Mock<SomeInterface> mock;
57+
58+
stubFunc(mock);
59+
stubFunc2(mock);
60+
61+
When(Method(mock, func).Using(20)).Return(20);
62+
When(Method(mock, func).Using(50)).Return(50);
63+
64+
When(Method(mock, func2).Using(20)).Return("Something-20");
65+
When(Method(mock, func2).Using(50)).Return("Something-50");
66+
67+
stubMoreFunc(mock);
68+
stubMoreFunc2(mock);
69+
1670
SomeInterface &i = mock.get();
17-
71+
72+
// Uncatchable write access violation if ids collide.
73+
EXPECT_EQUAL(i.func(1), 10);
74+
EXPECT_EQUAL(i.func(2), 20);
75+
EXPECT_EQUAL(i.func(5), 5);
76+
EXPECT_EQUAL(i.func(20), 20);
77+
EXPECT_EQUAL(i.func(50), 50);
78+
EXPECT_EQUAL(i.func2(1), "String1");
79+
EXPECT_EQUAL(i.func2(2), "String2");
80+
EXPECT_EQUAL(i.func2(5), "String");
81+
EXPECT_EQUAL(i.func2(20), "Something-20");
82+
EXPECT_EQUAL(i.func2(50), "Something-50");
83+
84+
Verify(Method(mock, func).Using(1)).Exactly(1);
85+
Verify(Method(mock, func).Using(2)).Exactly(1);
86+
Verify(Method(mock, func).Using(5)).Exactly(1);
87+
Verify(Method(mock, func).Using(20)).Exactly(1);
88+
Verify(Method(mock, func).Using(50)).Exactly(1);
89+
Verify(Method(mock, func2).Using(1)).Exactly(1);
90+
Verify(Method(mock, func2).Using(2)).Exactly(1);
91+
Verify(Method(mock, func2).Using(5)).Exactly(1);
92+
Verify(Method(mock, func2).Using(20)).Exactly(1);
93+
Verify(Method(mock, func2).Using(50)).Exactly(1);
94+
}
95+
96+
void NoCollidingIdsWhenOverridingMocks()
97+
{
98+
Mock<SomeInterface> mock;
99+
100+
stubFunc(mock);
101+
When(Method(mock, func)).Return(123);
102+
When(Method(mock, func2)).Return("Something");
18103
stubFunc2(mock);
19-
When(Method(mock, func)).Return<int>(1);
20104

21-
mock.get().func2(); // Uncatchable write access violation if ids collide
105+
SomeInterface &i = mock.get();
106+
107+
EXPECT_EQUAL(i.func(0), 123);
108+
EXPECT_EQUAL(i.func2(0), "String");
109+
110+
try {
111+
i.func(0);
112+
FAIL();
113+
} catch (const UnexpectedMethodCallException&) {
114+
// There was only one action in the mock for this function.
115+
}
116+
117+
try {
118+
i.func2(0);
119+
FAIL();
120+
} catch (const UnexpectedMethodCallException&) {
121+
// There was only one action in the mock for this function.
122+
}
123+
124+
Verify(Method(mock, func)).Exactly(2);
125+
Verify(Method(mock, func)).Exactly(2);
22126
}
23127

24128
} __MultipleTranslationUnitsStub;

0 commit comments

Comments
 (0)