From 430ac2be095c8fc9ff84c09ca50bcbbc2ab2eaa6 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Tue, 14 Nov 2023 17:03:42 +0100 Subject: [PATCH] Add _TEST_NO_EXCEPTION macro --- test/expr_arithmetiques.cpp | 15 ++++++++++++--- test/include/test.h | 10 ++++++++++ test/variables.cpp | 10 ++++++++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/test/expr_arithmetiques.cpp b/test/expr_arithmetiques.cpp index 9730112..3a561c2 100644 --- a/test/expr_arithmetiques.cpp +++ b/test/expr_arithmetiques.cpp @@ -15,11 +15,20 @@ int execute(string s) { int main() { _TEST_PRESENTATION("Expressions Arithmétiques"); - _TEST_ASSERT(execute("10 + 3 * (7 - 2);") == 25, "Priorités"); + _TEST_ASSERT( + _TEST_NO_EXCEPTION(execute("10 + 3 * (7 - 2);") == 25), + "Priorités" + ); - _TEST_ASSERT(execute("12 - 4 - 20;") == -12, "Ordre de soustraction"); + _TEST_ASSERT( + _TEST_NO_EXCEPTION(execute("12 - 4 - 20;") == -12), + "Ordre de soustraction" + ); - _TEST_ASSERT(execute("-7 + 13 + -6;") == 0, "Opérateurs unaires"); + _TEST_ASSERT( + _TEST_NO_EXCEPTION(execute("-7 + 13 + -6;") == 0), + "Opérateurs unaires" + ); return 0; } \ No newline at end of file diff --git a/test/include/test.h b/test/include/test.h index c079f4d..a9756cb 100644 --- a/test/include/test.h +++ b/test/include/test.h @@ -19,5 +19,15 @@ } +#define _TEST_NO_EXCEPTION(expr) \ + [&]() -> bool { \ + try { \ + bool result = (expr); \ + return result; \ + } catch (...) { \ + return false; \ + } \ + }() + #endif \ No newline at end of file diff --git a/test/variables.cpp b/test/variables.cpp index f4e2ba4..cbf7be2 100644 --- a/test/variables.cpp +++ b/test/variables.cpp @@ -15,9 +15,15 @@ int execute(string s) { int main() { _TEST_PRESENTATION("Variables"); - _TEST_ASSERT(execute("int uneVariableFarfelue__ = 12;") == 12, "Déclaration avec assignement"); + _TEST_ASSERT( + _TEST_NO_EXCEPTION(execute("int uneVariableFarfelue__ = 12;") == 12), + "Déclaration avec assignement" + ); - _TEST_ASSERT(execute("int x = 5; x + 3;") == 8, "Déclaration puis assignement"); + _TEST_ASSERT( + _TEST_NO_EXCEPTION(execute("int x = 5; x = x + 3;") == 8), + "Déclaration puis assignement" + ); return 0; } \ No newline at end of file