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