Skip to content

Commit

Permalink
Added any_throw and none_throw assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Snaipe committed Sep 14, 2015
1 parent d427186 commit 5e8491a
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 12 deletions.
59 changes: 51 additions & 8 deletions include/criterion/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ enum criterion_assert_messages {
CRITERION_ASSERT_MSG_FILE_STR_MATCH,
CRITERION_ASSERT_MSG_THROW,
CRITERION_ASSERT_MSG_NO_THROW,
CRITERION_ASSERT_MSG_ANY_THROW,
CRITERION_ASSERT_MSG_NONE_THROW,
};

CR_BEGIN_C_API
Expand Down Expand Up @@ -487,13 +489,13 @@ CR_END_C_API

# ifdef __cplusplus

# define cr_assert_throw_abort_(Fail, Msg, Statement, Exception, ...) \
# define cr_assert_throw_abort_(Fail, Msg, MsgArgs, ...) \
CR_EXPAND(cr_assert_impl( \
Fail, \
0, \
dummy, \
Msg, \
(CR_STR(Statement), CR_STR(Exception)), \
MsgArgs, \
CR_VA_TAIL(__VA_ARGS__) \
))

Expand All @@ -504,9 +506,8 @@ CR_END_C_API
} catch (...) { \
CR_EXPAND(cr_assert_throw_abort_( \
Fail, \
CRITERION_ASSERT_MSG_THROW, \
Statement, \
Exception, \
CRITERION_ASSERT_MSG_NO_THROW, \
(CR_STR(Statement), CR_STR(Exception)), \
__VA_ARGS__)); \
}

Expand All @@ -528,9 +529,8 @@ CR_END_C_API
} catch (Exception const &) { \
CR_EXPAND(cr_assert_throw_abort_( \
Fail, \
CRITERION_ASSERT_MSG_NO_THROW, \
Statement, \
Exception, \
CRITERION_ASSERT_MSG_THROW, \
(CR_STR(Statement), CR_STR(Exception)), \
__VA_ARGS__)); \
}

Expand All @@ -546,6 +546,49 @@ CR_END_C_API
# define cr_assert_no_throw(...) CR_EXPAND(cr_assert_no_throw_va_(CR_FAIL_ABORT_, __VA_ARGS__))
# define cr_expect_no_throw(...) CR_EXPAND(cr_assert_no_throw_va_(CR_FAIL_CONTINUES_, __VA_ARGS__))

# define cr_assert_any_throw_(Fail, Statement, ...) \
try { \
Statement; \
CR_EXPAND(cr_assert_throw_abort_( \
Fail, \
CRITERION_ASSERT_MSG_ANY_THROW, \
(CR_STR(Statement)), \
__VA_ARGS__)); \
} catch (...) {}

# define cr_assert_any_throw_va_(...) \
CR_EXPAND(cr_assert_any_throw_( \
CR_VA_HEAD(__VA_ARGS__), \
CR_VA_HEAD(CR_VA_TAIL(__VA_ARGS__)), \
dummy, \
CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__)) \
))

# define cr_assert_any_throw(...) CR_EXPAND(cr_assert_any_throw_va_(CR_FAIL_ABORT_, __VA_ARGS__))
# define cr_expect_any_throw(...) CR_EXPAND(cr_assert_any_throw_va_(CR_FAIL_CONTINUES_, __VA_ARGS__))

# define cr_assert_none_throw_(Fail, Statement, ...) \
try { \
Statement; \
} catch (...) { \
CR_EXPAND(cr_assert_throw_abort_( \
Fail, \
CRITERION_ASSERT_MSG_NONE_THROW, \
(CR_STR(Statement)), \
__VA_ARGS__)); \
}

# define cr_assert_none_throw_va_(...) \
CR_EXPAND(cr_assert_none_throw_( \
CR_VA_HEAD(__VA_ARGS__), \
CR_VA_HEAD(CR_VA_TAIL(__VA_ARGS__)), \
dummy, \
CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__)) \
))

# define cr_assert_none_throw(...) CR_EXPAND(cr_assert_none_throw_va_(CR_FAIL_ABORT_, __VA_ARGS__))
# define cr_expect_none_throw(...) CR_EXPAND(cr_assert_none_throw_va_(CR_FAIL_CONTINUES_, __VA_ARGS__))

