Skip to content

Commit b11058f

Browse files
committed
add Call macro
1 parent ba8c1df commit b11058f

File tree

1 file changed

+10
-114
lines changed

1 file changed

+10
-114
lines changed

tests/exampls.cpp

Lines changed: 10 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <string>
1010
#include <queue>
1111
#include <iostream>
12+
#include <utility>
13+
#include <type_traits>
1214

1315
#include "tpunit++.hpp"
1416
#include "fakeit.hpp"
@@ -17,18 +19,15 @@
1719
using namespace fakeit;
1820
using namespace std;
1921
using namespace tpunit;
20-
/*
22+
2123
struct DemoTests
2224
: tpunit::TestFixture
2325
{
2426
DemoTests()
2527
:
2628
tpunit::TestFixture(
2729

28-
TEST(DemoTests::on_other_invocations_verification), //
29-
TEST(DemoTests::simple_inheritance_upcast), //
30-
TEST(DemoTests::simple_inheritance_dynamic_down_cast)
31-
//
30+
TEST(DemoTests::test1) //
3231
)
3332
{
3433
}
@@ -40,122 +39,19 @@ struct DemoTests
4039
virtual void f2(){}
4140
};
4241

43-
class Some2: public Some {
44-
public:
45-
virtual ~Some2(){}
46-
virtual void f3(){}
47-
};
48-
4942
struct SomeInterface {
5043
virtual int foo(int) = 0;
5144
virtual int bar(int, int) = 0;
5245
};
5346

54-
void basic_stubbing() {
55-
}
56-
57-
void basic_verification() {
58-
}//
59-
60-
struct A {
61-
virtual int foo() = 0;
62-
};
63-
64-
struct B : public A {
65-
virtual int foo() override = 0;
66-
};
67-
68-
struct C : public B
69-
{
70-
virtual int foo() override = 0;
71-
};
72-
73-
void simple_inheritance_upcast() {
74-
auto size = VTUtils::getVTSize<Some2>();
75-
Mock<C> cMock;
76-
When(cMock[&C::foo]).AlwaysReturn(0);
77-
78-
C& c = cMock.get();
79-
B& b = c;
80-
A& a = b;
81-
82-
ASSERT_EQUAL(0, c.foo());
83-
ASSERT_EQUAL(0, b.foo());
84-
ASSERT_EQUAL(0, a.foo());
85-
}
86-
87-
void simple_inheritance_dynamic_down_cast() {
88-
Mock<C, B, A> cMock;
89-
When(cMock[&C::foo]).AlwaysReturn(0);
90-
A& a = cMock.get();
9147

92-
B& b = dynamic_cast<B&>(a);
93-
ASSERT_EQUAL(0, b.foo());
48+
#define Call(mock, method) \
49+
mock[&std::remove_reference<decltype(mock.get())>::type::method]
9450

95-
C& c = dynamic_cast<C&>(a);
96-
ASSERT_EQUAL(0, c.foo());
51+
void test1(){
52+
Mock<SomeInterface> m;
53+
When(Call(m,foo)).Return(0);
54+
ASSERT_EQUAL(0,m.get().foo(1));
9755
}
9856

99-
void on_other_invocations_verification() {
100-
Mock<SomeInterface> mock;
101-
// Stub a method to return a value once
102-
When(mock[&SomeInterface::foo]).Return(1);
103-
104-
// Stub multiple return values (The next two lines do exactly the same)
105-
When(mock[&SomeInterface::foo]).Return(1, 2, 3);
106-
When(mock[&SomeInterface::foo]).Return(1).Return(2).Return(3);
107-
108-
// Return the same value many times (56 in this example)
109-
When(mock[&SomeInterface::foo]).Return(Times<56>::of(1));
110-
111-
// Return many values many times (First 100 calls will return 1, next 200 calls will return 2)
112-
When(mock[&SomeInterface::foo]).Return(Times<100>::of(1), Times<200>::of(2));
113-
114-
// Always return a value (The next two lines do exactly the same)
115-
When(mock[&SomeInterface::foo]).AlwaysReturn(1);
116-
mock[&SomeInterface::foo] = 1;
117-
118-
// Throw once
119-
When(mock[&SomeInterface::foo]).Throw(exception());
120-
// Throw several times
121-
When(mock[&SomeInterface::foo]).Throw(exception(), exception());
122-
// Throw many times
123-
When(mock[&SomeInterface::foo]).Throw(Times<23>::of(exception()));
124-
// Always throw
125-
When(mock[&SomeInterface::foo]).AlwaysThrow(exception());
126-
127-
When(mock[&SomeInterface::foo]).AlwaysReturn(1);
128-
129-
SomeInterface & i = mock.get();
130-
131-
// Production code:
132-
i.foo(1);
133-
i.foo(2);
134-
i.foo(3);
135-
136-
// Verify foo was invoked at least once. (The four lines do exactly the same)
137-
Verify(mock[&SomeInterface::foo]);
138-
Verify(mock[&SomeInterface::foo]).AtLeastOnce();
139-
Verify(mock[&SomeInterface::foo]).AtLeast(1);
140-
Verify(mock[&SomeInterface::foo]).AtLeast(Times<1>());
141-
142-
// Verify foo was invoked at exactly 3 times. (The next two lines do exactly the same)
143-
Verify(mock[&SomeInterface::foo]).Exactly(3);
144-
Verify(mock[&SomeInterface::foo]).Exactly(Times<3>());
145-
146-
// Verify foo(1) was invoked exactly once
147-
Verify(mock[&SomeInterface::foo].Using(1)).Once();
148-
Verify(mock[&SomeInterface::foo].Using(1)).Exactly(Once);
149-
150-
// verify the actual invocation sequence contains two consecutive invocations of foo at least once.
151-
Verify(mock[&SomeInterface::foo] * 2);
152-
153-
// verify the actual invocation sequence contains two consecutive invocations of foo exactly once.
154-
Verify(mock[&SomeInterface::foo] * 2).Exactly(Once);
155-
156-
// verify the actual invocation sequence contains an invocation of foo(1) followed by bar(1,2) exactly twice.
157-
Verify(mock[&SomeInterface::foo].Using(1) + mock[&SomeInterface::bar].Using(1,2)).Exactly(Times<2>());
158-
}//
159-
16057
} __DemoTests;
161-
*/

0 commit comments

Comments
 (0)