From dd147887cb70a52dcba1cd26d9bbf7e29545afc4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 7 Sep 2013 09:39:38 -0500 Subject: add preliminary unit tests for utils --- src/utils.c | 51 ++++++++++++++---------- tests/Makefile.am | 6 +++ tests/test-utils.cc | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 tests/test-utils.cc diff --git a/src/utils.c b/src/utils.c index b99de94..5dccc52 100644 --- a/src/utils.c +++ b/src/utils.c @@ -53,32 +53,41 @@ is_locale_12h (void) void split_settings_location (const gchar * location, gchar ** zone, gchar ** name) { - gchar * location_dup = g_strdup (location); - gchar * first = strchr (location_dup, ' '); + gchar * location_dup; + gchar * first; - if (first) { - first[0] = 0; - } + location_dup = g_strdup (location); + g_strstrip (location_dup); - if (zone) { - *zone = location_dup; - } + if ((first = strchr (location_dup, ' '))) + *first = '\0'; - if (name) { - gchar * after = first ? g_strstrip (first + 1) : NULL; - if (after == NULL || after[0] == 0) { - /* Make up name from zone */ - gchar * chr = strrchr (location_dup, '/'); - after = g_strdup (chr ? chr + 1 : location_dup); - while ((chr = strchr (after, '_')) != NULL) { /* and turn underscores to spaces */ - *chr = ' '; - } - *name = after; + if (zone != NULL) + { + *zone = location_dup; } - else { - *name = g_strdup (after); + + if (name != NULL) + { + gchar * after = first ? g_strstrip (first + 1) : NULL; + + if (after && *after) + { + *name = g_strdup (after); + } + else /* make the name from zone */ + { + gchar * chr = strrchr (location_dup, '/'); + after = g_strdup (chr ? chr + 1 : location_dup); + + /* replace underscores with spaces */ + for (chr=after; chr && *chr; chr++) + if (*chr == '_') + *chr = ' '; + + *name = after; + } } - } } gchar * diff --git a/tests/Makefile.am b/tests/Makefile.am index dce6076..6435d75 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -50,3 +50,9 @@ test_indicator_CPPFLAGS = $(TEST_CPPFLAGS) -DSCHEMA_DIR="\"$(top_builddir)/tests ### ### ### + +TESTS += test-utils +check_PROGRAMS += test-utils +test_utils_SOURCES = test-utils.cc $(top_srcdir)/src/utils.c +test_utils_LDADD = $(TEST_LIBS) +test_utils_CPPFLAGS = $(TEST_CPPFLAGS) -DSCHEMA_DIR="\"$(top_builddir)/tests/\"" diff --git a/tests/test-utils.cc b/tests/test-utils.cc new file mode 100644 index 0000000..c757a11 --- /dev/null +++ b/tests/test-utils.cc @@ -0,0 +1,112 @@ +/* +Copyright 2012 Canonical Ltd. + +Authors: + Charles Kerr + +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 . +*/ + +#include + +#include + +#include "utils.h" + +/*** +**** +***/ + +TEST (UtilsTest, SplitSettingsLocation) +{ + guint i; + guint n; + + struct { + const char * location; + const char * expected_zone; + const char * expected_name; + } test_cases[] = { + { "America/Chicago Chicago", "America/Chicago", "Chicago" }, + { "America/Chicago Oklahoma City", "America/Chicago", "Oklahoma City" }, + { "America/Los_Angeles", "America/Los_Angeles", "Los Angeles" }, + { "America/Los_Angeles ", "America/Los_Angeles", "Los Angeles" }, + { " America/Los_Angeles", "America/Los_Angeles", "Los Angeles" }, + { " America/Los_Angeles ", "America/Los_Angeles", "Los Angeles" }, + { "UTC UTC", "UTC", "UTC" } + }; + + for (i=0, n=G_N_ELEMENTS(test_cases); i Date: Sat, 7 Sep 2013 09:48:46 -0500 Subject: get coverage reports working. We now have nonzero coverage, whoo --- src/Makefile.am | 83 +++++++++++++++++++++++++++++++++++-------------------- tests/Makefile.am | 4 +-- 2 files changed, 55 insertions(+), 32 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 2f247c1..093a258 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,44 +1,67 @@ -if BUILD_CCPANEL -ccpaneldir = $(libdir)/control-center-1/panels/ -ccpanel_LTLIBRARIES = libindicator-datetime.la -endif +SHARED_CFLAGS = \ + -Wall \ + -Wextra -Wno-missing-field-initializers \ + -Werror \ + $(SERVICE_CFLAGS) \ + $(COVERAGE_CFLAGS) \ + -DTIMEZONE_FILE="\"/etc/timezone\"" \ + -DG_LOG_DOMAIN=\"Indicator-Datetime\" + +### +### +### + +noinst_LIBRARIES = libindicator-datetime-service.a + +libindicator_datetime_service_a_CFLAGS = \ + $(SHARED_CFLAGS) + +libindicator_datetime_service_a_SOURCES = \ + planner.c \ + planner.h \ + planner-eds.c \ + planner-eds.h \ + service.c \ + service.h \ + timezone.c \ + timezone.h \ + timezone-file.c \ + timezone-file.h \ + timezone-geoclue.c \ + timezone-geoclue.h \ + utils.c \ + utils.h \ + dbus-shared.h \ + settings-shared.h + +### +### +### libexec_PROGRAMS = indicator-datetime-service indicator_datetime_service_SOURCES = \ - planner.c \ - planner.h \ - planner-eds.c \ - planner-eds.h \ - service.c \ - service.h \ - main.c \ - timezone.c \ - timezone.h \ - timezone-file.c \ - timezone-file.h \ - timezone-geoclue.c \ - timezone-geoclue.h \ - utils.c \ - utils.h \ - dbus-shared.h \ - settings-shared.h + main.c indicator_datetime_service_CFLAGS = \ - -Wall \ - -Wextra -Wno-missing-field-initializers \ - -Werror \ - $(SERVICE_CFLAGS) \ - $(COVERAGE_CFLAGS) \ - -DTIMEZONE_FILE="\"/etc/timezone\"" \ - -DG_LOG_DOMAIN=\"Indicator-Datetime\" + $(SHARED_CFLAGS) + indicator_datetime_service_LDADD = \ - $(SERVICE_LIBS) + libindicator-datetime-service.a \ + $(SERVICE_LIBS) + indicator_datetime_service_LDFLAGS = \ - $(COVERAGE_LDFLAGS) + $(COVERAGE_LDFLAGS) + +### +### +### if BUILD_CCPANEL +ccpaneldir = $(libdir)/control-center-1/panels/ +ccpanel_LTLIBRARIES = libindicator-datetime.la + libindicator_datetime_la_SOURCES =\ datetime-prefs.c \ datetime-prefs-locations.c \ diff --git a/tests/Makefile.am b/tests/Makefile.am index 6435d75..e204378 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -53,6 +53,6 @@ test_indicator_CPPFLAGS = $(TEST_CPPFLAGS) -DSCHEMA_DIR="\"$(top_builddir)/tests TESTS += test-utils check_PROGRAMS += test-utils -test_utils_SOURCES = test-utils.cc $(top_srcdir)/src/utils.c -test_utils_LDADD = $(TEST_LIBS) +test_utils_SOURCES = test-utils.cc +test_utils_LDADD = $(top_builddir)/src/libindicator-datetime-service.a $(TEST_LIBS) test_utils_CPPFLAGS = $(TEST_CPPFLAGS) -DSCHEMA_DIR="\"$(top_builddir)/tests/\"" -- cgit v1.2.3 From 2bf6740a420f35942e9f74b7f7ef566de0ba736b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 9 Sep 2013 11:53:10 -0500 Subject: in utils.h, include gio/gio.h to ensure GSettings struct is declared --- src/utils.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils.h b/src/utils.h index fff9194..31a12bc 100644 --- a/src/utils.h +++ b/src/utils.h @@ -24,6 +24,7 @@ with this program. If not, see . #define __DATETIME_UTILS_H__ #include +#include /* GSettings */ G_BEGIN_DECLS -- cgit v1.2.3