tests/plover/test-util.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jun 24 17:48:44 2016 +0100 (2016-06-24)
changeset 47 2d0ee44ab3c6
permissions -rw-r--r--
Remove debugging
ali@38
     1
/*
ali@38
     2
 * Copyright (C) 2016  J. Ali Harlow <ali@juiblex.co.uk>
ali@38
     3
 *
ali@38
     4
 * This program is free software; you can redistribute it and/or modify
ali@38
     5
 * it under the terms of the GNU General Public License as published by
ali@38
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@38
     7
 * (at your option) any later version.
ali@38
     8
 *
ali@38
     9
 * This program is distributed in the hope that it will be useful,
ali@38
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@38
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@38
    12
 * GNU General Public License for more details.
ali@38
    13
 *
ali@38
    14
 * You should have received a copy of the GNU General Public License along
ali@38
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@38
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@38
    17
 */
ali@38
    18
ali@38
    19
#include <stdlib.h>
ali@38
    20
#ifdef WIN32
ali@38
    21
#include <windows.h>
ali@38
    22
#endif
ali@38
    23
#include <sys/time.h>
ali@38
    24
#include <sys/errno.h>
ali@38
    25
#include <glib.h>
ali@38
    26
#include <zlib.h>
ali@38
    27
#include <razor.h>
ali@38
    28
#include <plover/plover.h>
ali@38
    29
ali@38
    30
static void test_pre_install_prefix(void)
ali@38
    31
{
ali@38
    32
    gchar *pre_install_prefix;
ali@38
    33
    pre_install_prefix=plover_pre_install_prefix();
ali@38
    34
    g_assert_cmpstr(pre_install_prefix,!=,NULL);
ali@38
    35
    g_assert_cmpstr(pre_install_prefix,!=,"");
ali@38
    36
    g_free(pre_install_prefix);
ali@38
    37
}
ali@38
    38
ali@38
    39
static void test_reports_directory(void)
ali@38
    40
{
ali@38
    41
    gchar *reports_directory;
ali@38
    42
    reports_directory=plover_get_reports_directory();
ali@38
    43
    g_assert_cmpstr(reports_directory,!=,NULL);
ali@38
    44
    g_assert_cmpstr(reports_directory,!=,"");
ali@38
    45
    g_free(reports_directory);
ali@38
    46
}
ali@38
    47
ali@38
    48
static void test_program_directory(void)
ali@38
    49
{
ali@38
    50
    gchar *program_directory;
ali@38
    51
    program_directory=plover_get_program_directory("test-util");
ali@38
    52
    g_assert_cmpstr(program_directory,!=,NULL);
ali@38
    53
    g_assert_cmpstr(program_directory,!=,"");
ali@38
    54
    free(program_directory);
ali@38
    55
    program_directory=plover_get_program_directory("./test-util");
ali@38
    56
    g_assert_cmpstr(program_directory,!=,NULL);
ali@38
    57
    g_assert_cmpstr(program_directory,!=,"");
ali@38
    58
    free(program_directory);
ali@38
    59
}
ali@38
    60
ali@38
    61
static void verify_error_propagation(int domain,int code)
ali@38
    62
{
ali@38
    63
    GError *src,*dest=NULL;
ali@38
    64
    struct razor_error *tmp=NULL;
ali@38
    65
    src=g_error_new_literal(domain,code,"test error");
ali@38
    66
    plover_propagate_g_error(&tmp,src);
ali@38
    67
    g_assert(tmp!=NULL);
ali@38
    68
    plover_propagate_razor_error(&dest,tmp);
ali@38
    69
    g_assert(dest!=NULL);
ali@38
    70
    g_assert_cmpint(dest->domain,==,domain);
ali@38
    71
    g_assert_cmpint(dest->code,==,code);
ali@38
    72
    g_assert_cmpstr(dest->message,==,"test error");
ali@38
    73
    g_error_free(dest);
ali@38
    74
}
ali@38
    75
ali@38
    76
static void test_propagate_razor_error(void)
ali@38
    77
{
ali@38
    78
    verify_error_propagation(PLOVER_RAZOR_ERROR,RAZOR_GENERAL_ERROR_FAILED);
ali@38
    79
}
ali@38
    80
ali@38
    81
static void test_propagate_posix_error(void)
ali@38
    82
{
ali@38
    83
    verify_error_propagation(PLOVER_POSIX_ERROR,ENOENT);
ali@38
    84
}
ali@38
    85
ali@38
    86
static void test_propagate_mswin_error(void)
ali@38
    87
{
ali@38
    88
#ifdef WIN32
ali@38
    89
    verify_error_propagation(PLOVER_MSWIN_ERROR,ERROR_NOT_SUPPORTED);
ali@38
    90
#else
ali@38
    91
    verify_error_propagation(PLOVER_MSWIN_ERROR,0);
ali@38
    92
#endif
ali@38
    93
}
ali@38
    94
ali@38
    95
static void test_propagate_zlib_error(void)
ali@38
    96
{
ali@38
    97
    verify_error_propagation(PLOVER_ZLIB_ERROR,Z_VERSION_ERROR);
ali@38
    98
}
ali@38
    99
ali@38
   100
static void test_propagate_cancelled(void)
ali@38
   101
{
ali@38
   102
    verify_error_propagation(G_IO_ERROR,G_IO_ERROR_CANCELLED);
ali@38
   103
}
ali@38
   104
ali@38
   105
static void test_propagate_other_error(void)
ali@38
   106
{
ali@38
   107
    GError *src,*dest=NULL;
ali@38
   108
    struct razor_error *tmp=NULL;
ali@38
   109
    src=g_error_new_literal(G_SHELL_ERROR,G_SHELL_ERROR_FAILED,"test error");
ali@38
   110
    plover_propagate_g_error(&tmp,src);
ali@38
   111
    g_assert(tmp!=NULL);
ali@38
   112
    plover_propagate_razor_error(&dest,tmp);
ali@38
   113
    g_assert(dest!=NULL);
ali@38
   114
    g_assert_cmpint(dest->domain,==,PLOVER_RAZOR_ERROR);
ali@38
   115
    g_assert_cmpint(dest->code,==,RAZOR_GENERAL_ERROR_FAILED);
ali@38
   116
    g_assert_cmpstr(dest->message,==,"test error");
ali@38
   117
    g_error_free(dest);
ali@38
   118
}
ali@38
   119
ali@38
   120
int main(int argc,char **argv)
ali@38
   121
{
ali@38
   122
    int retval;
ali@38
   123
    g_test_init(&argc,&argv,NULL);
ali@38
   124
    g_test_bug_base("mailto:ali@juiblex.co.uk");
ali@38
   125
    g_test_add_func("/util/pre-install-prefix",test_pre_install_prefix);
ali@38
   126
    g_test_add_func("/util/reports-directory",test_reports_directory);
ali@38
   127
    g_test_add_func("/util/program-directory",test_program_directory);
ali@38
   128
    g_test_add_func("/util/propagate-razor-error",test_propagate_razor_error);
ali@38
   129
    g_test_add_func("/util/propagate-posix-error",test_propagate_posix_error);
ali@38
   130
    g_test_add_func("/util/propagate-mswin-error",test_propagate_mswin_error);
ali@38
   131
    g_test_add_func("/util/propagate-zlib-error",test_propagate_zlib_error);
ali@38
   132
    g_test_add_func("/util/propagate-cancelled",test_propagate_cancelled);
ali@38
   133
    g_test_add_func("/util/propagate-other-error",test_propagate_other_error);
ali@38
   134
    retval=g_test_run();
ali@38
   135
    return retval;
ali@38
   136
}