Search
lxdream.org :: lxdream/src/test/testlxpaths.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/test/testlxpaths.c
changeset 1205:a486ac64f34b
next1283:2cbafe321d6f
author nkeynes
date Mon Jan 30 20:11:08 2012 +1000 (7 years ago)
permissions -rw-r--r--
last change Replace wordexp() with a hand-coded env-var substitution. More portable, and
avoids bugs with some wordexp() implementations
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/test/testlxpaths.c Mon Jan 30 20:11:08 2012 +1000
1.3 @@ -0,0 +1,105 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Test cases for path helper functions
1.8 + *
1.9 + * Copyright (c) 2012 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <stdlib.h>
1.23 +#include <stdio.h>
1.24 +#include <string.h>
1.25 +#include <glib/gmem.h>
1.26 +#include "lxpaths.h"
1.27 +
1.28 +char *lxdream_get_global_config_path_value() { }
1.29 +
1.30 +struct expanded_path_case_t {
1.31 + const char *input;
1.32 + const char *output;
1.33 +};
1.34 +
1.35 +char *env_vars[] = { "TEST1=quux", "TEST2=${BLAH}", "TEST3=", "2=3", "TEST_HOME=/home/foo", NULL };
1.36 +const char *unset_env_vars[] = { "PATH_TEST", "1", NULL };
1.37 +struct expanded_path_case_t expanded_path_cases[] = {
1.38 + {NULL, NULL},
1.39 + {"", ""},
1.40 + {"a", "a"},
1.41 + {"$", "$"},
1.42 + {"blah$", "blah$"},
1.43 + {"\\$", "$"},
1.44 + {"foo\\${TEST}\\n\\\\r", "foo${TEST}n\\r"},
1.45 + {"/home/user/.lxdreamrc", "/home/user/.lxdreamrc"},
1.46 + {"${TEST_HOME}/.lxdreamrc", "/home/foo/.lxdreamrc"},
1.47 + {"$TEST_HOME/bar", "/home/foo/bar"},
1.48 + {"/home/$TEST1/blah", "/home/quux/blah"},
1.49 + {"/tmp/${TEST2}/abcd", "/tmp/${BLAH}/abcd"},
1.50 + {"$TEST1$TEST2$TEST3$1$2", "quux${BLAH}3"},
1.51 + {NULL,NULL}
1.52 +};
1.53 +
1.54 +gboolean check_expanded_path( const char *input, const char *output )
1.55 +{
1.56 + char * result = get_expanded_path(input);
1.57 + if( output == NULL ) {
1.58 + if( result != NULL ) {
1.59 + printf( "Unexpected non-null result from get_expanded_path(NULL), got '%s'\n", result );
1.60 + g_free(result);
1.61 + return FALSE;
1.62 + } else {
1.63 + return TRUE;
1.64 + }
1.65 + } else if( result == NULL ) {
1.66 + printf( "Unexpected NULL result from get_expanded_path('%s'), expected '%s'\n", input, output );
1.67 + return FALSE;
1.68 + } else if( strcmp(result, output) != 0 ) {
1.69 + printf( "Unexpected result from get_expanded_path('%s'), expected '%s' but was '%s'\n", input, output, result );
1.70 + g_free(result);
1.71 + return FALSE;
1.72 + } else {
1.73 + g_free(result);
1.74 + return TRUE;
1.75 + }
1.76 +}
1.77 +
1.78 +
1.79 +gboolean test_get_expanded_path()
1.80 +{
1.81 + int count, i;
1.82 + int fails = 0;
1.83 +
1.84 + for( i=0; env_vars[i] != NULL; i++ ) {
1.85 + putenv(env_vars[i]);
1.86 + }
1.87 + for( i=0; unset_env_vars[i] != NULL; i++ ) {
1.88 + unsetenv(unset_env_vars[i]);
1.89 + }
1.90 +
1.91 + for( count=0; expanded_path_cases[count].input != NULL || count == 0; count++ ) {
1.92 + gboolean success = check_expanded_path(expanded_path_cases[count].input, expanded_path_cases[count].output);
1.93 + if( !success )
1.94 + fails ++;
1.95 + }
1.96 + printf( "get_expanded_path: %d/%d (%s)\n", (count-fails), count, (fails == 0 ? "OK" : "ERROR"));
1.97 + return fails == 0 ? TRUE : FALSE;
1.98 +
1.99 + /* FIXME: Should probably restore the env state, but doesn't matter at the moment */
1.100 +}
1.101 +
1.102 +int main()
1.103 +{
1.104 + gboolean result = TRUE;
1.105 + result = test_get_expanded_path() && result;
1.106 +
1.107 + return result ? 0 : 1;
1.108 +}
.