From b8374bca163387207cea60dc43486a895754c564 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 7 Sep 2015 20:07:59 +0200 Subject: [PATCH] [Issue #36] Added throw assertions --- include/criterion/assert.h | 28 ++++++++++++++++++++++++++++ samples/asserts.cc | 7 +++++++ samples/asserts.cc.bin.err.expected | 10 ++++++---- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/include/criterion/assert.h b/include/criterion/assert.h index 539393d..1d67a00 100644 --- a/include/criterion/assert.h +++ b/include/criterion/assert.h @@ -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 diff --git a/samples/asserts.cc b/samples/asserts.cc index 0d9daab..c7f5c8f 100644 --- a/samples/asserts.cc +++ b/samples/asserts.cc @@ -1,4 +1,6 @@ #include +#include +#include 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); +} diff --git a/samples/asserts.cc.bin.err.expected b/samples/asserts.cc.bin.err.expected index 3b7d7ae..8b27ddd 100644 --- a/samples/asserts.cc.bin.err.expected +++ b/samples/asserts.cc.bin.err.expected @@ -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