-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints.hpp
226 lines (211 loc) · 6.7 KB
/
constraints.hpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// Copyright Mathieu Champlon 2008
//
// 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)
//
#ifndef MOCK_CONSTRAINTS_HPP_INCLUDED
#define MOCK_CONSTRAINTS_HPP_INCLUDED
#include "constraint.hpp"
#include "log.hpp"
#include <boost/ref.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/preprocessor/stringize.hpp>
namespace mock
{
#define MOCK_CONSTRAINT(N,Expr) \
namespace detail \
{ \
struct N \
{ \
template< typename Actual > \
bool operator()( const Actual& actual ) const \
{ \
return Expr; \
} \
friend std::ostream& operator<<( std::ostream& s, const N& ) \
{ \
return s << BOOST_STRINGIZE(N); \
} \
}; \
} \
template<> \
struct constraint< detail::N > \
{ \
constraint() \
{} \
detail::N f_; \
}; \
const constraint< detail::N > N;
MOCK_CONSTRAINT(any, true && &actual)
MOCK_CONSTRAINT(affirm, !! actual)
MOCK_CONSTRAINT(negate, ! actual)
MOCK_CONSTRAINT(evaluate, actual())
#undef MOCK_CONSTRAINT
#define MOCK_CONSTRAINT(N,Expr) \
namespace detail \
{ \
template< typename Expected > \
struct N \
{ \
explicit N( const Expected& expected ) \
: expected_( expected ) \
{} \
template< typename Actual > \
bool operator()( const Actual& actual ) const \
{ \
return Expr; \
} \
friend std::ostream& operator<<( std::ostream& s, const N& n ) \
{ \
return s << BOOST_STRINGIZE(N) << "( " << mock::format( n.expected_ ) << " )"; \
} \
Expected expected_; \
}; \
} \
template< typename T > \
constraint< detail::N< T > > N( T t ) \
{ \
return detail::N< T >( t ); \
}
MOCK_CONSTRAINT(equal, actual == boost::unwrap_ref( expected_ ))
MOCK_CONSTRAINT(less, actual < boost::unwrap_ref( expected_ ))
MOCK_CONSTRAINT(greater, actual > boost::unwrap_ref( expected_ ))
MOCK_CONSTRAINT(less_equal, actual <= boost::unwrap_ref( expected_ ))
MOCK_CONSTRAINT(greater_equal, actual >= boost::unwrap_ref( expected_ ))
#undef MOCK_CONSTRAINT
namespace detail
{
template< typename Expected >
struct same
{
explicit same( const Expected& expected )
: expected_( &boost::unwrap_ref( expected ) )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return &actual == expected_;
}
friend std::ostream& operator<<( std::ostream& os, const same& s )
{
return os << "same( " << mock::format( *s.expected_ ) << " )";
}
const BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type* expected_;
};
template< typename Expected >
struct assign
{
explicit assign( const Expected& expected )
: expected_( expected )
{}
template< typename Actual >
bool operator()( Actual& actual ) const
{
actual = boost::unwrap_ref( expected_ );
return true;
}
template< typename Actual >
bool operator()( Actual* actual,
BOOST_DEDUCED_TYPENAME boost::enable_if<
boost::is_convertible<
BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type,
Actual
>
>::type* = 0 ) const
{
*actual = boost::unwrap_ref( expected_ );
return true;
}
friend std::ostream& operator<<( std::ostream& s, const assign& a )
{
return s << "assign( " << mock::format( a.expected_ ) << " )";
}
Expected expected_;
};
template< typename Expected >
struct retrieve
{
explicit retrieve( Expected& expected )
: expected_( &boost::unwrap_ref( expected ) )
{}
template< typename Actual >
bool operator()( const Actual& actual,
BOOST_DEDUCED_TYPENAME boost::disable_if<
boost::is_convertible<
const Actual*,
BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type
>
>::type* = 0 ) const
{
*expected_ = actual;
return true;
}
template< typename Actual >
bool operator()( Actual& actual,
BOOST_DEDUCED_TYPENAME boost::enable_if<
boost::is_convertible< Actual*,
BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type
>
>::type* = 0 ) const
{
*expected_ = &actual;
return true;
}
friend std::ostream& operator<<( std::ostream& s, const retrieve& r )
{
return s << "retrieve( " << mock::format( *r.expected_ ) << " )";
}
BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type* expected_;
};
template< typename Expected >
struct contain
{
explicit contain( const Expected& expected )
: expected_( expected )
{}
bool operator()( const std::string& actual ) const
{
return actual.find( boost::unwrap_ref( expected_ ) ) != std::string::npos;
}
friend std::ostream& operator<<( std::ostream& s, const contain& n )
{
return s << "contain ( " << mock::format( n.expected_ ) << " )";
}
Expected expected_;
};
}
template< typename T >
constraint< detail::same< T > > same( T& t )
{
return detail::same< T >( t );
}
template< typename T >
constraint< detail::retrieve< T > > retrieve( T& t )
{
return detail::retrieve< T >( t );
}
template< typename T >
constraint< detail::assign< T > > assign( T t )
{
return detail::assign< T >( t );
}
template< typename T >
constraint< detail::contain< T > > contain( T t )
{
return detail::contain< T >( t );
}
template< typename T >
constraint< T > call( T t )
{
return t;
}
}
#endif // MOCK_CONSTRAINTS_HPP_INCLUDED