From 273c3b3829c9a3e853d0b6b0a32ae87cc3c6852b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 16 Mar 2015 21:07:54 +0100 Subject: add DateTime::end_of_month(), DateTime::end_of_day(). Add unit tests for them. --- tests/CMakeLists.txt | 1 + tests/manual-test-snap.cpp | 2 +- tests/test-alarm-queue.cpp | 4 +- tests/test-datetime.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++++ tests/test-live-actions.cpp | 14 +++-- tests/test-snap.cpp | 7 +-- 6 files changed, 158 insertions(+), 13 deletions(-) create mode 100644 tests/test-datetime.cpp (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 20e744a..25fe5dc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -47,6 +47,7 @@ function(add_test_by_name name) add_dependencies (${TEST_NAME} libindicatordatetimeservice) target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS}) endfunction() +add_test_by_name(test-datetime) add_test_by_name(test-snap) add_test_by_name(test-actions) add_test_by_name(test-alarm-queue) diff --git a/tests/manual-test-snap.cpp b/tests/manual-test-snap.cpp index 94c1ded..22ef137 100644 --- a/tests/manual-test-snap.cpp +++ b/tests/manual-test-snap.cpp @@ -71,7 +71,7 @@ int main(int argc, const char* argv[]) a.uid = "D4B57D50247291478ED31DED17FF0A9838DED402"; a.type = Appointment::UBUNTU_ALARM; a.begin = DateTime::Local(2014, 12, 25, 0, 0, 0); - a.end = a.begin.add_full(0,0,1,0,0,-1); + a.end = a.begin.end_of_day(); auto loop = g_main_loop_new(nullptr, false); auto on_snooze = [loop](const Appointment& appt){ diff --git a/tests/test-alarm-queue.cpp b/tests/test-alarm-queue.cpp index 0492539..3fdf787 100644 --- a/tests/test-alarm-queue.cpp +++ b/tests/test-alarm-queue.cpp @@ -69,7 +69,7 @@ protected: { const auto now = m_state->clock->localtime(); const auto tomorrow_begin = now.add_days(1).start_of_day(); - const auto tomorrow_end = tomorrow_begin.add_full(0, 0, 1, 0, 0, -1); + const auto tomorrow_end = tomorrow_begin.end_of_day(); Appointment a1; // an alarm clock appointment a1.color = "red"; @@ -81,7 +81,7 @@ protected: a1.end = tomorrow_end; const auto ubermorgen_begin = now.add_days(2).start_of_day(); - const auto ubermorgen_end = ubermorgen_begin.add_full(0, 0, 1, 0, 0, -1); + const auto ubermorgen_end = ubermorgen_begin.end_of_day(); Appointment a2; // a non-alarm appointment a2.color = "green"; diff --git a/tests/test-datetime.cpp b/tests/test-datetime.cpp new file mode 100644 index 0000000..41f78fb --- /dev/null +++ b/tests/test-datetime.cpp @@ -0,0 +1,143 @@ +/* + * Copyright 2015 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include + +#include "glib-fixture.h" + +using namespace unity::indicator::datetime; + +/*** +**** +***/ + +class DateTimeFixture: public GlibFixture +{ + public: + + DateTimeFixture() =default; + virtual ~DateTimeFixture() =default; + + private: + + typedef GlibFixture super; + + protected: + + GRand * m_rand = nullptr; + + virtual void SetUp() override + { + super::SetUp(); + + m_rand = g_rand_new(); + } + + virtual void TearDown() override + { + g_clear_pointer(&m_rand, g_rand_free); + + super::TearDown(); + } + + DateTime random_day() + { + return DateTime::Local(g_rand_int_range(m_rand, 1970, 3000), + g_rand_int_range(m_rand, 1, 13), + g_rand_int_range(m_rand, 1, 29), + g_rand_int_range(m_rand, 0, 24), + g_rand_int_range(m_rand, 0, 60), + g_rand_double_range(m_rand, 0, 60.0)); + } +}; + +/*** +**** +***/ + +TEST_F(DateTimeFixture, StartAndEnd) +{ + const int n_iterations{10000}; + + for (int i{0}; icalendar_month->appointments().set(std::vector({a1, a2})); diff --git a/tests/test-snap.cpp b/tests/test-snap.cpp index e7161d3..3dd4501 100644 --- a/tests/test-snap.cpp +++ b/tests/test-snap.cpp @@ -109,10 +109,9 @@ protected: appt.url = "alarm:///hello-world"; appt.uid = "D4B57D50247291478ED31DED17FF0A9838DED402"; appt.type = Appointment::EVENT; - auto begin = DateTime::Local(2014,12,25,0,0,0); - auto end = begin.add_full(0,0,1,0,0,-1); - appt.begin = begin; - appt.end = end; + const auto christmas = DateTime::Local(2015,12,25,0,0,0); + appt.begin = christmas.start_of_day(); + appt.end = christmas.end_of_day(); service = dbus_test_service_new(nullptr); -- cgit v1.2.3