Search
lxdream.org :: lxdream/src/test/testlxpaths.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/test/testlxpaths.c
changeset 1298:d0eb2307b847
prev1296:30ecee61f811
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
file annotate diff log raw
nkeynes@1205
     1
/**
nkeynes@1205
     2
 * $Id$
nkeynes@1205
     3
 *
nkeynes@1205
     4
 * Test cases for path helper functions
nkeynes@1205
     5
 *
nkeynes@1205
     6
 * Copyright (c) 2012 Nathan Keynes.
nkeynes@1205
     7
 *
nkeynes@1205
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@1205
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@1205
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@1205
    11
 * (at your option) any later version.
nkeynes@1205
    12
 *
nkeynes@1205
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@1205
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@1205
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@1205
    16
 * GNU General Public License for more details.
nkeynes@1205
    17
 */
nkeynes@1205
    18
nkeynes@1205
    19
#include <stdlib.h>
nkeynes@1205
    20
#include <stdio.h>
nkeynes@1205
    21
#include <string.h>
nkeynes@1296
    22
#include <glib.h>
nkeynes@1205
    23
#include "lxpaths.h"
nkeynes@1205
    24
nkeynes@1298
    25
char *lxdream_get_global_config_path_value() { return NULL; }
nkeynes@1283
    26
void log_message( void *ptr, int level, const gchar *source, const char *msg, ... ) { }
nkeynes@1205
    27
nkeynes@1205
    28
struct expanded_path_case_t {
nkeynes@1205
    29
    const char *input;
nkeynes@1205
    30
    const char *output;
nkeynes@1205
    31
};
nkeynes@1205
    32
nkeynes@1205
    33
char *env_vars[] = { "TEST1=quux", "TEST2=${BLAH}", "TEST3=", "2=3", "TEST_HOME=/home/foo", NULL };
nkeynes@1205
    34
const char *unset_env_vars[] = { "PATH_TEST", "1", NULL };
nkeynes@1205
    35
struct expanded_path_case_t expanded_path_cases[] = {
nkeynes@1205
    36
    {NULL, NULL},
nkeynes@1205
    37
    {"", ""},
nkeynes@1205
    38
    {"a", "a"},
nkeynes@1205
    39
    {"$", "$"},
nkeynes@1205
    40
    {"blah$", "blah$"},
nkeynes@1205
    41
    {"\\$", "$"},
nkeynes@1205
    42
    {"foo\\${TEST}\\n\\\\r", "foo${TEST}n\\r"},
nkeynes@1205
    43
    {"/home/user/.lxdreamrc", "/home/user/.lxdreamrc"},
nkeynes@1205
    44
    {"${TEST_HOME}/.lxdreamrc", "/home/foo/.lxdreamrc"},
nkeynes@1205
    45
    {"$TEST_HOME/bar", "/home/foo/bar"},
nkeynes@1205
    46
    {"/home/$TEST1/blah", "/home/quux/blah"},
nkeynes@1205
    47
    {"/tmp/${TEST2}/abcd", "/tmp/${BLAH}/abcd"},
nkeynes@1205
    48
    {"$TEST1$TEST2$TEST3$1$2", "quux${BLAH}3"},
nkeynes@1283
    49
    {"\"/home/foo\"", "/home/foo"},
nkeynes@1205
    50
    {NULL,NULL}
nkeynes@1205
    51
};
nkeynes@1205
    52
nkeynes@1205
    53
gboolean check_expanded_path( const char *input, const char *output )
nkeynes@1205
    54
{
nkeynes@1205
    55
    char * result = get_expanded_path(input);
nkeynes@1205
    56
    if( output == NULL ) {
nkeynes@1205
    57
        if( result != NULL ) {
nkeynes@1205
    58
            printf( "Unexpected non-null result from get_expanded_path(NULL), got '%s'\n", result );
nkeynes@1205
    59
            g_free(result);
nkeynes@1205
    60
            return FALSE;
nkeynes@1205
    61
        } else {
nkeynes@1205
    62
            return TRUE;
nkeynes@1205
    63
        }
nkeynes@1205
    64
    } else if( result == NULL ) {
nkeynes@1205
    65
        printf( "Unexpected NULL result from get_expanded_path('%s'), expected '%s'\n", input, output );
nkeynes@1205
    66
        return FALSE;
nkeynes@1205
    67
    } else if( strcmp(result, output) != 0 ) {
nkeynes@1205
    68
        printf( "Unexpected result from get_expanded_path('%s'), expected '%s' but was '%s'\n", input, output, result );
nkeynes@1205
    69
        g_free(result);
nkeynes@1205
    70
        return FALSE;
nkeynes@1205
    71
    } else {
nkeynes@1205
    72
        g_free(result);
nkeynes@1205
    73
        return TRUE;
nkeynes@1205
    74
    }
nkeynes@1205
    75
}
nkeynes@1205
    76
nkeynes@1205
    77
nkeynes@1205
    78
gboolean test_get_expanded_path()
nkeynes@1205
    79
{
nkeynes@1205
    80
    int count, i;
nkeynes@1205
    81
    int fails = 0;
nkeynes@1205
    82
    
nkeynes@1205
    83
    for( i=0; env_vars[i] != NULL; i++ ) {
nkeynes@1205
    84
        putenv(env_vars[i]);
nkeynes@1205
    85
    }
nkeynes@1205
    86
    for( i=0; unset_env_vars[i] != NULL; i++ ) {
nkeynes@1205
    87
        unsetenv(unset_env_vars[i]);
nkeynes@1205
    88
    }
nkeynes@1205
    89
nkeynes@1205
    90
    for( count=0; expanded_path_cases[count].input != NULL || count == 0; count++ ) {
nkeynes@1205
    91
        gboolean success = check_expanded_path(expanded_path_cases[count].input, expanded_path_cases[count].output);
nkeynes@1205
    92
        if( !success )
nkeynes@1205
    93
            fails ++;
nkeynes@1205
    94
    }
nkeynes@1205
    95
    printf( "get_expanded_path: %d/%d (%s)\n", (count-fails), count, (fails == 0 ? "OK" : "ERROR"));
nkeynes@1205
    96
    return fails == 0 ? TRUE : FALSE;
nkeynes@1205
    97
nkeynes@1205
    98
    /* FIXME: Should probably restore the env state, but doesn't matter at the moment */
nkeynes@1205
    99
}
nkeynes@1205
   100
nkeynes@1205
   101
int main()
nkeynes@1205
   102
{
nkeynes@1205
   103
    gboolean result = TRUE;
nkeynes@1205
   104
    result =  test_get_expanded_path() && result;
nkeynes@1205
   105
    
nkeynes@1205
   106
    return result ? 0 : 1;
nkeynes@1205
   107
}
.