00001 #include <eh_utils.h>
00002 #include <check.h>
00003
00004 Eh_project create_test_project( )
00005 {
00006 char* proj_dir_name = "/tmp/project_test";
00007 char* proj_full_name = tempnam( proj_dir_name , "TEST_" );
00008 char* proj_name = g_path_get_basename( proj_full_name );
00009 Eh_project p = eh_create_project( proj_name );
00010
00011 eh_set_project_dir( p , proj_dir_name );
00012
00013
00014
00015
00016
00017 return p;
00018 }
00019
00020 START_TEST ( test_create_project )
00021 {
00022 Eh_project p;
00023
00024 p = eh_create_project( "TEST_PROJECT" );
00025
00026 fail_unless( strcmp(eh_project_name(p),"TEST_PROJECT")==0 ,
00027 "Wrong project name" );
00028
00029 fail_unless( eh_project_dir(p)==NULL ,
00030 "Directory should be set to NULL" );
00031
00032 fail_unless( eh_project_dir_name(p)==NULL ,
00033 "Directory name should be set to NULL" );
00034
00035 p = eh_destroy_project( p );
00036 }
00037 END_TEST
00038
00039 START_TEST ( test_set_project_dir )
00040 {
00041 Eh_project p;
00042 FILE *fp;
00043 int n;
00044
00045 p = eh_create_project( "TEST_PROJECT" );
00046
00047 eh_set_project_dir( p , "/tmp/project_test" );
00048
00049 fp = fopen( "/tmp/project_test/test.txt" , "w" );
00050 fail_if( fp==NULL , "Error opening file in working directory" );
00051
00052 n = fprintf( fp , "This is a test" );
00053 fail_unless( n==14 , "Error writing to project file" );
00054
00055 fclose( fp );
00056
00057 p = eh_destroy_project( p );
00058 }
00059 END_TEST
00060
00061 START_TEST ( test_project_add_info_entry )
00062 {
00063 gchar* info_file_name;
00064 gchar* proj_name;
00065
00066 {
00067 Eh_project p = create_test_project( proj_name );
00068
00069 eh_project_add_info_val( p , "TEST KEY" , "TEST VALUE" );
00070
00071 eh_project_add_info_val( p , "TEST KEY WITH EQUAL" , "x=1" );
00072
00073 eh_project_add_info_val( p , "KEY LIST" , "1" );
00074 eh_project_add_info_val( p , "KEY LIST" , "2" );
00075 eh_project_add_info_val( p , "KEY LIST" , "3" );
00076
00077 eh_write_project_info_file( p );
00078
00079 info_file_name = eh_project_info_file_full_name( p );
00080 proj_name = g_strdup( eh_project_name(p) );
00081
00082
00083 p = eh_destroy_project( p );
00084 }
00085
00086 {
00087 GKeyFile* info_file = g_key_file_new();
00088 char* group_name = g_strconcat( proj_name ,
00089 " info entry" ,
00090 NULL );
00091 char* value;
00092 gint* list;
00093 gsize len;
00094
00095 g_key_file_load_from_file( info_file ,
00096 info_file_name ,
00097 G_KEY_FILE_NONE ,
00098 NULL );
00099
00100 fail_unless( g_key_file_has_key( info_file ,
00101 group_name ,
00102 "TEST KEY" ,
00103 NULL ) ,
00104 "Missing key 'TEST KEY'" );
00105 value = g_key_file_get_value( info_file ,
00106 group_name ,
00107 "TEST KEY" ,
00108 NULL );
00109 fail_unless( strcmp( value , "TEST VALUE" )==0 ,
00110 "Value written incorrectly" );
00111
00112 eh_free( value );
00113
00114 fail_unless( g_key_file_has_key( info_file ,
00115 group_name ,
00116 "TEST KEY WITH EQUAL" ,
00117 NULL ) ,
00118 "Missing key 'TEST KEY WITH EQUAL'" );
00119 value = g_key_file_get_value( info_file ,
00120 group_name ,
00121 "TEST KEY WITH EQUAL" ,
00122 NULL );
00123 fail_unless( strcmp( value , "x=1" )==0 ,
00124 "Value written incorrectly" );
00125
00126 eh_free( value );
00127
00128 fail_unless( g_key_file_has_key( info_file ,
00129 group_name ,
00130 "KEY LIST" ,
00131 NULL ) ,
00132 "Missing key 'KEY LIST'" );
00133
00134 list = g_key_file_get_integer_list( info_file ,
00135 group_name ,
00136 "KEY LIST" ,
00137 &len ,
00138 NULL );
00139
00140 fail_unless( len==3 , "List length is incorrect" );
00141 fail_unless( list[0]==1 , "Fist element set incorrectly" );
00142 fail_unless( list[1]==2 , "Second element set incorrectly" );
00143 fail_unless( list[2]==3 , "Third element set incorrectly" );
00144
00145 eh_free( list );
00146 eh_free( group_name );
00147
00148 g_key_file_free( info_file );
00149 }
00150
00151 eh_free( info_file_name );
00152 eh_free( proj_name );
00153 }
00154 END_TEST
00155
00156 START_TEST ( test_project_info_file )
00157 {
00158 gchar* info_file_name;
00159 gchar* proj_name;
00160
00161 {
00162 Eh_project p = create_test_project( );
00163
00164 eh_write_project_info_file( p );
00165
00166 info_file_name = eh_project_info_file_full_name( p );
00167 proj_name = g_strdup( eh_project_name(p) );
00168
00169 p = eh_destroy_project( p );
00170 }
00171
00172 {
00173 GKeyFile* info_file = g_key_file_new();
00174 char* group_name = g_strconcat( proj_name ,
00175 " info entry" ,
00176 NULL );
00177
00178 fail_unless( g_file_test( info_file_name , G_FILE_TEST_EXISTS ) ,
00179 "Info file does not exist where it should" );
00180
00181 fail_unless( g_key_file_load_from_file( info_file ,
00182 info_file_name ,
00183 G_KEY_FILE_NONE ,
00184 NULL ) ,
00185 "Error loading info file" );
00186
00187 fail_unless( g_key_file_has_group( info_file , group_name ) ,
00188 "Group (%s) is missing" , group_name );
00189
00190 fail_unless( g_key_file_has_key( info_file ,
00191 group_name ,
00192 "RUN START DATE" ,
00193 NULL ) ,
00194 "Missing key 'RUN START DATE'" );
00195 fail_unless( g_key_file_has_key( info_file ,
00196 group_name ,
00197 "RUN START TIME" ,
00198 NULL ) ,
00199 "Missing key 'RUN START TIME'" );
00200 fail_unless( g_key_file_has_key( info_file ,
00201 group_name ,
00202 "USER" ,
00203 NULL ) ,
00204 "Missing key 'USER'" );
00205 fail_unless( g_key_file_has_key( info_file ,
00206 group_name ,
00207 "HOST" ,
00208 NULL ) ,
00209 "Missing key 'HOST'" );
00210 fail_unless( g_key_file_has_key( info_file ,
00211 group_name ,
00212 "RUN DIRECTORY" ,
00213 NULL ) ,
00214 "Missing key 'RUN DIRECTORY'" );
00215
00216 eh_free( group_name );
00217
00218 g_key_file_free( info_file );
00219 }
00220
00221 eh_free( info_file_name );
00222 eh_free( proj_name );
00223 }
00224 END_TEST
00225
00226 START_TEST ( test_project_load_info_file )
00227 {
00228 gchar* info_file;
00229
00230 {
00231 Eh_project p = create_test_project( );
00232
00233 eh_project_add_info_val( p , "TEST KEY" , "TEST VALUE" );
00234
00235 eh_write_project_info_file( p );
00236
00237 info_file = eh_project_info_file_full_name( p );
00238
00239 p = eh_destroy_project( p );
00240 }
00241
00242 {
00243 Eh_project p = eh_load_project( "WRONG NAME" );
00244
00245 fail_unless( p==NULL , "eh_load_project should return NULL on error." );
00246 }
00247
00248 {
00249 Eh_project p = eh_load_project( info_file );
00250 gchar* value;
00251
00252 fail_if( p==NULL , "Info file did not load correctly" );
00253
00254 value = eh_project_get_info_val( p , "TEST KEY" );
00255
00256 fail_unless( strcmp( value , "TEST VALUE" )==0 ,
00257 "Value loaded incorrectly" );
00258
00259 p = eh_destroy_project( p );
00260
00261 eh_free( value );
00262 }
00263
00264 eh_free( info_file );
00265
00266 }
00267 END_TEST
00268
00269 START_TEST ( test_project_set_info_val )
00270 {
00271 Eh_project p;
00272 gchar* value;
00273
00274 p = create_test_project( );
00275
00276 eh_project_set_info_val( p , "TEST VAL" , "1" );
00277
00278 value = eh_project_get_info_val( p , "TEST VAL" );
00279
00280 fail_unless( strcmp( value , "1" )==0 ,
00281 "Info value set incorrectly" );
00282
00283 eh_free( value );
00284
00285 eh_project_set_info_val( p , "TEST VAL" , "2" );
00286
00287 value = eh_project_get_info_val( p , "TEST VAL" );
00288
00289 fail_unless( strcmp( value , "2" )==0 ,
00290 "Info value reset incorrectly" );
00291
00292 eh_free( value );
00293
00294 eh_destroy_project( p );
00295
00296 }
00297 END_TEST
00298
00299 START_TEST ( test_project_get_info_val )
00300 {
00301 Eh_project p;
00302 gchar* value;
00303 const gchar* name = g_get_user_name();
00304
00305 p = create_test_project( );
00306
00307 value = eh_project_get_info_val( p , "GARBAGE" );
00308
00309 fail_unless( value==NULL , "Value should be NULL if key doesn't exist." );
00310
00311 value = eh_project_get_info_val( p , "USER" );
00312
00313 fail_unless( strcmp( value , name )==0 ,
00314 "Incorrect info value retrieved." );
00315
00316 eh_free( value );
00317
00318 eh_destroy_project( p );
00319 }
00320 END_TEST
00321
00322 Suite *project_suite( void )
00323 {
00324 Suite *s = suite_create( "Project" );
00325 TCase *test_case_core = tcase_create( "Core" );
00326
00327 suite_add_tcase( s , test_case_core );
00328
00329 tcase_add_test( test_case_core , test_create_project );
00330 tcase_add_test( test_case_core , test_set_project_dir );
00331 tcase_add_test( test_case_core , test_project_info_file );
00332 tcase_add_test( test_case_core , test_project_add_info_entry );
00333 tcase_add_test( test_case_core , test_project_load_info_file );
00334 tcase_add_test( test_case_core , test_project_set_info_val );
00335 tcase_add_test( test_case_core , test_project_get_info_val );
00336
00337 return s;
00338 }
00339
00340 int test_project( void )
00341 {
00342 int n;
00343
00344 {
00345 Suite *s_project = project_suite();
00346 SRunner *sr_project = srunner_create( s_project );
00347
00348 srunner_run_all( sr_project , CK_NORMAL );
00349 n = srunner_ntests_failed( sr_project );
00350 srunner_free( sr_project );
00351 }
00352
00353 return n;
00354 }
00355
00356