[Issue #36] Added throw assertions

This commit is contained in:
Snaipe 2015-09-07 20:07:59 +02:00
parent afad78c433
commit b8374bca16
3 changed files with 41 additions and 4 deletions

View file

@ -288,6 +288,34 @@ struct criterion_assert_args {
cr_expect_arrays_eq_cmp_(__VA_ARGS__, CR_SENTINEL)
# endif /* !__GNUC__ */
# ifdef __cplusplus
# define cr_assert_throw(...) CR_EXPAND(cr_assert_throw_(__VA_ARGS__, CR_SENTINEL))
# define cr_assert_throw_(Statement, Exception, ...) \
try { \
Statement; \
} catch (Exception &ex) { \
} catch (...) { CR_EXPAND(cr_assert_impl(FATAL, 0, __VA_ARGS__)); }
# define cr_assert_no_throw(...) CR_EXPAND(cr_assert_not_throw_(__VA_ARGS__, CR_SENTINEL))
# define cr_assert_no_throw_(Statement, Exception, ...) \
try { \
Statement; \
} catch (Exception &ex) { CR_EXPAND(cr_assert_impl(FATAL, 0, __VA_ARGS__)); }
# define cr_expect_throw(...) CR_EXPAND(cr_expect_throw_(__VA_ARGS__, CR_SENTINEL))
# define cr_expect_throw_(Statement, Exception, ...) \
try { \
Statement; \
} catch (Exception &ex) { \
} catch (...) { CR_EXPAND(cr_assert_impl(NORMAL, 0, __VA_ARGS__)); }
# define cr_expect_no_throw(...) CR_EXPAND(cr_expect_not_throw_(__VA_ARGS__, CR_SENTINEL))
# define cr_expect_no_throw_(Statement, Exception, ...) \
try { \
Statement; \
} catch (Exception &ex) { CR_EXPAND(cr_assert_impl(NORMAL, 0, __VA_ARGS__)); }
# endif
// The section below is here for backward compatibility purposes.
// It shall be removed in the text major version of Criterion
# ifndef CRITERION_NO_COMPAT

View file

@ -1,4 +1,6 @@
#include <criterion/criterion.h>
#include <exception>
#include <new>
Test(asserts, base) {
cr_assert(true);
@ -78,3 +80,8 @@ Test(asserts, array) {
cr_assert_arrays_eq_cmp(s1, s2, 2, eq_dummy);
#endif
}
Test(asserts, exception) {
cr_assert_throw(throw std::exception(), std::exception);
cr_assert_throw(throw std::exception(), std::bad_alloc);
}

View file

@ -1,6 +1,8 @@
[----] asserts.cc:9: Assertion failed: assert is fatal, expect isn't
[----] asserts.cc:10: Assertion failed: This assert runs
[----] asserts.cc:11: Assertion failed: assert is fatal, expect isn't
[----] asserts.cc:12: Assertion failed: This assert runs
[FAIL] asserts::base: (0.00s)
[----] asserts.cc:18: Assertion failed: The conditions for this test were not met.
[----] asserts.cc:86: Assertion failed: 0
[FAIL] asserts::exception: (0.00s)
[----] asserts.cc:20: Assertion failed: The conditions for this test were not met.
[FAIL] asserts::old_school: (0.00s)
[====] Synthesis: Tested: 6 | Passing: 4 | Failing: 2 | Crashing: 0 
[====] Synthesis: Tested: 7 | Passing: 4 | Failing: 3 | Crashing: 0