[344] | 1 | #include <string> |
---|
[544] | 2 | #include <vector> |
---|
[418] | 3 | #include <string.h> |
---|
[386] | 4 | #include "base/user_info.h" |
---|
[717] | 5 | #include "../test_util.h" |
---|
[418] | 6 | #include "UnitTest++.h" |
---|
[244] | 7 | |
---|
| 8 | using namespace std; |
---|
[254] | 9 | using namespace bdm; |
---|
[244] | 10 | |
---|
[471] | 11 | class Passenger : public root { |
---|
[351] | 12 | public: |
---|
| 13 | |
---|
[471] | 14 | Passenger() { |
---|
| 15 | } |
---|
[351] | 16 | }; |
---|
| 17 | |
---|
[471] | 18 | class Human : public Passenger { |
---|
[351] | 19 | public: |
---|
[471] | 20 | string name; |
---|
[351] | 21 | |
---|
[471] | 22 | Human() { |
---|
| 23 | name = "none"; |
---|
| 24 | } |
---|
[351] | 25 | |
---|
[471] | 26 | virtual void from_setting ( const Setting &set ) { |
---|
| 27 | UI::get ( name, set, "name" ); |
---|
| 28 | } |
---|
[351] | 29 | |
---|
[471] | 30 | virtual void to_setting ( Setting &set ) const { |
---|
| 31 | Setting &name_setting = set.add ( "name", Setting::TypeString ); |
---|
| 32 | name_setting = name; |
---|
| 33 | } |
---|
[351] | 34 | |
---|
[674] | 35 | string to_string() const { |
---|
[471] | 36 | return name; |
---|
| 37 | } |
---|
[351] | 38 | }; |
---|
| 39 | |
---|
[471] | 40 | UIREGISTER ( Human ); |
---|
[351] | 41 | |
---|
[471] | 42 | class Robot : public Passenger { |
---|
[351] | 43 | public: |
---|
[471] | 44 | int number; |
---|
| 45 | Array<string> software; |
---|
[351] | 46 | |
---|
[471] | 47 | Robot() : software() { |
---|
| 48 | number = -1; |
---|
| 49 | } |
---|
[351] | 50 | |
---|
[471] | 51 | virtual void from_setting ( const Setting &set ) { |
---|
| 52 | UI::get ( number, set, "number" ); |
---|
| 53 | UI::get ( software, set, "software" ); |
---|
| 54 | } |
---|
[351] | 55 | |
---|
[471] | 56 | virtual void to_setting ( Setting &set ) const { |
---|
| 57 | Setting &number_setting = set.add ( "number", Setting::TypeInt ); |
---|
| 58 | number_setting = number; |
---|
[351] | 59 | |
---|
[471] | 60 | UI::save ( software, set, "software" ); |
---|
| 61 | } |
---|
[351] | 62 | |
---|
[674] | 63 | string to_string() const { |
---|
[471] | 64 | stringstream stream; |
---|
| 65 | stream << number; |
---|
| 66 | for ( int i = 0; i < software.length(); i++ ) |
---|
| 67 | stream << "_" + software ( i ); |
---|
| 68 | return stream.str(); |
---|
| 69 | } |
---|
[351] | 70 | }; |
---|
| 71 | |
---|
[471] | 72 | UIREGISTER ( Robot ); |
---|
[351] | 73 | |
---|
[471] | 74 | class Transport : public root { |
---|
[344] | 75 | public: |
---|
[471] | 76 | int year; |
---|
| 77 | string manufacturer; |
---|
[344] | 78 | |
---|
[544] | 79 | Transport() : year( 1900 ), manufacturer( "unknown" ) { |
---|
[471] | 80 | } |
---|
[344] | 81 | |
---|
[544] | 82 | Transport ( int year, string manufacturer ) : |
---|
| 83 | year ( year ), manufacturer ( manufacturer ) { |
---|
[471] | 84 | } |
---|
[344] | 85 | |
---|
[471] | 86 | virtual void from_setting ( const Setting &set ) { |
---|
| 87 | UI::get ( year, set, "year" ); |
---|
| 88 | UI::get ( manufacturer, set, "manufacturer" ); |
---|
| 89 | } |
---|
[344] | 90 | |
---|
[471] | 91 | virtual void to_setting ( Setting &set ) const { |
---|
| 92 | Setting &year_setting = set.add ( "year", Setting::TypeInt ); |
---|
| 93 | year_setting = year; |
---|
[344] | 94 | |
---|
[471] | 95 | Setting &manufacturer_setting = set.add ( "manufacturer", Setting::TypeString ); |
---|
| 96 | manufacturer_setting = manufacturer; |
---|
| 97 | } |
---|
[244] | 98 | }; |
---|
| 99 | |
---|
[471] | 100 | class Car : public Transport { |
---|
[344] | 101 | public: |
---|
[471] | 102 | int kilometers; |
---|
[527] | 103 | Array<shared_ptr<Passenger> > passengers; |
---|
[344] | 104 | |
---|
[471] | 105 | Car() : Transport() { |
---|
| 106 | kilometers = 0; |
---|
| 107 | } |
---|
[344] | 108 | |
---|
| 109 | |
---|
[471] | 110 | Car ( int year, string manufacturer, int kilometers ) |
---|
| 111 | : Transport ( year, manufacturer ), kilometers ( kilometers ) { |
---|
| 112 | } |
---|
[344] | 113 | |
---|
[471] | 114 | virtual void from_setting ( const Setting &set ) { |
---|
| 115 | Transport::from_setting ( set ); |
---|
[344] | 116 | |
---|
[471] | 117 | UI::get ( kilometers, set, "kilometers" ); |
---|
| 118 | UI::get ( passengers, set, "passengers" ); |
---|
| 119 | } |
---|
[351] | 120 | |
---|
[471] | 121 | virtual void to_setting ( Setting &set ) const { |
---|
| 122 | Transport::to_setting ( set ); |
---|
[344] | 123 | |
---|
[471] | 124 | Setting &kilometers_setting = set.add ( "kilometers", Setting::TypeInt ); |
---|
| 125 | kilometers_setting = kilometers; |
---|
[344] | 126 | |
---|
[471] | 127 | UI::save ( passengers, set, "passengers" ); |
---|
| 128 | } |
---|
[351] | 129 | |
---|
[674] | 130 | string to_string() const { |
---|
[471] | 131 | stringstream stream; |
---|
| 132 | stream << "A car made in " << year << " by " << manufacturer << ", having " << kilometers << " kilometers on the clock."; |
---|
| 133 | if ( passengers.length() ) { |
---|
| 134 | stream << "The names of passengers are as follows: "; |
---|
| 135 | for ( int i = 0; i < passengers.length(); i++ ) |
---|
| 136 | stream << passengers ( i )->to_string() << " "; |
---|
| 137 | } |
---|
| 138 | return stream.str(); |
---|
| 139 | } |
---|
[244] | 140 | }; |
---|
| 141 | |
---|
[471] | 142 | UIREGISTER ( Car ); |
---|
[344] | 143 | |
---|
[471] | 144 | class Bike : public Transport { |
---|
[344] | 145 | public: |
---|
[471] | 146 | bool electricLights; |
---|
| 147 | mat matr; |
---|
[344] | 148 | |
---|
[471] | 149 | Bike() : Transport(), matr ( "2,2;3,4" ) { |
---|
| 150 | electricLights = false; |
---|
| 151 | } |
---|
[344] | 152 | |
---|
[471] | 153 | Bike ( int year, string manufacturer, bool electricLights ) |
---|
| 154 | : Transport ( year, manufacturer ), electricLights ( electricLights ) { |
---|
| 155 | } |
---|
[344] | 156 | |
---|
[471] | 157 | ~Bike() { |
---|
| 158 | } |
---|
[344] | 159 | |
---|
[471] | 160 | void from_setting ( const Setting &set ) { |
---|
| 161 | Transport::from_setting ( set ); |
---|
[344] | 162 | |
---|
[480] | 163 | int elights; |
---|
| 164 | if ( UI::get ( elights, set, "electricLights", UI::optional ) ) { |
---|
| 165 | electricLights = !!elights; |
---|
| 166 | } |
---|
[344] | 167 | |
---|
[471] | 168 | UI::get ( matr, set, "matr" ); |
---|
| 169 | } |
---|
[344] | 170 | |
---|
[471] | 171 | void to_setting ( Setting &set ) const { |
---|
| 172 | Transport::to_setting ( set ); |
---|
[344] | 173 | |
---|
[471] | 174 | Setting &electricLights_setting = set.add ( "electricLights", Setting::TypeBoolean ); |
---|
| 175 | electricLights_setting = electricLights; |
---|
[344] | 176 | |
---|
[471] | 177 | UI::save ( matr, set, "matr" ); |
---|
| 178 | } |
---|
[344] | 179 | |
---|
[674] | 180 | string to_string() const { |
---|
[471] | 181 | stringstream stream; |
---|
| 182 | stream << "a bike made in " << year << " by " << manufacturer; |
---|
| 183 | if ( electricLights ) stream << " with electric lights included"; |
---|
| 184 | return stream.str(); |
---|
| 185 | } |
---|
[244] | 186 | }; |
---|
| 187 | |
---|
[471] | 188 | UIREGISTER ( Bike ); |
---|
[244] | 189 | |
---|
[544] | 190 | class Teleport : public Transport { |
---|
| 191 | public: |
---|
| 192 | void to_setting ( Setting &set ) const { |
---|
| 193 | set.add ( "this throws", Setting::TypeNone ); |
---|
| 194 | } |
---|
| 195 | }; |
---|
| 196 | |
---|
| 197 | UIREGISTER ( Teleport ); |
---|
| 198 | |
---|
[689] | 199 | TEST ( load_test ) { |
---|
[717] | 200 | UIFile in ( "user_info_valid.cfg" ); |
---|
[527] | 201 | shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); |
---|
[471] | 202 | CHECK_EQUAL ( string ( "A car made in 1998 by audi, having 25000 kilometers on the clock.The names of passengers are as follows: Karlos Novak -1_CygWin_Matlab_Aimsun Karlosik Novacek " ), pepikovo->to_string() ); |
---|
[351] | 203 | |
---|
[527] | 204 | shared_ptr<Transport> jardovo ( UI::build<Transport> ( in, "jardovo", UI::compulsory ) ); |
---|
[471] | 205 | CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), jardovo->to_string() ); |
---|
[351] | 206 | |
---|
[527] | 207 | shared_ptr<Transport> ondrejovo ( UI::build<Transport> ( in, "ondrejovo", UI::compulsory ) ); |
---|
[471] | 208 | CHECK_EQUAL ( string ( "a bike made in 1996 by author with electric lights included" ), ondrejovo->to_string() ); |
---|
[418] | 209 | |
---|
[527] | 210 | shared_ptr<Transport> elisky ( UI::build<Transport> ( in, "elisky", UI::compulsory ) ); |
---|
[471] | 211 | CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), elisky->to_string() ); |
---|
[418] | 212 | |
---|
[527] | 213 | shared_ptr<Transport> kati ( UI::build<Transport> ( in, "kati", UI::compulsory ) ); |
---|
[471] | 214 | CHECK_EQUAL ( string ( "A car made in 1980 by vecernicek, having 250000 kilometers on the clock." ), kati->to_string() ); |
---|
[544] | 215 | } |
---|
[542] | 216 | |
---|
[689] | 217 | TEST ( load_error_test ) { |
---|
[717] | 218 | UIFile in ( "user_info_valid.cfg" ); |
---|
[544] | 219 | |
---|
[542] | 220 | try { |
---|
[544] | 221 | shared_ptr<Robot> failing ( UI::build<Robot> ( in, "elisky", UI::compulsory ) ); |
---|
| 222 | CHECK ( false ); |
---|
| 223 | } catch ( UIClassException &exc ) { |
---|
| 224 | CHECK ( exc.what() ); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | try { |
---|
[542] | 228 | shared_ptr<Robot> failing ( UI::build<Robot> ( in, "kati", UI::compulsory ) ); |
---|
| 229 | CHECK ( false ); |
---|
| 230 | } catch ( UIException &exc ) { |
---|
| 231 | CHECK ( exc.what() ); |
---|
| 232 | } |
---|
[544] | 233 | |
---|
[717] | 234 | UIFile erroneous ( "user_info_erroneous.cfg" ); |
---|
[544] | 235 | try { |
---|
| 236 | shared_ptr<Transport> jardovo ( UI::build<Transport> ( erroneous, "jardovo", UI::compulsory ) ); |
---|
| 237 | CHECK ( false ); |
---|
| 238 | } catch ( UISettingException &exc ) { |
---|
| 239 | CHECK ( exc.what() ); |
---|
| 240 | } |
---|
[418] | 241 | } |
---|
| 242 | |
---|
[689] | 243 | TEST ( save_test ) { |
---|
[717] | 244 | UIFile in ( "user_info_valid.cfg" ); |
---|
[527] | 245 | shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); |
---|
[418] | 246 | |
---|
[471] | 247 | Car audi ( 1968, "zyl", 200 ); |
---|
| 248 | Car liaz ( 1989, "skoda", 1000 ); |
---|
| 249 | Bike author ( 2001, "noname", false ); |
---|
| 250 | UIFile out; |
---|
[351] | 251 | |
---|
[471] | 252 | UI::save ( &audi, out, "marty" ); |
---|
| 253 | UI::save ( &liaz, out, "bohousovo" ); |
---|
| 254 | UI::save ( &author, out, "karlovo" ); |
---|
[527] | 255 | UI::save ( pepikovo, out, "pepikovo" ); |
---|
[721] | 256 | out.save ( "user_info_output.cfg" ); |
---|
[351] | 257 | |
---|
[721] | 258 | string expected ( load_test_file ( "user_info_matrix.cfg" ) ); |
---|
| 259 | string actual ( load_test_file ( "user_info_output.cfg" ) ); |
---|
[471] | 260 | CHECK_EQUAL ( expected, actual ); |
---|
[244] | 261 | } |
---|
[544] | 262 | |
---|
[689] | 263 | |
---|
| 264 | TEST ( save_error_test ) { |
---|
[544] | 265 | UIFile out; |
---|
| 266 | |
---|
| 267 | Teleport teleport; |
---|
| 268 | try { |
---|
| 269 | UI::save ( &teleport, out, "unsaveable" ); |
---|
| 270 | CHECK ( false ); |
---|
| 271 | } catch ( UIException &exc ) { |
---|
| 272 | CHECK ( exc.what() ); |
---|
| 273 | } |
---|
| 274 | } |
---|