Add tests for new g_environ_ functions

This commit is contained in:
Matthias Clasen 2011-10-15 16:28:07 -04:00
parent 7a9987d35d
commit 94c246e8e3

View File

@ -1,7 +1,7 @@
#include <glib.h>
static void
test_environment (void)
test_listenv (void)
{
GHashTable *table;
gchar **list;
@ -40,12 +40,70 @@ test_environment (void)
g_strfreev (list);
}
static void
test_setenv (void)
{
const gchar *var, *value;
var = "NOSUCHENVVAR";
value = "value1";
g_assert (g_getenv (var) == NULL);
g_setenv (var, value, FALSE);
g_assert_cmpstr (g_getenv (var), ==, value);
g_assert (g_setenv (var, "value2", FALSE));
g_assert_cmpstr (g_getenv (var), ==, value);
g_assert (g_setenv (var, "value2", TRUE));
g_assert_cmpstr (g_getenv (var), ==, "value2");
g_unsetenv (var);
g_assert (g_getenv (var) == NULL);
}
static void
test_environ_array (void)
{
gchar **env;
const gchar *value;
env = g_new (gchar *, 1);
env[0] = NULL;
value = g_environ_getenv (env, "foo");
g_assert (value == NULL);
env = g_environ_setenv (env, "foo", "bar", TRUE);
value = g_environ_getenv (env, "foo");
g_assert_cmpstr (value, ==, "bar");
env = g_environ_setenv (env, "foo2", "bar2", FALSE);
value = g_environ_getenv (env, "foo");
g_assert_cmpstr (value, ==, "bar");
value = g_environ_getenv (env, "foo2");
g_assert_cmpstr (value, ==, "bar2");
env = g_environ_setenv (env, "foo", "x", FALSE);
value = g_environ_getenv (env, "foo");
g_assert_cmpstr (value, ==, "bar");
env = g_environ_setenv (env, "foo", "x", TRUE);
value = g_environ_getenv (env, "foo");
g_assert_cmpstr (value, ==, "x");
env = g_environ_unsetenv (env, "foo2");
value = g_environ_getenv (env, "foo2");
g_assert (value == NULL);
g_strfreev (env);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/glib/environment", test_environment);
g_test_add_func ("/environ/listenv", test_listenv);
g_test_add_func ("/environ/setenv", test_setenv);
g_test_add_func ("/environ/array", test_environ_array);
return g_test_run ();
}