1 | #include <string> |
---|
2 | #include <vector> |
---|
3 | #include <string.h> |
---|
4 | #include "base/user_info.h" |
---|
5 | #include "test_util.h" |
---|
6 | #include "UnitTest++.h" |
---|
7 | |
---|
8 | using namespace std; |
---|
9 | using namespace bdm; |
---|
10 | |
---|
11 | class Passenger : public root { |
---|
12 | public: |
---|
13 | |
---|
14 | Passenger() { |
---|
15 | } |
---|
16 | }; |
---|
17 | |
---|
18 | class Human : public Passenger { |
---|
19 | public: |
---|
20 | string name; |
---|
21 | |
---|
22 | Human() { |
---|
23 | name = "none"; |
---|
24 | } |
---|
25 | |
---|
26 | virtual void from_setting ( const Setting &set ) { |
---|
27 | UI::get ( name, set, "name" ); |
---|
28 | } |
---|
29 | |
---|
30 | virtual void to_setting ( Setting &set ) const { |
---|
31 | Setting &name_setting = set.add ( "name", Setting::TypeString ); |
---|
32 | name_setting = name; |
---|
33 | } |
---|
34 | |
---|
35 | string to_string() { |
---|
36 | return name; |
---|
37 | } |
---|
38 | }; |
---|
39 | |
---|
40 | UIREGISTER ( Human ); |
---|
41 | |
---|
42 | class Robot : public Passenger { |
---|
43 | public: |
---|
44 | int number; |
---|
45 | Array<string> software; |
---|
46 | |
---|
47 | Robot() : software() { |
---|
48 | number = -1; |
---|
49 | } |
---|
50 | |
---|
51 | virtual void from_setting ( const Setting &set ) { |
---|
52 | UI::get ( number, set, "number" ); |
---|
53 | UI::get ( software, set, "software" ); |
---|
54 | } |
---|
55 | |
---|
56 | virtual void to_setting ( Setting &set ) const { |
---|
57 | Setting &number_setting = set.add ( "number", Setting::TypeInt ); |
---|
58 | number_setting = number; |
---|
59 | |
---|
60 | UI::save ( software, set, "software" ); |
---|
61 | } |
---|
62 | |
---|
63 | string to_string() { |
---|
64 | stringstream stream; |
---|
65 | stream << number; |
---|
66 | for ( int i = 0; i < software.length(); i++ ) |
---|
67 | stream << "_" + software ( i ); |
---|
68 | return stream.str(); |
---|
69 | } |
---|
70 | }; |
---|
71 | |
---|
72 | UIREGISTER ( Robot ); |
---|
73 | |
---|
74 | class Transport : public root { |
---|
75 | public: |
---|
76 | int year; |
---|
77 | string manufacturer; |
---|
78 | |
---|
79 | Transport() : year( 1900 ), manufacturer( "unknown" ) { |
---|
80 | } |
---|
81 | |
---|
82 | Transport ( int year, string manufacturer ) : |
---|
83 | year ( year ), manufacturer ( manufacturer ) { |
---|
84 | } |
---|
85 | |
---|
86 | virtual void from_setting ( const Setting &set ) { |
---|
87 | UI::get ( year, set, "year" ); |
---|
88 | UI::get ( manufacturer, set, "manufacturer" ); |
---|
89 | } |
---|
90 | |
---|
91 | virtual void to_setting ( Setting &set ) const { |
---|
92 | Setting &year_setting = set.add ( "year", Setting::TypeInt ); |
---|
93 | year_setting = year; |
---|
94 | |
---|
95 | Setting &manufacturer_setting = set.add ( "manufacturer", Setting::TypeString ); |
---|
96 | manufacturer_setting = manufacturer; |
---|
97 | } |
---|
98 | }; |
---|
99 | |
---|
100 | class Car : public Transport { |
---|
101 | public: |
---|
102 | int kilometers; |
---|
103 | Array<shared_ptr<Passenger> > passengers; |
---|
104 | |
---|
105 | Car() : Transport() { |
---|
106 | kilometers = 0; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | Car ( int year, string manufacturer, int kilometers ) |
---|
111 | : Transport ( year, manufacturer ), kilometers ( kilometers ) { |
---|
112 | } |
---|
113 | |
---|
114 | virtual void from_setting ( const Setting &set ) { |
---|
115 | Transport::from_setting ( set ); |
---|
116 | |
---|
117 | UI::get ( kilometers, set, "kilometers" ); |
---|
118 | UI::get ( passengers, set, "passengers" ); |
---|
119 | } |
---|
120 | |
---|
121 | virtual void to_setting ( Setting &set ) const { |
---|
122 | Transport::to_setting ( set ); |
---|
123 | |
---|
124 | Setting &kilometers_setting = set.add ( "kilometers", Setting::TypeInt ); |
---|
125 | kilometers_setting = kilometers; |
---|
126 | |
---|
127 | UI::save ( passengers, set, "passengers" ); |
---|
128 | } |
---|
129 | |
---|
130 | string to_string() { |
---|
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 | } |
---|
140 | }; |
---|
141 | |
---|
142 | UIREGISTER ( Car ); |
---|
143 | |
---|
144 | class Bike : public Transport { |
---|
145 | public: |
---|
146 | bool electricLights; |
---|
147 | mat matr; |
---|
148 | |
---|
149 | Bike() : Transport(), matr ( "2,2;3,4" ) { |
---|
150 | electricLights = false; |
---|
151 | } |
---|
152 | |
---|
153 | Bike ( int year, string manufacturer, bool electricLights ) |
---|
154 | : Transport ( year, manufacturer ), electricLights ( electricLights ) { |
---|
155 | } |
---|
156 | |
---|
157 | ~Bike() { |
---|
158 | } |
---|
159 | |
---|
160 | void from_setting ( const Setting &set ) { |
---|
161 | Transport::from_setting ( set ); |
---|
162 | |
---|
163 | int elights; |
---|
164 | if ( UI::get ( elights, set, "electricLights", UI::optional ) ) { |
---|
165 | electricLights = !!elights; |
---|
166 | } |
---|
167 | |
---|
168 | UI::get ( matr, set, "matr" ); |
---|
169 | } |
---|
170 | |
---|
171 | void to_setting ( Setting &set ) const { |
---|
172 | Transport::to_setting ( set ); |
---|
173 | |
---|
174 | Setting &electricLights_setting = set.add ( "electricLights", Setting::TypeBoolean ); |
---|
175 | electricLights_setting = electricLights; |
---|
176 | |
---|
177 | UI::save ( matr, set, "matr" ); |
---|
178 | } |
---|
179 | |
---|
180 | string to_string() { |
---|
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 | } |
---|
186 | }; |
---|
187 | |
---|
188 | UIREGISTER ( Bike ); |
---|
189 | |
---|
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 | |
---|
199 | TEST ( test_load ) { |
---|
200 | UIFile in ( "test_user_info_input.cfg" ); |
---|
201 | shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); |
---|
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() ); |
---|
203 | |
---|
204 | shared_ptr<Transport> jardovo ( UI::build<Transport> ( in, "jardovo", UI::compulsory ) ); |
---|
205 | CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), jardovo->to_string() ); |
---|
206 | |
---|
207 | shared_ptr<Transport> ondrejovo ( UI::build<Transport> ( in, "ondrejovo", UI::compulsory ) ); |
---|
208 | CHECK_EQUAL ( string ( "a bike made in 1996 by author with electric lights included" ), ondrejovo->to_string() ); |
---|
209 | |
---|
210 | shared_ptr<Transport> elisky ( UI::build<Transport> ( in, "elisky", UI::compulsory ) ); |
---|
211 | CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), elisky->to_string() ); |
---|
212 | |
---|
213 | shared_ptr<Transport> kati ( UI::build<Transport> ( in, "kati", UI::compulsory ) ); |
---|
214 | CHECK_EQUAL ( string ( "A car made in 1980 by vecernicek, having 250000 kilometers on the clock." ), kati->to_string() ); |
---|
215 | } |
---|
216 | |
---|
217 | TEST ( 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 | } |
---|
226 | |
---|
227 | try { |
---|
228 | shared_ptr<Robot> failing ( UI::build<Robot> ( in, "kati", UI::compulsory ) ); |
---|
229 | CHECK ( false ); |
---|
230 | } 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 ) { |
---|
239 | CHECK ( exc.what() ); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | TEST ( test_save ) { |
---|
244 | UIFile in ( "test_user_info_input.cfg" ); |
---|
245 | shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); |
---|
246 | |
---|
247 | Car audi ( 1968, "zyl", 200 ); |
---|
248 | Car liaz ( 1989, "skoda", 1000 ); |
---|
249 | Bike author ( 2001, "noname", false ); |
---|
250 | UIFile out; |
---|
251 | |
---|
252 | UI::save ( &audi, out, "marty" ); |
---|
253 | UI::save ( &liaz, out, "bohousovo" ); |
---|
254 | UI::save ( &author, out, "karlovo" ); |
---|
255 | UI::save ( pepikovo, out, "pepikovo" ); |
---|
256 | out.save ( "testUI_out.cfg" ); |
---|
257 | |
---|
258 | string expected ( load_test_file ( "testUI_out.matrix" ) ); |
---|
259 | string actual ( load_test_file ( "testUI_out.cfg" ) ); |
---|
260 | CHECK_EQUAL ( expected, actual ); |
---|
261 | } |
---|
262 | |
---|
263 | TEST ( 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 | } |
---|