From cb3fe67baad34128b93c76720900ccac555eb0eb Mon Sep 17 00:00:00 2001 From: Yuri Gorshenin Date: Tue, 9 Feb 2016 12:46:12 +0300 Subject: [PATCH] [testing] Fixed TEST*() macroses. --- .../generator_tests/metadata_parser_test.cpp | 18 +- map/map_tests/gps_track_collection_test.cpp | 2 +- map/map_tests/gps_track_test.cpp | 4 +- testing/testing.hpp | 215 ++++++++++++++---- 4 files changed, 178 insertions(+), 61 deletions(-) diff --git a/generator/generator_tests/metadata_parser_test.cpp b/generator/generator_tests/metadata_parser_test.cpp index f726ec57e0..b04a210e1f 100644 --- a/generator/generator_tests/metadata_parser_test.cpp +++ b/generator/generator_tests/metadata_parser_test.cpp @@ -37,40 +37,40 @@ UNIT_TEST(Metadata_ValidateAndFormat_stars) // Check correct values. p("stars", "1"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "1", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "1", ()); md.Drop(Metadata::FMD_STARS); p("stars", "2"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "2", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "2", ()); md.Drop(Metadata::FMD_STARS); p("stars", "3"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "3", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "3", ()); md.Drop(Metadata::FMD_STARS); p("stars", "4"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "4", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "4", ()); md.Drop(Metadata::FMD_STARS); p("stars", "5"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "5", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "5", ()); md.Drop(Metadata::FMD_STARS); p("stars", "6"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "6", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "6", ()); md.Drop(Metadata::FMD_STARS); p("stars", "7"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "7", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "7", ()); md.Drop(Metadata::FMD_STARS); // Check almost correct values. p("stars", "4+"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "4", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "4", ()); md.Drop(Metadata::FMD_STARS); p("stars", "5s"); - TEST_EQUAL(md.Get(Metadata::FMD_STARS), "5", ()) + TEST_EQUAL(md.Get(Metadata::FMD_STARS), "5", ()); md.Drop(Metadata::FMD_STARS); } diff --git a/map/map_tests/gps_track_collection_test.cpp b/map/map_tests/gps_track_collection_test.cpp index d31e5454b1..9fca7caa0f 100644 --- a/map/map_tests/gps_track_collection_test.cpp +++ b/map/map_tests/gps_track_collection_test.cpp @@ -98,7 +98,7 @@ UNIT_TEST(GpsTrackCollection_EvictedByTimestamp) collection.ForEach([&](location::GpsTrackInfo const & info, size_t id)->bool { - TEST_EQUAL(id, 2, ()) + TEST_EQUAL(id, 2, ()); TEST_EQUAL(info.m_latitude, lastInfo.m_latitude, ()); TEST_EQUAL(info.m_longitude, lastInfo.m_longitude, ()); TEST_EQUAL(info.m_speed, lastInfo.m_speed, ()); diff --git a/map/map_tests/gps_track_test.cpp b/map/map_tests/gps_track_test.cpp index 6da0d64e29..33ef98af4f 100644 --- a/map/map_tests/gps_track_test.cpp +++ b/map/map_tests/gps_track_test.cpp @@ -170,7 +170,7 @@ UNIT_TEST(GpsTrack_EvictedByAdd) TEST(callback.WaitForCallback(kWaitForCallbackTimeout), ()); // Check pt1 was added - TEST_EQUAL(1, callback.m_toAdd.size(), ()) + TEST_EQUAL(1, callback.m_toAdd.size(), ()); TEST_EQUAL(0, callback.m_toAdd[0].first, ()); TEST_EQUAL(pt1.m_timestamp, callback.m_toAdd[0].second.m_timestamp, ()); TEST_EQUAL(pt1.m_speed, callback.m_toAdd[0].second.m_speed, ()); @@ -187,7 +187,7 @@ UNIT_TEST(GpsTrack_EvictedByAdd) TEST(callback.WaitForCallback(kWaitForCallbackTimeout), ()); // Check pt2 was added - TEST_EQUAL(1, callback.m_toAdd.size(), ()) + TEST_EQUAL(1, callback.m_toAdd.size(), ()); TEST_EQUAL(1, callback.m_toAdd[0].first, ()); TEST_EQUAL(pt2.m_timestamp, callback.m_toAdd[0].second.m_timestamp, ()); TEST_EQUAL(pt2.m_speed, callback.m_toAdd[0].second.m_speed, ()); diff --git a/testing/testing.hpp b/testing/testing.hpp index 19ef6b87df..818f1a0aa9 100644 --- a/testing/testing.hpp +++ b/testing/testing.hpp @@ -59,54 +59,171 @@ struct CommandLineOptions }; CommandLineOptions const & GetTestingOptions(); -#define TEST(X, msg) { if (X) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X")", ::my::impl::Message msg));}} -#define TEST_EQUAL(X, Y, msg) { if ((X) == (Y)) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" == "#Y")", \ - ::my::impl::Message(X, Y), \ - ::my::impl::Message msg));}} -#define TEST_NOT_EQUAL(X, Y, msg) { if ((X) != (Y)) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" != "#Y")", \ - ::my::impl::Message(X, Y), \ - ::my::impl::Message msg));}} -#define TEST_LESS(X, Y, msg) { if ((X) < (Y)) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" < "#Y")", \ - ::my::impl::Message(X, Y), \ - ::my::impl::Message msg));}} -#define TEST_LESS_OR_EQUAL(X, Y, msg) { if ((X) <= (Y)) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" <= "#Y")", \ - ::my::impl::Message(X, Y), \ - ::my::impl::Message msg));}} -#define TEST_GREATER(X, Y, msg) { if ((X) > (Y)) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" > "#Y")", \ - ::my::impl::Message(X, Y), \ - ::my::impl::Message msg));}} -#define TEST_GREATER_OR_EQUAL(X, Y, msg) { if ((X) >= (Y)) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" >= "#Y")", \ - ::my::impl::Message(X, Y), \ - ::my::impl::Message msg));}} -#define TEST_ALMOST_EQUAL_ULPS(X, Y, msg) { if (::my::AlmostEqualULPs(X, Y)) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST(my::AlmostEqualULPs("#X", "#Y")", \ - ::my::impl::Message(X, Y), \ - ::my::impl::Message msg));}} -#define TEST_NOT_ALMOST_EQUAL_ULPS(X, Y, msg) { if (!::my::AlmostEqualULPs(X, Y)) {} else { \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST(!my::AlmostEqualULPs("#X", "#Y")", \ - ::my::impl::Message(X, Y), \ - ::my::impl::Message msg));}} +#define TEST(X, msg) \ + do \ + { \ + if (X) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), ::my::impl::Message("TEST(" #X ")", ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_EQUAL(X, Y, msg) \ + do \ + { \ + if ((X) == (Y)) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), \ + ::my::impl::Message("TEST(" #X " == " #Y ")", ::my::impl::Message(X, Y), \ + ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_NOT_EQUAL(X, Y, msg) \ + do \ + { \ + if ((X) != (Y)) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), \ + ::my::impl::Message("TEST(" #X " != " #Y ")", ::my::impl::Message(X, Y), \ + ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_LESS(X, Y, msg) \ + do \ + { \ + if ((X) < (Y)) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), \ + ::my::impl::Message("TEST(" #X " < " #Y ")", ::my::impl::Message(X, Y), \ + ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_LESS_OR_EQUAL(X, Y, msg) \ + do \ + { \ + if ((X) <= (Y)) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), \ + ::my::impl::Message("TEST(" #X " <= " #Y ")", ::my::impl::Message(X, Y), \ + ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_GREATER(X, Y, msg) \ + do \ + { \ + if ((X) > (Y)) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), \ + ::my::impl::Message("TEST(" #X " > " #Y ")", ::my::impl::Message(X, Y), \ + ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_GREATER_OR_EQUAL(X, Y, msg) \ + do \ + { \ + if ((X) >= (Y)) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), \ + ::my::impl::Message("TEST(" #X " >= " #Y ")", ::my::impl::Message(X, Y), \ + ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_ALMOST_EQUAL_ULPS(X, Y, msg) \ + do \ + { \ + if (::my::AlmostEqualULPs(X, Y)) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), \ + ::my::impl::Message("TEST(my::AlmostEqualULPs(" #X ", " #Y ")", \ + ::my::impl::Message(X, Y), ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_NOT_ALMOST_EQUAL_ULPS(X, Y, msg) \ + do \ + { \ + if (!::my::AlmostEqualULPs(X, Y)) \ + { \ + } \ + else \ + { \ + ::my::OnTestFailed(SRC(), \ + ::my::impl::Message("TEST(!my::AlmostEqualULPs(" #X ", " #Y ")", \ + ::my::impl::Message(X, Y), ::my::impl::Message msg)); \ + } \ + } while (0) // TODO(AlexZ): Add more cool macroses (or switch all unit tests to gtest). -#define TEST_THROW(X, exception, msg) { bool expected_exception = false; \ - try { X; } catch (exception const &) { expected_exception = true; } \ - catch (...) { ::my::OnTestFailed(SRC(), ::my::impl::Message("Unexpected exception at TEST("#X")", \ - ::my::impl::Message msg)); } \ - if (!expected_exception) \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("Expected exception "#exception" was not thrown in TEST("#X")", \ - ::my::impl::Message msg));} -#define TEST_NO_THROW(X, msg) { \ - try { X; } catch (...) { ::my::OnTestFailed(SRC(), ::my::impl::Message("Unexpected exception at TEST("#X")", \ - ::my::impl::Message msg));}} -#define TEST_ANY_THROW(X, msg) { bool was_exception = false; \ - try { X; } catch (...) { was_exception = true; } \ - if (!was_exception) \ - ::my::OnTestFailed(SRC(), ::my::impl::Message("No exceptions were thrown in TEST("#X")", \ - ::my::impl::Message msg));} +#define TEST_THROW(X, exception, msg) \ + do \ + { \ + bool expected_exception = false; \ + try \ + { \ + X; \ + } \ + catch (exception const &) \ + { \ + expected_exception = true; \ + } \ + catch (...) \ + { \ + ::my::OnTestFailed(SRC(), ::my::impl::Message("Unexpected exception at TEST(" #X ")", \ + ::my::impl::Message msg)); \ + } \ + if (!expected_exception) \ + ::my::OnTestFailed(SRC(), ::my::impl::Message("Expected exception " #exception \ + " was not thrown in TEST(" #X ")", \ + ::my::impl::Message msg)); \ + } while (0) +#define TEST_NO_THROW(X, msg) \ + do \ + { \ + try \ + { \ + X; \ + } \ + catch (...) \ + { \ + ::my::OnTestFailed(SRC(), ::my::impl::Message("Unexpected exception at TEST(" #X ")", \ + ::my::impl::Message msg)); \ + } \ + } while (0) +#define TEST_ANY_THROW(X, msg) \ + do \ + { \ + bool was_exception = false; \ + try \ + { \ + X; \ + } \ + catch (...) \ + { \ + was_exception = true; \ + } \ + if (!was_exception) \ + ::my::OnTestFailed(SRC(), ::my::impl::Message("No exceptions were thrown in TEST(" #X ")", \ + ::my::impl::Message msg)); \ + } while (0)