# endif

// The section below is here for backward compatibility purposes.
Expand Down
16 changes: 13 additions & 3 deletions po/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: criterion 1.0.0\n"
"Report-Msgid-Bugs-To: franklinmathieu+criterion@gmail.com\n"
"POT-Creation-Date: 2015-09-14 02:01+0200\n"
"POT-Creation-Date: 2015-09-14 02:33+0200\n"
"PO-Revision-Date: 2015-04-03 17:58+0200\n"
"Last-Translator: <franklinmathieu@gmail.com>\n"
"Language-Team: French\n"
Expand Down Expand Up @@ -143,19 +143,29 @@ msgstr "%s est vide."
msgid "%s is not empty."
msgstr "%s n'est pas vide."

#: src/i18n.c:20
#, fuzzy, c-format
msgid "The statement `%s` did not throw any exception."
msgstr "L'instruction `%s` n'a pas levé d'exception."

#: src/i18n.c:21
#, fuzzy, c-format
msgid "The statement `%s` threw some exception."
msgstr "L'instruction `%1$s` a levé une exception."

#: src/i18n.c:24
#, c-format
msgid "The file contents of %1$s does not match the string \"%2$s\"."
msgstr ""
"Le contenu du fichier %1$s ne correspond pas à la chaine de caractères \"%2$s"
"\"."

#: src/i18n.c:22
#: src/i18n.c:25
#, c-format
msgid "The statement `%1$s` did throw an instance of the `%2$s` exception."
msgstr "L'instruction `%1$s` a levé une instance de l'exception `%2$s`."

#: src/i18n.c:23
#: src/i18n.c:26
#, c-format
msgid "The statement `%1$s` did not throw an instance of the `%2$s` exception."
msgstr "L'instruction `%1$s` n'a pas levé d'instance de l'exception `%2$s`."
2 changes: 1 addition & 1 deletion samples/outputs/asserts.cc.bin.err.expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[----] asserts.cc:13: Assertion failed: assert is fatal, expect isn't
[----] asserts.cc:14: Assertion failed: This assert runs
[FAIL] asserts::base: (0.00s)
[----] asserts.cc:89: Assertion failed: The conditions for this assertion were not met.
[----] asserts.cc:89: Assertion failed: The statement `throw std::exception()` did throw an instance of the `std::bad_alloc` exception.
[FAIL] asserts::exception: (0.00s)
[----] asserts.cc:19: Assertion failed: You can fail an assertion with a message from anywhere
[----] asserts.cc:20: Assertion failed: The conditions for this assertion were not met.
Expand Down
4 changes: 4 additions & 0 deletions samples/tests/failmessages.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Test(messages, default) {

cr_expect_throw(throw std::exception(), std::bad_alloc);
cr_expect_no_throw(throw std::exception(), std::exception);
cr_expect_any_throw({});
cr_expect_none_throw(throw std::exception());
}

