diff --git a/include/criterion/assert.h b/include/criterion/assert.h index f88842b..c3e0250 100644 --- a/include/criterion/assert.h +++ b/include/criterion/assert.h @@ -53,6 +53,7 @@ struct criterion_assert_args { #endif }; +// Do NOT reorder unless you want to break the ABI enum criterion_assert_messages { CRITERION_ASSERT_MSG_FAIL, CRITERION_ASSERT_MSG_EXPR_FALSE, @@ -62,6 +63,8 @@ enum criterion_assert_messages { CRITERION_ASSERT_MSG_IS_EMPTY, CRITERION_ASSERT_MSG_IS_NOT_EMPTY, CRITERION_ASSERT_MSG_FILE_STR_MATCH, + CRITERION_ASSERT_MSG_THROW, + CRITERION_ASSERT_MSG_NO_THROW, }; CR_BEGIN_C_API @@ -484,36 +487,60 @@ CR_END_C_API # ifdef __cplusplus -# define cr_assert_throw_(Fail, Statement, Exception, ...) \ - try { \ - Statement; \ - } catch (Exception const &) { \ - } catch (...) { CR_EXPAND(cr_fail(Fail, CR_VA_TAIL(__VA_ARGS__))); } +# define cr_assert_throw_abort_(Fail, Msg, Statement, Exception, ...) \ + CR_EXPAND(cr_assert_impl( \ + Fail, \ + 0, \ + dummy, \ + Msg, \ + (CR_STR(Statement), CR_STR(Exception)), \ + CR_VA_TAIL(__VA_ARGS__) \ + )) -# define cr_assert_throw_va_(...) \ - CR_EXPAND(cr_assert_throw_( \ - CR_VA_HEAD(__VA_ARGS__), \ - CR_VA_HEAD(CR_VA_TAIL(__VA_ARGS__)), \ - CR_VA_HEAD(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))), \ - dummy, \ - CR_VA_TAIL(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))) \ +# define cr_assert_throw_(Fail, Statement, Exception, ...) \ + try { \ + Statement; \ + } catch (Exception const &) { \ + } catch (...) { \ + CR_EXPAND(cr_assert_throw_abort_( \ + Fail, \ + CRITERION_ASSERT_MSG_THROW, \ + Statement, \ + Exception, \ + __VA_ARGS__)); \ + } + +# define cr_assert_throw_va_(...) \ + CR_EXPAND(cr_assert_throw_( \ + CR_VA_HEAD(__VA_ARGS__), \ + CR_VA_HEAD(CR_VA_TAIL(__VA_ARGS__)), \ + CR_VA_HEAD(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))), \ + dummy, \ + CR_VA_TAIL(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))) \ )) # define cr_assert_throw(...) CR_EXPAND(cr_assert_throw_va_(CR_FAIL_ABORT_, __VA_ARGS__)) # define cr_expect_throw(...) CR_EXPAND(cr_assert_throw_va_(CR_FAIL_CONTINUES_, __VA_ARGS__)) -# define cr_assert_no_throw_(Fail, Statement, Exception, ...) \ - try { \ - Statement; \ - } catch (Exception const &) { CR_EXPAND(cr_fail(Fail, CR_VA_TAIL(__VA_ARGS__))); } +# define cr_assert_no_throw_(Fail, Statement, Exception, ...) \ + try { \ + Statement; \ + } catch (Exception const &) { \ + CR_EXPAND(cr_assert_throw_abort_( \ + Fail, \ + CRITERION_ASSERT_MSG_NO_THROW, \ + Statement, \ + Exception, \ + __VA_ARGS__)); \ + } -# define cr_assert_no_throw_va_(...) \ - CR_EXPAND(cr_assert_no_throw_( \ - CR_VA_HEAD(__VA_ARGS__), \ - CR_VA_HEAD(CR_VA_TAIL(__VA_ARGS__)), \ - CR_VA_HEAD(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))), \ - dummy, \ - CR_VA_TAIL(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))) \ +# define cr_assert_no_throw_va_(...) \ + CR_EXPAND(cr_assert_no_throw_( \ + CR_VA_HEAD(__VA_ARGS__), \ + CR_VA_HEAD(CR_VA_TAIL(__VA_ARGS__)), \ + CR_VA_HEAD(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))), \ + dummy, \ + CR_VA_TAIL(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))) \ )) # define cr_assert_no_throw(...) CR_EXPAND(cr_assert_no_throw_va_(CR_FAIL_ABORT_, __VA_ARGS__)) diff --git a/po/fr.po b/po/fr.po index a59b066..3df7e37 100644 --- a/po/fr.po +++ b/po/fr.po @@ -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-13 14:51+0200\n" +"POT-Creation-Date: 2015-09-14 02:01+0200\n" "PO-Revision-Date: 2015-04-03 17:58+0200\n" "Last-Translator: \n" "Language-Team: French\n" @@ -143,9 +143,19 @@ msgstr "%s est vide." msgid "%s is not empty." msgstr "%s n'est pas vide." -#: src/i18n.c:22 +#: src/i18n.c:21 #, 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 +#, 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 +#, 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`." diff --git a/samples/tests/CMakeLists.txt b/samples/tests/CMakeLists.txt index d5b4da5..463d4da 100644 --- a/samples/tests/CMakeLists.txt +++ b/samples/tests/CMakeLists.txt @@ -4,6 +4,7 @@ set(SAMPLES long-messages.c other-crashes.c + failmessages.cc exit.cc long-messages.cc other-crashes.cc diff --git a/samples/tests/failmessages.cc b/samples/tests/failmessages.cc new file mode 100644 index 0000000..c39a769 --- /dev/null +++ b/samples/tests/failmessages.cc @@ -0,0 +1,56 @@ +#include +#include + +Test(messages, default) { + cr_expect(0); + cr_expect_eq(0, 1); + cr_expect_neq(1, 1); + cr_expect_lt(2, 1); + cr_expect_leq(2, 1); + cr_expect_gt(1, 2); + cr_expect_geq(1, 2); + cr_expect_null(""); + cr_expect_not_null(NULL); + + cr_expect_float_eq(1, 2, 0.1); + cr_expect_float_neq(2, 2, 0.1); + + cr_expect_str_empty("foo"); + cr_expect_str_not_empty(""); + cr_expect_str_eq("abc", "abd"); + cr_expect_str_neq("abc", "abc"); + cr_expect_str_lt("abc", "aba"); + cr_expect_str_leq("abc", "aba"); + cr_expect_str_gt("abc", "abd"); + cr_expect_str_geq("abc", "abd"); + + cr_expect_throw(throw std::exception(), std::bad_alloc); + cr_expect_no_throw(throw std::exception(), std::exception); +} + +Test(messages, user) { + cr_expect(0, "foo %s", "bar"); + cr_expect_eq(0, 1, "foo %s", "bar"); + cr_expect_neq(1, 1, "foo %s", "bar"); + cr_expect_lt(2, 1, "foo %s", "bar"); + cr_expect_leq(2, 1, "foo %s", "bar"); + cr_expect_gt(1, 2, "foo %s", "bar"); + cr_expect_geq(1, 2, "foo %s", "bar"); + cr_expect_null("", "foo %s", "bar"); + cr_expect_not_null(NULL, "foo %s", "bar"); + + cr_expect_float_eq(1, 2, 0.1, "foo %s", "bar"); + cr_expect_float_neq(2, 2, 0.1, "foo %s", "bar"); + + cr_expect_str_empty("foo", "foo %s", "bar"); + cr_expect_str_not_empty("", "foo %s", "bar"); + cr_expect_str_eq("abc", "abd", "foo %s", "bar"); + cr_expect_str_neq("abc", "abc", "foo %s", "bar"); + cr_expect_str_lt("abc", "aba", "foo %s", "bar"); + cr_expect_str_leq("abc", "aba", "foo %s", "bar"); + cr_expect_str_gt("abc", "abd", "foo %s", "bar"); + cr_expect_str_geq("abc", "abd", "foo %s", "bar"); + + cr_expect_throw(throw std::exception(), std::bad_alloc, "foo %s", "bar"); + cr_expect_no_throw(throw std::exception(), std::exception, "foo %s", "bar"); +} diff --git a/src/i18n.c b/src/i18n.c index fb6e7d7..3178064 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -17,12 +17,16 @@ 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."), - #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."), + [CRITERION_ASSERT_MSG_NO_THROW] = N_("The statement `%1$s` did not throw an instance of the `%2$s` exception."), #else [CRITERION_ASSERT_MSG_FILE_STR_MATCH] = "The file contents of %s does not match the string \"%s\".", + [CRITERION_ASSERT_MSG_THROW] = "The statement `%s` did throw an instance of the `%s` exception.", + [CRITERION_ASSERT_MSG_NO_THROW] = "The statement `%s` did not throw an instance of the `%s` exception.", #endif + }; va_list vl;