-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hamlest.cpp
379 lines (288 loc) · 11.7 KB
/
test_hamlest.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2013 by Martin Moene
//
// 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 "hamlest.hpp"
using lest::test;
using namespace lest::hamlest;
int a() { return 33; }
int b() { return 55; }
int c() { return 77; }
char const *hello() { return "hello"; }
char const *world() { return "world"; }
char const *hello_world() { return "hello world"; }
std::set<int> s{ 1, 2, 3, };
std::set<int> t{ 2, 1, 0, };
std::set<int> u;
const test specification[] =
{
CASE("is decorates properly")
{
EXPECT( true == is(77)(77) );
EXPECT( true == is( equal_to(77)(77) )(true) );
EXPECT( true == is( hello() )( hello() ) );
EXPECT( true == is( equal_to( hello() )( hello() ) )(true) );
EXPECT( false == is(77)(33) );
EXPECT( false == is( equal_to(77)(33) )(true) );
EXPECT( false == is( hello() )( world() ) );
EXPECT( false == is( equal_to( hello() )( world() ) )(true) );
},
CASE("is_not decorates properly")
{
EXPECT( true == is_not(77)(33) );
EXPECT( true == is_not( equal_to(77)(33) )(true) );
EXPECT( true == is_not( hello() )( world() ) );
EXPECT( true == is_not( equal_to( hello() )( world() ) )(true) );
EXPECT( false == is_not(77)(77) );
EXPECT( false == is_not( equal_to(77)(77) )(true) );
EXPECT( false == is_not( hello() )( hello() ) );
EXPECT( false == is_not( equal_to( hello() )( hello() ) )(true) );
},
CASE("expect_that generates no message exception for a succeeding test")
{
test pass[] = {{ CASE("P") { EXPECT_THAT( true, is( true ) ); } }};
std::ostringstream os;
EXPECT( 0 == lest::run( pass, os ) );
},
CASE("expect_that generates a message exception for a failing test")
{
test fail[] = {{ CASE("F") { EXPECT_THAT( false, is( true ) ); } }};
std::ostringstream os;
EXPECT( 0 != lest::run( fail, os ) );
},
CASE("expect_that succeeds for success (match) and failure (non-match)")
{
test pass[] = {{ CASE("P") { EXPECT_THAT( true , is( true ) ); } }};
test fail[] = {{ CASE("F") { EXPECT_THAT( false, is( true ) ); } }};
std::ostringstream os;
EXPECT( 0 == run( pass, os ) );
EXPECT( 1 == run( fail, os ) );
},
CASE("expect_that succeeds with an unexpected standard exception")
{
std::string text = "hello-world";
test pass[] = {{ CASE_ON("P", text) { EXPECT_THAT( (throw std::runtime_error(text), true), is( true ) ); } }};
std::ostringstream os;
EXPECT( 1 == run( pass, os ) );
EXPECT( std::string::npos != os.str().find(text) );
},
CASE("expect_that succeeds with an unexpected non-standard exception")
{
test pass[] = {{ CASE("P") { EXPECT_THAT( (throw 77, true), is( true ) ); } }};
std::ostringstream os;
EXPECT( 1 == run( pass, os ) );
},
// object:
CASE("same_instance matches properly")
{
int x{1}, y{2};
EXPECT( true == same_instance(x)(x) );
EXPECT( false == same_instance(x)(y) );
},
// numeric:
CASE("close_to matches properly")
{
EXPECT( true == close_to(10, 2)( 8) );
EXPECT( true == close_to(10, 2)( 9) );
EXPECT( true == close_to(10, 2)(10) );
EXPECT( true == close_to(10, 2)(11) );
EXPECT( true == close_to(10, 2)(12) );
EXPECT( false == close_to(10, 2)( 6) );
EXPECT( false == close_to(10, 2)( 7) );
EXPECT( false == close_to(10, 2)(13) );
EXPECT( false == close_to(10, 2)(14) );
EXPECT_THAT( a(), close_to( a()-2, 2) );
EXPECT_THAT( a(), close_to( a()+2, 2) );
test fail[] = {{ CASE("F1") { EXPECT_THAT( a(), close_to( a()-3, 2) ); }},
{ CASE("F2") { EXPECT_THAT( a(), close_to( a()+3, 2) ); }} };
std::ostringstream os;
EXPECT( 2 == run( fail, os ) );
},
CASE("equal_to matches properly")
{
EXPECT( true == equal_to(3)(3) );
EXPECT( false == equal_to(3)(4) );
},
CASE("not_equal_to matches properly")
{
EXPECT( true == not_equal_to(3)(4) );
EXPECT( false == not_equal_to(3)(3) );
},
CASE("less_than matches properly")
{
EXPECT( true == less_than(-3)(-4) );
EXPECT( true == less_than( 3)( 2) );
EXPECT( false == less_than(-3)(-3) );
EXPECT( false == less_than(-3)(-2) );
EXPECT( false == less_than( 3)( 3) );
EXPECT( false == less_than( 3)( 4) );
},
CASE("less_equal matches properly")
{
EXPECT( true == less_equal(-3)(-4) );
EXPECT( true == less_equal(-3)(-3) );
EXPECT( true == less_equal( 3)( 2) );
EXPECT( true == less_equal( 3)( 3) );
EXPECT( false == less_equal(-3)(-2) );
EXPECT( false == less_equal( 3)( 4) );
},
CASE("greater_than matches properly")
{
EXPECT( true == greater_than(-3)(-2) );
EXPECT( true == greater_than( 3)( 4) );
EXPECT( false == greater_than(-3)(-3) );
EXPECT( false == greater_than(-3)(-4) );
EXPECT( false == greater_than( 3)( 3) );
EXPECT( false == greater_than( 3)( 2) );
},
CASE("greater_equal matches properly")
{
EXPECT( true == greater_equal(-3)(-3) );
EXPECT( true == greater_equal(-3)(-2) );
EXPECT( true == greater_equal( 3)( 3) );
EXPECT( true == greater_equal( 3)( 4) );
EXPECT( false == greater_equal(-3)(-4) );
EXPECT( false == greater_equal( 3)( 2) );
},
// textual:
CASE("starts_with matches properly")
{
EXPECT( true == starts_with( hello() )( hello_world() ) );
EXPECT( false == starts_with( world() )( hello_world() ) );
},
CASE("ends_with matches properly")
{
EXPECT( true == ends_with( world() )( hello_world() ) );
EXPECT( false == ends_with( hello() )( hello_world() ) );
},
CASE("contains matches properly")
{
EXPECT( true == contains( world() )( hello_world() ) );
EXPECT( true == contains( hello() )( hello_world() ) );
EXPECT( false == contains( "hola" )( hello_world() ) );
},
#if 0 // skip
CASE("contains_regexp matches properly (fails for Clang 3.2 and GNUC 4.8.1)")
{
EXPECT( true == contains_regexp( hello() )( hello_world() ) );
EXPECT( true == contains_regexp( "h.*o" )( hello_world() ) );
EXPECT( false == contains_regexp( "hola" )( hello_world() ) );
},
#endif
CASE("matches_regexp matches properly")
{
EXPECT( true == matches_regexp( hello_world() )( hello_world() ) );
EXPECT( true == matches_regexp( "h.*d" )( hello_world() ) );
EXPECT( false == matches_regexp( "hola" )( hello_world() ) );
},
// sequence:
CASE("set of int is empty")
{
EXPECT( true == is_empty()( u ) );
EXPECT( false == is_empty()( s ) );
EXPECT_THAT( u, is_empty() );
// not yet:
// EXPECT_THAT( s, is_not( is_empty() ) );
},
CASE("size of set match")
{
EXPECT( true == size_is( s.size() )( s ) );
EXPECT( false == size_is( s.size()-1 )( s ) );
EXPECT( false == size_is( s.size()+1 )( s ) );
EXPECT_THAT( s, size_is( 3 ) );
EXPECT_THAT( s, size_is( s.size() ) );
EXPECT_THAT( s, size_is( equal_to( s.size() ) ) );
EXPECT( true == size_is( equal_to( s.size() ) )( s ) );
EXPECT( false == size_is( equal_to( s.size()+1 ) )( s ) );
EXPECT( false == size_is( equal_to( s.size()-1 ) )( s ) );
EXPECT_THAT( s, size_is( equal_to( s.size() ) ) );
},
CASE("set of int contains element")
{
EXPECT( true == contains( 1 )( s ) );
EXPECT( true == contains( 2 )( s ) );
EXPECT( true == contains( 3 )( s ) );
EXPECT( false == contains( 0 )( s ) );
EXPECT( false == contains( 4 )( s ) );
EXPECT_THAT( s, contains( 1 ) );
EXPECT_THAT( s, contains( 2 ) );
EXPECT_THAT( s, contains( 3 ) );
// not yet:
// EXPECT_THAT( s, is_not( contains( 0 ) ) );
// EXPECT_THAT( s, is_not( contains( 4 ) ) );
},
CASE("set of int contains sequence")
{
EXPECT( true == contains( { 1 } )( s ) );
EXPECT( true == contains( { 1, 2 } )( s ) );
EXPECT( true == contains( { 1, 2, 3 } )( s ) );
EXPECT( false == contains( { 0 } )( s ) );
EXPECT( false == contains( { 2, 1 } )( s ) );
EXPECT( false == contains( { 1, 2, 7 } )( s ) );
EXPECT_THAT( s, contains( { 1, 2 } ) );
test fail[] = {{ CASE("F") {
EXPECT_THAT( s, contains( { 2, 1 } ) ); } }};
std::ostringstream os;
EXPECT( 1 == run( fail, os ) );
},
CASE("set of int contains elements")
{
EXPECT( true == contains_elements( { 3 } )( s ) );
EXPECT( true == contains_elements( { 3, 2 } )( s ) );
EXPECT( true == contains_elements( { 3, 2, 1 } )( s ) );
EXPECT( false == contains_elements( { 7 } )( s ) );
EXPECT( false == contains_elements( { 7, 2 } )( s ) );
EXPECT( false == contains_elements( { 7, 2, 1 } )( s ) );
EXPECT_THAT( s, contains_elements( { 3 } ) );
EXPECT_THAT( s, contains_elements( { 3, 2 } ) );
EXPECT_THAT( s, contains_elements( { 3, 2, 1 } ) );
test fail[] = {{ CASE("F") {
EXPECT_THAT( s, contains_elements( { 7, 2, 1 } ) ); } }};
std::ostringstream os;
EXPECT( 1 == run( fail, os ) );
},
// dictionary:
//using lest::has_entries;
//using lest::has_entry;
//using lest::has_key;
//using lest::has_value;
// logical:
CASE("anything matches properly")
{
EXPECT( true == anything<int >( "[desciption]" )( 77 ) );
EXPECT( true == anything<char const*>( "[desciption]" )( "77" ) );
EXPECT( true == anything<std::string>( "[desciption]" )( "77" ) );
},
CASE("all_of matches properly")
{
EXPECT_THAT( a(), all_of( equal_to(a()), equal_to(a()) ) );
EXPECT_THAT( a(), all_of( { a(), a() } ) );
EXPECT_THAT( 'a', all_of( { 'a', 'a' } ) );
test fail_1[] = {{ CASE("F1") { EXPECT_THAT( a(), all_of( equal_to(a()), equal_to(b()) ) ); } }};
test fail_2[] = {{ CASE("F2") { EXPECT_THAT( a(), all_of( { a(), b() } ) ); } }};
test fail_3[] = {{ CASE("F3") { EXPECT_THAT( 'a', all_of( { 'a', 'b' } ) ); } }};
std::ostringstream os;
EXPECT( 1 == run( fail_1, os ) );
EXPECT( 1 == run( fail_2, os ) );
EXPECT( 1 == run( fail_3, os ) );
},
CASE("any_of matches properly")
{
EXPECT_THAT( a(), any_of( equal_to(a()), equal_to(b()) ) );
EXPECT_THAT( a(), any_of( { a(), b() } ) );
EXPECT_THAT( 'a', any_of( { 'a', 'b' } ) );
test fail_1[] = {{ CASE("F1") { EXPECT_THAT( a(), any_of( equal_to(b()), equal_to(c()) ) ); } }};
test fail_2[] = {{ CASE("F2") { EXPECT_THAT( a(), any_of( { b(), b() } ) ); } }};
test fail_3[] = {{ CASE("F3") { EXPECT_THAT( 'a', any_of( { 'b', 'c' } ) ); } }};
std::ostringstream os;
EXPECT( 1 == run( fail_1, os ) );
EXPECT( 1 == run( fail_2, os ) );
EXPECT( 1 == run( fail_3, os ) );
},
};
int main()
{
return lest::run( specification );
}
// cl -nologo -Wall -EHsc test_hamlest.cpp && test_hamlest
// g++ -Wall -Wextra -Weffc++ -std=c++11 -o test_hamlest.exe test_hamlest.cpp && test_hamlest