Skip to content

Commit 6dcd174

Browse files
committed
Use variadic macros for tests
1 parent 357cf58 commit 6dcd174

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

src/test/json/create.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ namespace tao
373373

374374
// TODO: Add more tests
375375

376-
TEST_THROWS(( value { { "foo", 1 }, { "foo", 2 } } ));
376+
TEST_THROWS( value { { "foo", 1 }, { "foo", 2 } } );
377377
}
378378

379379
void unit_test()

src/test/json/test_assert.hh

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,57 @@
1-
// Copyright (c) 2015 Dr. Colin Hirsch and Daniel Frey
1+
// Copyright (c) 2015-2016 Dr. Colin Hirsch and Daniel Frey
22
// Please see LICENSE for license or visit https://github.com/taocpp/json/
33

44
#ifndef TAOCPP_JSON_INCLUDE_SRC_TEST_TEST_ASSERT_HH
55
#define TAOCPP_JSON_INCLUDE_SRC_TEST_TEST_ASSERT_HH
66

77
#include <iostream>
8+
#include <stdexcept>
89

9-
#define TEST_ASSERT( eXPReSSioN ) \
10+
#define TEST_ASSERT( ... ) \
1011
do { \
11-
if ( ! ( eXPReSSioN ) ) { \
12-
std::cerr << "json: unit test assert [ " \
13-
<< ( # eXPReSSioN ) \
14-
<< " ] failed in line [ " \
15-
<< __LINE__ \
16-
<< " ] file [ " \
17-
<< __FILE__ << " ]" \
18-
<< std::endl; \
19-
++failed; \
20-
} } while ( 0 )
12+
try { \
13+
if ( ! ( __VA_ARGS__ ) ) { \
14+
std::cerr << "json: unit test assert [ " \
15+
<< ( # __VA_ARGS__ ) \
16+
<< " ] failed in line [ " \
17+
<< __LINE__ \
18+
<< " ] file [ " \
19+
<< __FILE__ << " ]" \
20+
<< std::endl; \
21+
++failed; \
22+
} \
23+
} \
24+
catch ( const std::exception & e ) { \
25+
std::cerr << "json: unit test [ " \
26+
<< ( # __VA_ARGS__ ) \
27+
<< " ] threw an exception [ " \
28+
<< e.what() \
29+
<< " ] in line [ " \
30+
<< __LINE__ \
31+
<< " ] file [ " \
32+
<< __FILE__ << " ]" \
33+
<< std::endl; \
34+
++failed; \
35+
} \
36+
catch ( ... ) { \
37+
std::cerr << "json: unit test [ " \
38+
<< ( # __VA_ARGS__ ) \
39+
<< " ] threw an unknown exception" \
40+
<< "in line [ " \
41+
<< __LINE__ \
42+
<< " ] file [ " \
43+
<< __FILE__ << " ]" \
44+
<< std::endl; \
45+
++failed; \
46+
} \
47+
} while ( false )
2148

22-
#define TEST_THROWS( STaTeMeNT ) \
49+
#define TEST_THROWS( ... ) \
2350
do { \
2451
try { \
25-
STaTeMeNT; \
26-
std::cerr << "json: unit test assert [ " \
27-
<< ( # STaTeMeNT ) \
52+
__VA_ARGS__; \
53+
std::cerr << "json: unit test [ " \
54+
<< ( # __VA_ARGS__ ) \
2855
<< " ] did not throw in line [ " \
2956
<< __LINE__ \
3057
<< " ] file [ " \
@@ -34,6 +61,6 @@
3461
} \
3562
catch ( ... ) { \
3663
} \
37-
} while ( 0 )
64+
} while ( false )
3865

3966
#endif

0 commit comments

Comments
 (0)