From 6e26045943d27656b4444e157d0d10c025aa69db Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 18 Jun 2015 11:57:12 +0200 Subject: gdm-common: Add gdm_shell_expand() and tests This allows shell-like expansion of strings. It will be later used to allow configuring the environment via config files. https://bugzilla.gnome.org/show_bug.cgi?id=751158 --- tests/Makefile.am | 2 + tests/m-common.c | 11 ++++++ tests/s-common.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/s-common.h | 28 ++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 tests/s-common.c create mode 100644 tests/s-common.h (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 63fc198c..c6475af8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -21,6 +21,8 @@ m_common_SOURCES = \ m-common.c \ s-common-address.c \ s-common-address.h \ + s-common.c \ + s-common.h \ $(NULL) m_common_CFLAGS = \ diff --git a/tests/m-common.c b/tests/m-common.c index c5ff2f4a..62f1f44a 100644 --- a/tests/m-common.c +++ b/tests/m-common.c @@ -24,6 +24,7 @@ #include #include "s-common-address.h" +#include "s-common.h" static gboolean no_fork = FALSE; static gboolean verbose = FALSE; @@ -66,5 +67,15 @@ main (int argc, char **argv) failed = srunner_ntests_failed (r); srunner_free (r); + r = srunner_create (suite_common ()); + + if (no_fork) { + srunner_set_fork_status (r, CK_NOFORK); + } + + srunner_run_all (r, verbose ? CK_VERBOSE : CK_NORMAL); + failed |= srunner_ntests_failed (r); + srunner_free (r); + return failed != 0; } diff --git a/tests/s-common.c b/tests/s-common.c new file mode 100644 index 00000000..c91306fe --- /dev/null +++ b/tests/s-common.c @@ -0,0 +1,110 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2007 Andrew Ziem + * Copyright (C) 2007 William Jon McCann + * Copyright (C) 2015 Alexander Larsson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include + +#include "gdm-common.h" +#include "s-common.h" + +static void +setup (void) +{ +} + +static void +teardown (void) +{ +} + +static char * +expand_fn (const char *var, gpointer data) +{ + if (strcmp (var, "FOO") == 0) + return g_strdup ("BAR"); + if (strcmp (var, "FOO9") == 0) + return g_strdup ("XXX"); + if (strcmp (var, "_FOO") == 0) + return g_strdup ("YYY"); + if (strcmp (var, "FOO_FOO") == 0) + return g_strdup ("ZZZ"); + return NULL; +} + +static gboolean expands_to (const char *to_expand, const char *expanded) +{ + return strcmp (gdm_shell_expand (to_expand, expand_fn, NULL), expanded) == 0; +} + +START_TEST (test_gdm_shell_expand) +{ + fail_unless (expands_to ("foo", "foo")); + fail_unless (expands_to ("foo ", "foo ")); + fail_unless (expands_to ("foo#bar", "foo#bar")); + fail_unless (expands_to ("foo #bar", "foo ")); + fail_unless (expands_to ("#bar", "")); + fail_unless (expands_to ("foo #bar gazonk", "foo ")); + fail_unless (expands_to ("foo #bar gazonk", "foo ")); + fail_unless (expands_to ("foo #bar gazonk", "foo ")); + fail_unless (expands_to ("$FOO", "BAR")); + fail_unless (expands_to ("$9FOO", "$9FOO")); + fail_unless (expands_to ("$FOO9", "XXX")); + fail_unless (expands_to ("${FOO}9", "BAR9")); + fail_unless (expands_to ("$_FOO", "YYY")); + fail_unless (expands_to ("$FOO_FOO", "ZZZ")); + fail_unless (expands_to ("${FOO}", "BAR")); + fail_unless (expands_to ("$FOO$FOO", "BARBAR")); + fail_unless (expands_to ("${FOO}${FOO}", "BARBAR")); + fail_unless (expands_to ("$FOO${FOO}", "BARBAR")); + fail_unless (expands_to ("$foo", "")); + fail_unless (expands_to ("$FOOBAR", "")); + fail_unless (expands_to ("$FOO/BAR", "BAR/BAR")); + fail_unless (expands_to ("${FOO}BAR", "BARBAR")); + fail_unless (expands_to ("$/BAR", "$/BAR")); + fail_unless (expands_to ("${FOO BAR}BAR", "${FOO BAR}BAR")); + fail_unless (expands_to ("${}BAR", "${}BAR")); + fail_unless (expands_to ("${$FOO}BAR", "${BAR}BAR")); + fail_unless (expands_to ("\\$foo", "$foo")); + fail_unless (expands_to ("a\\\\b", "a\\b")); + fail_unless (expands_to ("a\\b", "a\\b")); + fail_unless (expands_to ("a\\#b", "a#b")); +} +END_TEST + +Suite * +suite_common (void) +{ + Suite *s; + TCase *tc_core; + + s = suite_create ("gdm-common"); + tc_core = tcase_create ("core"); + + tcase_add_checked_fixture (tc_core, setup, teardown); + tcase_add_test (tc_core, test_gdm_shell_expand); + suite_add_tcase (s, tc_core); + + return s; +} diff --git a/tests/s-common.h b/tests/s-common.h new file mode 100644 index 00000000..561186f3 --- /dev/null +++ b/tests/s-common.h @@ -0,0 +1,28 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2015 Alexander Larsson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __S_COMMON_H +#define __S_COMMON_H + +#include + +Suite *suite_common (void); + +#endif /* __S_COMMON_H */ -- cgit v1.2.1