lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi] [PATCH] Add LMI_ASSERT_MESSAGE() macro


From: Vadim Zeitlin
Subject: [lmi] [PATCH] Add LMI_ASSERT_MESSAGE() macro
Date: Mon, 6 Oct 2014 19:57:02 +0200

 Hello again,

 There are just 2 more changes to non-test files that remain, so I'd like
to submit them already, before the full testing patch. The first one of
them is a pure addition and defines LMI_ASSERT_MESSAGE() macro which,
unlike plain LMI_ASSERT(), shows the message specified as its second
argument if the check fails. This still doesn't make the GUI test suite as
user friendly as I'd like it to be, but it's better than nothing.

 As an example, consider LMI_ASSERT_EQUAL() macro defined in terms of
LMI_ASSERT_MESSAGE() and its use in the code, e.g.:

    LMI_ASSERT_EQUAL(settings.xsl_fo_command(), "CMD /c c:/fop-0.20.5/fop");

If the check fails, the macro will output the expected value, which is not
indispensable, as it can be found in the source code, but still convenient,
but also, and mostly usefully, the actual value of xsl_fo_command in the
settings.

 Another, less common, example of use of this macro:

            double const diff_in_percents =
                100*(time_real - time_expected)
                    / static_cast<double>(time_expected);

            LMI_ASSERT_MESSAGE
                (std::fabs(diff_in_percents) < 10
                ,wxString::Format
                    (
                    "expected %ldms, but actually took %ldms, i.e. %+.2f%%"
                    ,time_expected
                    ,time_real
                    ,diff_in_percents
                    )
                );

This shows that LMI_ASSERT_EQUAL() is not always enough and that the
ability to specify an arbitrary version is really useful.


 And here is the patch itself:

-- >8 --
diff --git a/assert_lmi.hpp b/assert_lmi.hpp
index a39d5b4..e65c1e3 100644
--- a/assert_lmi.hpp
+++ b/assert_lmi.hpp
@@ -59,5 +59,40 @@
         }                                                       \
     while(0)

+
+/// A version of LMI_ASSERT() allowing to specify additional information to log
+/// in case of assertion failure.
+
+#define LMI_ASSERT_MESSAGE(condition, message)                  \
+    do                                                          \
+        {                                                       \
+        if(!(condition))                                        \
+            {                                                   \
+            std::ostringstream oss;                             \
+            oss                                                 \
+                << "Assertion '" << (#condition) << "' failed"  \
+                << "\n(" << message << ")\n"                    \
+                << "\n[file " << __FILE__                       \
+                << ", line " << __LINE__ << "]\n"               \
+                ;                                               \
+            throw std::runtime_error(oss.str());                \
+            }                                                   \
+        }                                                       \
+    while(0)
+
+
+/// A specialized version of LMI_ASSERT() checking for equality of two
+/// expressions.
+///
+/// Passing "a" and "b" separately instead of just using LMI_ASSERT(a == b)
+/// allows to automatically construct an informative error message shown in
+/// case of the assert failure with the values of "a" and "b".
+
+#define LMI_ASSERT_EQUAL(actual, expected)                   \
+    LMI_ASSERT_MESSAGE                                       \
+        (actual == expected                                  \
+        ,"expected " << expected << " but actual " << actual \
+        )
+
 #endif // assert_lmi_hpp
-- >8 --

 TIA,
VZ

reply via email to

[Prev in Thread] Current Thread [Next in Thread]