Test(messages, user) {
Expand Down Expand Up @@ -53,4 +55,6 @@ Test(messages, user) {

cr_expect_throw(throw std::exception(), std::bad_alloc, "foo %s", "bar");
cr_expect_no_throw(throw std::exception(), std::exception, "foo %s", "bar");
cr_expect_any_throw({}, "foo %s", "bar");
cr_expect_none_throw(throw std::exception(), "foo %s", "bar");
}
49 changes: 49 additions & 0 deletions samples/tests/outputs/failmessages.cc.bin.err.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[----] failmessages.cc:5: Assertion failed: The expression 0 is false.
[----] failmessages.cc:6: Assertion failed: The expression (0) == (1) is false.
[----] failmessages.cc:7: Assertion failed: The expression (1) != (1) is false.
[----] failmessages.cc:8: Assertion failed: The expression (2) < (1) is false.
[----] failmessages.cc:9: Assertion failed: The expression (2) <= (1) is false.
[----] failmessages.cc:10: Assertion failed: The expression (1) > (2) is false.
[----] failmessages.cc:11: Assertion failed: The expression (1) >= (2) is false.
[----] failmessages.cc:12: Assertion failed: "" is not null.
[----] failmessages.cc:13: Assertion failed: __null is null.
[----] failmessages.cc:15: Assertion failed: The expression (2) - (1) <= (0.1) && (1) - (2) <= (0.1) is false.
[----] failmessages.cc:16: Assertion failed: The expression (2) - (2) > (0.1) || (2) - (2) > (0.1) is false.
[----] failmessages.cc:18: Assertion failed: "foo" is not empty.
[----] failmessages.cc:19: Assertion failed: "" is empty.
[----] failmessages.cc:20: Assertion failed: The expression (as strings) ("abc") == ("abd") is false.
[----] failmessages.cc:21: Assertion failed: The expression (as strings) ("abc") != ("abc") is false.
[----] failmessages.cc:22: Assertion failed: The expression (as strings) ("abc") < ("aba") is false.
[----] failmessages.cc:23: Assertion failed: The expression (as strings) ("abc") <= ("aba") is false.
[----] failmessages.cc:24: Assertion failed: The expression (as strings) ("abc") > ("abd") is false.
[----] failmessages.cc:25: Assertion failed: The expression (as strings) ("abc") >= ("abd") is false.
[----] failmessages.cc:27: Assertion failed: The statement `throw std::exception()` did not throw an instance of the `std::bad_alloc` exception.
[----] failmessages.cc:28: Assertion failed: The statement `throw std::exception()` did throw an instance of the `std::exception` exception.
[----] failmessages.cc:29: Assertion failed: The statement `{}` did not throw any exception.
[----] failmessages.cc:30: Assertion failed: The statement `throw std::exception()` threw some exception.
[FAIL] messages::default: (0.00s)
[----] failmessages.cc:34: Assertion failed: foo bar
[----] failmessages.cc:35: Assertion failed: foo bar
[----] failmessages.cc:36: Assertion failed: foo bar
[----] failmessages.cc:37: Assertion failed: foo bar
[----] failmessages.cc:38: Assertion failed: foo bar
[----] failmessages.cc:39: Assertion failed: foo bar
[----] failmessages.cc:40: Assertion failed: foo bar
[----] failmessages.cc:41: Assertion failed: foo bar
[----] failmessages.cc:42: Assertion failed: foo bar
[----] failmessages.cc:44: Assertion failed: foo bar
[----] failmessages.cc:45: Assertion failed: foo bar
[----] failmessages.cc:47: Assertion failed: foo bar
[----] failmessages.cc:48: Assertion failed: foo bar
[----] failmessages.cc:49: Assertion failed: foo bar
[----] failmessages.cc:50: Assertion failed: foo bar
[----] failmessages.cc:51: Assertion failed: foo bar
[----] failmessages.cc:52: Assertion failed: foo bar
[----] failmessages.cc:53: Assertion failed: foo bar
[----] failmessages.cc:54: Assertion failed: foo bar
[----] failmessages.cc:56: Assertion failed: foo bar
[----] failmessages.cc:57: Assertion failed: foo bar
[----] failmessages.cc:58: Assertion failed: foo bar
[----] failmessages.cc:59: Assertion failed: foo bar
[FAIL] messages::user: (0.00s)
[====] Synthesis: Tested: 2 | Passing: 0 | Failing: 2 | Crashing: 0 
Empty file.
3 changes: 3 additions & 0 deletions src/i18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ char *translate_assert_msg(int msg_index, ...) {
[CRITERION_ASSERT_MSG_IS_NOT_NULL] = N_("%s is not null."),
[CRITERION_ASSERT_MSG_IS_EMPTY] = N_("%s is empty."),
[CRITERION_ASSERT_MSG_IS_NOT_EMPTY] = N_("%s is not empty."),
[CRITERION_ASSERT_MSG_ANY_THROW] = N_("The statement `%s` did not throw any exception."),
[CRITERION_ASSERT_MSG_NONE_THROW] = N_("The statement `%s` threw some exception."),

#ifdef ENABLE_NLS
[CRITERION_ASSERT_MSG_FILE_STR_MATCH] = N_("The file contents of %1$s does not match the string \"%2$s\"."),
[CRITERION_ASSERT_MSG_THROW] = N_("The statement `%1$s` did throw an instance of the `%2$s` exception."),
Expand Down

0 comments on commit 5e8491a

Please sign in to comment.