forked from eranpeer/FakeIt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp14_tests.cpp
More file actions
92 lines (75 loc) · 2.4 KB
/
cpp14_tests.cpp
File metadata and controls
92 lines (75 loc) · 2.4 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
/*
* Copyright (c) 2014 Eran Pe'er.
*
* This program is made available under the terms of the MIT License.
*
* Created on Mar 10, 2014
*/
#if __cplusplus > 201103L
#include <string>
#include "tpunit++.hpp"
#include "fakeit.hpp"
using namespace fakeit;
struct Cpp14Tests : tpunit::TestFixture {
Cpp14Tests() :
tpunit::TestFixture(
//
TEST(Cpp14Tests::use_cpp14_lambda_in_do),
TEST(Cpp14Tests::use_cpp14_lambda_in_invocation_matching),
TEST(Cpp14Tests::use_cpp14_lambda_in_verifing),
TEST(Cpp14Tests::assingOutParamsWithLambdaCpp14)
)//
{
}
struct SomeInterface {
virtual int foo(int a, std::string bar) = 0;
virtual void bar(int a, std::string &bar) = 0;
};
void use_cpp14_lambda_in_do() {
Mock<SomeInterface> mock;
When(Method(mock,foo)).Do([](auto a, auto) {return a;});
ASSERT_EQUAL(1, mock().foo(1,""));
When(Method(mock,foo)).Do([](auto& a, auto&) {return a;});
ASSERT_EQUAL(2, mock().foo(2,""));
}
void use_cpp14_lambda_in_invocation_matching() {
Mock<SomeInterface> mock;
When(Method(mock,foo).Matching([](auto , auto b) {return b == "A";})).Return(1);
When(Method(mock,foo).Matching([](auto , auto &b) {return b == "B";})).Return(2);
When(Method(mock,foo).Matching([](auto const , auto const & b) {return b == "C";})).Return(3);
ASSERT_EQUAL(1, mock().foo(0,"A"));
ASSERT_EQUAL(2, mock().foo(0,"B"));
ASSERT_EQUAL(3, mock().foo(0,"C"));
}
void use_cpp14_lambda_in_verifing() {
Mock<SomeInterface> mock;
When(Method(mock,foo)).AlwaysReturn(1);
mock().foo(1,"A");
mock().foo(2,"B");
Verify(Method(mock,foo).Matching([](auto , auto b) {return b == "A";}));
Verify(Method(mock,foo).Matching([](auto &, auto &b) {return b == "A";}));
Verify(Method(mock,foo).Matching([](const auto , const auto b) {return b == "A";}));
ASSERT_THROW(
Verify(
Method(mock,foo).Matching(
[](auto , auto b) {
return b == "C";
}
)
), fakeit::VerificationException);
}
void assingOutParamsWithLambdaCpp14(){
struct ApiInterface {
virtual bool apiMethod(int a, int b, int& result) = 0;
};
Mock<ApiInterface> mock;
When(Method(mock, apiMethod)).AlwaysDo([](auto a, auto b, auto& result) {
result = a + b;
return true;
});
int result;
ASSERT_TRUE(mock.get().apiMethod(1, 2, result));
ASSERT_EQUAL(3, result);
}
}__Cpp14Tests;
#endif