[gnote] Make datetime unit test suite out of existing test



commit bb0b050cc48465ab1571de16d3d73601392f7b79
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Wed Jan 25 23:26:26 2017 +0200

    Make datetime unit test suite out of existing test

 src/Makefile.am                  |    6 +--
 src/test/dttest.cpp              |   92 --------------------------------------
 src/test/runner.cpp              |    3 +
 src/test/unit/datetimeutests.cpp |   85 +++++++++++++++++++++++++++++++++++
 4 files changed, 90 insertions(+), 96 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 5c864c4..8e955f3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -23,7 +23,7 @@ GNOTE_LIBS = libgnote.la $(LIBGNOTE_LIBS)
 
 lib_LTLIBRARIES = libgnote.la
 bin_PROGRAMS = gnote
-check_PROGRAMS = trietest notetest dttest uritest filestest \
+check_PROGRAMS = trietest notetest uritest filestest \
        fileinfotest xmlreadertest notemanagertest gnotesyncclienttest
 TESTS = trietest notetest dttest uritest filestest \
        fileinfotest xmlreadertest notemanagertest gnotesyncclienttest
@@ -34,6 +34,7 @@ TESTS += gnoteunittests
 
 gnoteunittests_SOURCES = \
        test/runner.cpp \
+       test/unit/datetimeutests.cpp \
        test/unit/stringutests.cpp \
        $(NULL)
 gnoteunittests_LDADD = libgnote.la @UNITTESTCPP_LIBS@
@@ -43,9 +44,6 @@ endif
 trietest_SOURCES = test/trietest.cpp
 trietest_LDADD = libgnote.la @LIBGLIBMM_LIBS@
 
-dttest_SOURCES = test/dttest.cpp
-dttest_LDADD = libgnote.la @LIBGLIBMM_LIBS@
-
 filestest_SOURCES = test/filestest.cpp
 filestest_LDADD = libgnote.la @LIBGLIBMM_LIBS@ -lgiomm-2.4
 
diff --git a/src/test/runner.cpp b/src/test/runner.cpp
index 1495f5a..d11aa17 100644
--- a/src/test/runner.cpp
+++ b/src/test/runner.cpp
@@ -22,6 +22,9 @@
 
 int main(int /*argc*/, char ** /*argv*/)
 {
+  // force certain timezone so that time tests work
+  setenv("TZ", "Europe/London", 1);
+
   return UnitTest::RunAllTests();
 }
 
diff --git a/src/test/unit/datetimeutests.cpp b/src/test/unit/datetimeutests.cpp
new file mode 100644
index 0000000..6149ab1
--- /dev/null
+++ b/src/test/unit/datetimeutests.cpp
@@ -0,0 +1,85 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2017 Aurimas Cernius
+ * Copyright (C) 2009 Hubert Figuiere
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY 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 <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <UnitTest++/UnitTest++.h>
+
+#include "utils.hpp"
+#include "sharp/xmlconvert.hpp"
+
+
+SUITE(DateTime)
+{
+
+  bool string_ends_with(const std::string & s, const std::string & other)
+  {
+    return (s.length() - s.rfind(other)) == other.length();
+  }
+
+  TEST(to_string)
+  {
+    sharp::DateTime d(678901234, 67890);
+    Glib::ustring date_string = sharp::XmlConvert::to_string(d);
+    CHECK_EQUAL("1991-07-07T15:40:34.067890Z", date_string);
+
+    d = sharp::DateTime::from_iso8601("2009-03-24T03:34:35.2914680-04:00");
+    // check when usec is 0.
+    // see http://bugzilla.gnome.org/show_bug.cgi?id=581844
+    d.set_usec(0);
+
+    date_string = sharp::XmlConvert::to_string(d);
+    CHECK_EQUAL("2009-03-24T07:34:35.000000Z", date_string);
+  }
+
+  TEST(from_iso8601)
+  {
+    sharp::DateTime d(678901234, 67890);
+    sharp::DateTime d2 = sharp::DateTime::from_iso8601("1991-07-07T15:40:34.067890Z");
+    CHECK(d == d2);
+
+    sharp::DateTime d3 = sharp::DateTime::from_iso8601("2009-03-24T03:34:35.2914680-04:00");
+    CHECK(d3.is_valid());
+  }
+
+  TEST(pretty_print_date)
+  {
+    sharp::DateTime d = sharp::DateTime::now();
+    Glib::ustring date_string = gnote::utils::get_pretty_print_date(d, false, false);
+    CHECK_EQUAL("Today", date_string);
+
+    d.add_days(1);
+    date_string = gnote::utils::get_pretty_print_date(d, false, false);
+    CHECK_EQUAL("Tomorrow", date_string);
+
+    d = sharp::DateTime::now();
+    d.add_days(-1);
+    date_string = gnote::utils::get_pretty_print_date(d, false, false);
+    CHECK_EQUAL("Yesterday", date_string);
+
+    d = sharp::DateTime::from_iso8601("2009-03-24T13:34:35.2914680-04:00");
+    date_string = gnote::utils::get_pretty_print_date(d, true, false);
+    CHECK(string_ends_with(date_string, "17:34"));
+
+    date_string = gnote::utils::get_pretty_print_date(d, true, true);
+    CHECK(string_ends_with(date_string.lowercase(), "5:34 pm"));
+  }
+
+}
+


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