Changeset 544

Show
Ignore:
Timestamp:
08/18/09 08:34:25 (15 years ago)
Author:
vbarta
Message:

corrected dynamic memory handling in UI::from_setting, more UIException tests

Location:
library
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/user_info.h

    r540 r544  
    331331                try { 
    332332                        instance->from_setting ( link.result ); 
    333                 } catch ( SettingException sttng_xcptn ) { 
     333                } catch ( SettingException &sttng_xcptn ) { 
     334                        delete instance; 
     335                        instance = 0; 
    334336                        string msg = "UIException: method "; 
    335337                        msg += class_name; 
    336338                        msg += ".from_setting(Setting&) has thrown a SettingException."; 
    337339                        throw UISettingException(msg, sttng_xcptn.getPath()); 
     340                } catch (...) { 
     341                        delete instance; 
     342                        instance = 0; 
     343                        throw; 
    338344                } 
    339345        } 
     
    521527                try { 
    522528                        instance->to_setting ( set ); 
    523                 } catch ( SettingException sttng_xcptn ) { 
     529                } catch ( SettingException &sttng_xcptn ) { 
    524530                        string msg = "UIException: method "; 
    525531                        msg += class_name; 
  • library/tests/user_info_test.cpp

    r542 r544  
    11#include <string> 
     2#include <vector> 
    23#include <string.h> 
    34#include "base/user_info.h" 
     
    7677        string manufacturer; 
    7778 
    78         Transport() { 
    79                 year = 1900; 
    80                 manufacturer = "unknown"; 
    81         } 
    82  
    83         Transport ( int year, string manufacturer ) 
    84                         : year ( year ), manufacturer ( manufacturer ) { 
     79        Transport() : year( 1900 ), manufacturer( "unknown" ) { 
     80        } 
     81 
     82        Transport ( int year, string manufacturer ) : 
     83                year ( year ), manufacturer ( manufacturer ) { 
    8584        } 
    8685 
     
    189188UIREGISTER ( Bike ); 
    190189 
     190class Teleport : public Transport { 
     191public: 
     192        void to_setting ( Setting &set ) const { 
     193                set.add ( "this throws", Setting::TypeNone ); 
     194        } 
     195}; 
     196 
     197UIREGISTER ( Teleport ); 
     198 
    191199TEST ( test_load ) { 
    192200        UIFile in ( "test_user_info_input.cfg" ); 
     
    205213        shared_ptr<Transport> kati ( UI::build<Transport> ( in, "kati", UI::compulsory ) ); 
    206214        CHECK_EQUAL ( string ( "A car made in 1980 by vecernicek, having 250000 kilometers on the clock." ), kati->to_string() ); 
     215} 
     216 
     217TEST ( test_load_error ) { 
     218        UIFile in ( "test_user_info_input.cfg" ); 
     219 
     220        try { 
     221                shared_ptr<Robot> failing ( UI::build<Robot> ( in, "elisky", UI::compulsory ) ); 
     222                CHECK ( false ); 
     223        } catch ( UIClassException &exc ) { 
     224                CHECK ( exc.what() ); 
     225        } 
    207226 
    208227        try { 
     
    210229                CHECK ( false ); 
    211230        } catch ( UIException &exc ) { 
     231                CHECK ( exc.what() ); 
     232        } 
     233 
     234        UIFile erroneous ( "erroneous.cfg" ); 
     235        try { 
     236                shared_ptr<Transport> jardovo ( UI::build<Transport> ( erroneous, "jardovo", UI::compulsory ) ); 
     237                CHECK ( false ); 
     238        } catch ( UISettingException &exc ) { 
    212239                CHECK ( exc.what() ); 
    213240        } 
     
    233260        CHECK_EQUAL ( expected, actual ); 
    234261} 
     262 
     263TEST ( test_save_error ) { 
     264        UIFile out; 
     265 
     266        Teleport teleport; 
     267        try { 
     268                UI::save ( &teleport, out, "unsaveable" ); 
     269                CHECK ( false ); 
     270        } catch ( UIException &exc ) { 
     271                CHECK ( exc.what() ); 
     272        } 
     273}