diff tests/libcommon/js-elapsed-timer/main.cpp @ 44:f30c84b4b9ed

Tests: switch from GoogleTest to Boost.Unit, closes #588
author David Demelier <markand@malikania.fr>
date Wed, 30 Nov 2016 21:15:53 +0100
parents fabbe1759cec
children 96ba0c5cf893
line wrap: on
line diff
--- a/tests/libcommon/js-elapsed-timer/main.cpp	Tue Nov 29 22:25:17 2016 +0100
+++ b/tests/libcommon/js-elapsed-timer/main.cpp	Wed Nov 30 21:15:53 2016 +0100
@@ -16,12 +16,17 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <gtest/gtest.h>
-
 #include <thread>
 
+#define BOOST_TEST_MODULE "Javascript ElapsedTimer"
+#include <boost/format.hpp>
+#include <boost/test/unit_test.hpp>
+
 #include <malikania/js_elapsed_timer.hpp>
 
+using boost::format;
+using boost::str;
+
 using namespace mlk;
 using namespace std::chrono_literals;
 
@@ -30,26 +35,29 @@
  */
 static constexpr int margin = 30;
 
-class TestElapsedTimer : public testing::Test {
+class test_elapsed_timer {
 protected:
     UniqueContext m_ctx;
 
-    TestElapsedTimer()
+    test_elapsed_timer()
     {
         duk_push_object(m_ctx);
         duk_put_global_string(m_ctx, "Malikania");
         mlk::dukx_load_elapsedtimer(m_ctx);
     }
 
-    inline void assertRange(int value, int expected) const noexcept
+    inline void assert_range(int value, int expected) const
     {
         if (value < (expected - margin) || value > (expected + margin)) {
-            FAIL() << value << " is bigger than [" << (expected - margin) << ", " << (expected + margin) << "]";
+            throw std::invalid_argument(
+                str(format("%d is bigger than [%d, %d]") % value % (expected - margin) % (expected + margin)));
         }
     }
 };
 
-TEST_F(TestElapsedTimer, standard)
+BOOST_FIXTURE_TEST_SUITE(test_elapsed_timer_suite, test_elapsed_timer)
+
+BOOST_AUTO_TEST_CASE(standard)
 {
     try {
         if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) {
@@ -63,14 +71,14 @@
         }
 
         duk_get_global_string(m_ctx, "result");
-        assertRange(duk_to_int(m_ctx, -1), 300);
+        assert_range(duk_to_int(m_ctx, -1), 300);
         duk_pop(m_ctx);
     } catch (const std::exception &ex) {
-        FAIL() << ex.what();
+        BOOST_FAIL(ex.what());
     }
 }
 
-TEST_F(TestElapsedTimer, reset)
+BOOST_AUTO_TEST_CASE(reset)
 {
     try {
         if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) {
@@ -84,14 +92,14 @@
         }
 
         duk_get_global_string(m_ctx, "result");
-        assertRange(duk_to_int(m_ctx, -1), 0);
+        assert_range(duk_to_int(m_ctx, -1), 0);
         duk_pop(m_ctx);
     } catch (const std::exception &ex) {
-        FAIL() << ex.what();
+        BOOST_FAIL(ex.what());
     }
 }
 
-TEST_F(TestElapsedTimer, pause)
+BOOST_AUTO_TEST_CASE(pause)
 {
     try {
         if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) {
@@ -125,14 +133,14 @@
         }
 
         duk_get_global_string(m_ctx, "result");
-        assertRange(duk_to_int(m_ctx, -1), 16);
+        assert_range(duk_to_int(m_ctx, -1), 16);
         duk_pop(m_ctx);
     } catch (const std::exception &ex) {
-        FAIL() << ex.what();
+        BOOST_FAIL(ex.what());
     }
 }
 
-TEST_F(TestElapsedTimer, doublecheck)
+BOOST_AUTO_TEST_CASE(doublecheck)
 {
     try {
         if (duk_peval_string(m_ctx, "timer = new Malikania.ElapsedTimer();") != 0) {
@@ -152,16 +160,11 @@
         }
 
         duk_get_global_string(m_ctx, "result");
-        assertRange(duk_to_int(m_ctx, -1), 100);
+        assert_range(duk_to_int(m_ctx, -1), 100);
         duk_pop(m_ctx);
     } catch (const std::exception &ex) {
-        FAIL() << ex.what();
+        BOOST_FAIL(ex.what());
     }
 }
 
-int main(int argc, char **argv)
-{
-    testing::InitGoogleTest(&argc, argv);
-
-    return RUN_ALL_TESTS();
-}
+BOOST_AUTO_TEST_SUITE_END()