Line data Source code
1 : // Copyright (c) 2011-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/edhttp
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : /** \file
20 : * \brief Verify the weighted_http_string class.
21 : *
22 : * This file implements a tests to verify that the weighted_http_string
23 : * class functions as expected.
24 : */
25 :
26 : // self
27 : //
28 : #include "catch_main.h"
29 :
30 :
31 : // edhttp
32 : //
33 : #include "edhttp/exception.h"
34 : #include "edhttp/weighted_http_string.h"
35 :
36 :
37 :
38 29 : CATCH_TEST_CASE("weighted_http_string", "[string]")
39 : {
40 54 : CATCH_START_SECTION("weighted_http_strings: verify object, except parts")
41 : {
42 2 : edhttp::weighted_http_string locale("en");
43 :
44 : // no error occurred
45 : //
46 1 : CATCH_REQUIRE(locale.error_messages().empty());
47 :
48 : // original string does not change
49 : //
50 1 : CATCH_REQUIRE(locale.get_string() == "en");
51 :
52 : // get_level() with correct and wrong names
53 : //
54 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::DEFAULT_LEVEL()));
55 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), edhttp::string_part::UNDEFINED_LEVEL()));
56 :
57 : // convert back to a string
58 : //
59 1 : CATCH_REQUIRE(locale.to_string() == "en");
60 : }
61 : CATCH_END_SECTION()
62 :
63 54 : CATCH_START_SECTION("weighted_http_strings: verify parts")
64 : {
65 2 : edhttp::weighted_http_string locale("en");
66 :
67 : // make sure the result is 1 part
68 : //
69 1 : edhttp::string_part::vector_t & p(locale.get_parts());
70 1 : CATCH_REQUIRE(p.size() == 1);
71 :
72 : // also get the constant version
73 : //
74 1 : edhttp::string_part::vector_t const & pc(locale.get_parts());
75 1 : CATCH_REQUIRE(pc.size() == 1);
76 :
77 : // now verify that the part is correct
78 : //
79 1 : CATCH_REQUIRE(p[0].get_name() == "en");
80 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
81 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
82 1 : CATCH_REQUIRE(p[0].to_string() == "en");
83 : }
84 : CATCH_END_SECTION()
85 :
86 54 : CATCH_START_SECTION("weighted_http_strings: verify weights top functions")
87 : {
88 2 : edhttp::weighted_http_string locale("en-US,en;q=0.8,fr-FR;q= 0.5,fr;q =0.3");
89 :
90 : // no error occurred
91 : //
92 1 : CATCH_REQUIRE(locale.error_messages().empty());
93 :
94 : // original string does not change
95 : //
96 1 : CATCH_REQUIRE(locale.get_string() == "en-US,en;q=0.8,fr-FR;q= 0.5,fr;q =0.3");
97 :
98 : // get_level() with correct and wrong names
99 : //
100 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en-US"), edhttp::string_part::DEFAULT_LEVEL()));
101 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), 0.8f));
102 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr-FR"), 0.5f));
103 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), 0.3f));
104 :
105 : // convert back to a string
106 : //
107 1 : CATCH_REQUIRE(locale.to_string() == "en-US, en; q=0.8, fr-FR; q=0.5, fr; q=0.3");
108 : }
109 : CATCH_END_SECTION()
110 :
111 54 : CATCH_START_SECTION("weighted_http_strings: verify weight parts")
112 : {
113 2 : edhttp::weighted_http_string locale("en-US,en;q=\"0.8\",fr-FR;q=0.5,fr;q='0.3'");
114 :
115 : // make sure the result is 4 parts
116 : //
117 1 : edhttp::string_part::vector_t & p(locale.get_parts());
118 1 : CATCH_REQUIRE(p.size() == 4);
119 :
120 : // also get the constant version
121 : //
122 1 : edhttp::string_part::vector_t const & pc(locale.get_parts());
123 1 : CATCH_REQUIRE(pc.size() == 4);
124 :
125 : // now verify that the parts are correct
126 : //
127 1 : CATCH_REQUIRE(p[0].get_name() == "en-US");
128 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
129 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
130 1 : CATCH_REQUIRE(p[0].to_string() == "en-US");
131 :
132 1 : CATCH_REQUIRE(p[1].get_name() == "en");
133 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), 0.8f));
134 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
135 1 : CATCH_REQUIRE(p[1].to_string() == "en; q=0.8");
136 :
137 1 : CATCH_REQUIRE(p[2].get_name() == "fr-FR");
138 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), 0.5f));
139 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
140 1 : CATCH_REQUIRE(p[2].to_string() == "fr-FR; q=0.5");
141 :
142 1 : CATCH_REQUIRE(p[3].get_name() == "fr");
143 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[3].get_level(), 0.3f));
144 1 : CATCH_REQUIRE(p[3].get_parameter("test") == "");
145 1 : CATCH_REQUIRE(p[3].to_string() == "fr; q=0.3");
146 :
147 1 : CATCH_REQUIRE(pc[0].get_name() == "en-US");
148 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
149 1 : CATCH_REQUIRE(pc[0].get_parameter("test") == "");
150 1 : CATCH_REQUIRE(pc[0].to_string() == "en-US");
151 :
152 1 : CATCH_REQUIRE(pc[1].get_name() == "en");
153 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[1].get_level(), 0.8f));
154 1 : CATCH_REQUIRE(pc[1].get_parameter("test") == "");
155 1 : CATCH_REQUIRE(pc[1].to_string() == "en; q=0.8");
156 :
157 1 : CATCH_REQUIRE(pc[2].get_name() == "fr-FR");
158 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[2].get_level(), 0.5f));
159 1 : CATCH_REQUIRE(pc[2].get_parameter("test") == "");
160 1 : CATCH_REQUIRE(pc[2].to_string() == "fr-FR; q=0.5");
161 :
162 1 : CATCH_REQUIRE(pc[3].get_name() == "fr");
163 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[3].get_level(), 0.3f));
164 1 : CATCH_REQUIRE(pc[3].get_parameter("test") == "");
165 1 : CATCH_REQUIRE(pc[3].to_string() == "fr; q=0.3");
166 : }
167 : CATCH_END_SECTION()
168 :
169 54 : CATCH_START_SECTION("weighted_http_strings: sort has no effect if weights are equal")
170 : {
171 2 : edhttp::weighted_http_string locale("en-US,en;q=0.8, fr-FR ; q = \"0.5\" ,fr;q=0.3");
172 :
173 1 : locale.sort_by_level();
174 :
175 : // make sure the result is 3 parts
176 : //
177 1 : edhttp::string_part::vector_t & p(locale.get_parts());
178 1 : CATCH_REQUIRE(p.size() == 4);
179 :
180 : // now verify that the parts are correct
181 : //
182 1 : CATCH_REQUIRE(p[0].get_name() == "en-US");
183 1 : CATCH_REQUIRE(p[1].get_name() == "en");
184 1 : CATCH_REQUIRE(p[2].get_name() == "fr-FR");
185 1 : CATCH_REQUIRE(p[3].get_name() == "fr");
186 : }
187 : CATCH_END_SECTION()
188 :
189 54 : CATCH_START_SECTION("weighted_http_strings: verify object")
190 : {
191 2 : edhttp::weighted_http_string locale("de, en, fr");
192 :
193 : // no error occurred
194 : //
195 1 : CATCH_REQUIRE(locale.error_messages().empty());
196 :
197 : // original string does not change
198 : //
199 1 : CATCH_REQUIRE(locale.get_string() == "de, en, fr");
200 :
201 : // get_level() with correct and wrong names
202 : //
203 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::DEFAULT_LEVEL()));
204 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::DEFAULT_LEVEL()));
205 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), edhttp::string_part::DEFAULT_LEVEL()));
206 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::UNDEFINED_LEVEL()));
207 :
208 : // convert back to a string
209 : //
210 1 : CATCH_REQUIRE(locale.to_string() == "de, en, fr");
211 : }
212 : CATCH_END_SECTION()
213 :
214 54 : CATCH_START_SECTION("weighted_http_strings: verify part")
215 : {
216 2 : edhttp::weighted_http_string locale("de, en, fr");
217 :
218 : // make sure the result is 3 parts
219 : //
220 1 : edhttp::string_part::vector_t & p(locale.get_parts());
221 1 : CATCH_REQUIRE(p.size() == 3);
222 :
223 : // also get the constant version
224 : //
225 1 : edhttp::string_part::vector_t const & pc(locale.get_parts());
226 1 : CATCH_REQUIRE(pc.size() == 3);
227 :
228 : // now verify that the parts are correct
229 : //
230 1 : CATCH_REQUIRE(p[0].get_name() == "de");
231 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
232 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
233 1 : CATCH_REQUIRE(p[0].to_string() == "de");
234 :
235 1 : CATCH_REQUIRE(p[1].get_name() == "en");
236 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
237 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
238 1 : CATCH_REQUIRE(p[1].to_string() == "en");
239 :
240 1 : CATCH_REQUIRE(p[2].get_name() == "fr");
241 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
242 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
243 1 : CATCH_REQUIRE(p[2].to_string() == "fr");
244 :
245 1 : CATCH_REQUIRE(pc[0].get_name() == "de");
246 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
247 1 : CATCH_REQUIRE(pc[0].get_parameter("test") == "");
248 1 : CATCH_REQUIRE(pc[0].to_string() == "de");
249 :
250 1 : CATCH_REQUIRE(pc[1].get_name() == "en");
251 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[1].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
252 1 : CATCH_REQUIRE(pc[1].get_parameter("test") == "");
253 1 : CATCH_REQUIRE(pc[1].to_string() == "en");
254 :
255 1 : CATCH_REQUIRE(pc[2].get_name() == "fr");
256 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[2].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
257 1 : CATCH_REQUIRE(pc[2].get_parameter("test") == "");
258 1 : CATCH_REQUIRE(pc[2].to_string() == "fr");
259 : }
260 : CATCH_END_SECTION()
261 :
262 54 : CATCH_START_SECTION("weighted_http_strings: sort has no effect if weights are equal")
263 : {
264 2 : edhttp::weighted_http_string locale("de, en, fr");
265 :
266 1 : locale.sort_by_level();
267 :
268 : // make sure the result is 3 parts
269 : //
270 1 : edhttp::string_part::vector_t & p(locale.get_parts());
271 1 : CATCH_REQUIRE(p.size() == 3);
272 :
273 : // now verify that the parts are correct
274 : //
275 1 : CATCH_REQUIRE(p[0].get_name() == "de");
276 1 : CATCH_REQUIRE(p[1].get_name() == "en");
277 1 : CATCH_REQUIRE(p[2].get_name() == "fr");
278 : }
279 : CATCH_END_SECTION()
280 :
281 54 : CATCH_START_SECTION("weighted_http_strings: verify object")
282 : {
283 2 : edhttp::weighted_http_string locale("fr, za, en");
284 :
285 : // no error occurred
286 : //
287 1 : CATCH_REQUIRE(locale.error_messages().empty());
288 :
289 : // original string does not change
290 : //
291 1 : CATCH_REQUIRE(locale.get_string() == "fr, za, en");
292 :
293 : // get_level() with correct and wrong names
294 : //
295 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), edhttp::string_part::DEFAULT_LEVEL()));
296 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("za"), edhttp::string_part::DEFAULT_LEVEL()));
297 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::DEFAULT_LEVEL()));
298 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::UNDEFINED_LEVEL()));
299 :
300 : // convert back to a string
301 : //
302 1 : CATCH_REQUIRE(locale.to_string() == "fr, za, en");
303 : }
304 : CATCH_END_SECTION()
305 :
306 54 : CATCH_START_SECTION("weighted_http_strings: verify part")
307 : {
308 2 : edhttp::weighted_http_string locale("fr, za, en");
309 :
310 : // make sure the result is 3 parts
311 : //
312 1 : edhttp::string_part::vector_t & p(locale.get_parts());
313 1 : CATCH_REQUIRE(p.size() == 3);
314 :
315 : // also get the constant version
316 : //
317 1 : edhttp::string_part::vector_t const & pc(locale.get_parts());
318 1 : CATCH_REQUIRE(pc.size() == 3);
319 :
320 : // now verify that the parts are correct
321 : //
322 1 : CATCH_REQUIRE(p[0].get_name() == "fr");
323 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
324 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
325 1 : CATCH_REQUIRE(p[0].to_string() == "fr");
326 :
327 1 : CATCH_REQUIRE(p[1].get_name() == "za");
328 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
329 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
330 1 : CATCH_REQUIRE(p[1].to_string() == "za");
331 :
332 1 : CATCH_REQUIRE(p[2].get_name() == "en");
333 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
334 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
335 1 : CATCH_REQUIRE(p[2].to_string() == "en");
336 :
337 1 : CATCH_REQUIRE(pc[0].get_name() == "fr");
338 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
339 1 : CATCH_REQUIRE(pc[0].get_parameter("test") == "");
340 1 : CATCH_REQUIRE(pc[0].to_string() == "fr");
341 :
342 1 : CATCH_REQUIRE(pc[1].get_name() == "za");
343 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[1].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
344 1 : CATCH_REQUIRE(pc[1].get_parameter("test") == "");
345 1 : CATCH_REQUIRE(pc[1].to_string() == "za");
346 :
347 1 : CATCH_REQUIRE(pc[2].get_name() == "en");
348 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[2].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
349 1 : CATCH_REQUIRE(pc[2].get_parameter("test") == "");
350 1 : CATCH_REQUIRE(pc[2].to_string() == "en");
351 : }
352 : CATCH_END_SECTION()
353 :
354 54 : CATCH_START_SECTION("weighted_http_strings: sort has no effect if weights are equal")
355 : {
356 2 : edhttp::weighted_http_string locale("fr, za, en");
357 :
358 1 : locale.sort_by_level();
359 :
360 : // make sure the result is 3 parts
361 : //
362 1 : edhttp::string_part::vector_t & p(locale.get_parts());
363 1 : CATCH_REQUIRE(p.size() == 3);
364 :
365 : // now verify that the parts are correct
366 : //
367 1 : CATCH_REQUIRE(p[0].get_name() == "fr");
368 1 : CATCH_REQUIRE(p[1].get_name() == "za");
369 1 : CATCH_REQUIRE(p[2].get_name() == "en");
370 : }
371 : CATCH_END_SECTION()
372 :
373 54 : CATCH_START_SECTION("weighted_http_strings: verify object")
374 : {
375 2 : edhttp::weighted_http_string locale("fr;q=0, za; q=0.6,en; q=0.4");
376 :
377 : // no error occurred
378 : //
379 1 : CATCH_REQUIRE(locale.error_messages().empty());
380 :
381 : // original string does not change
382 : //
383 1 : CATCH_REQUIRE(locale.get_string() == "fr;q=0, za; q=0.6,en; q=0.4");
384 :
385 : // get_level() with correct and wrong names
386 : //
387 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), 0.0f));
388 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("za"), 0.6f));
389 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), 0.4f));
390 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::UNDEFINED_LEVEL()));
391 :
392 : // convert back to a string
393 : //
394 1 : CATCH_REQUIRE(locale.to_string() == "fr; q=0, za; q=0.6, en; q=0.4");
395 : }
396 : CATCH_END_SECTION()
397 :
398 54 : CATCH_START_SECTION("weighted_http_strings: verify part after copy in non-const and in const")
399 : {
400 2 : edhttp::weighted_http_string locale("fr;q=0, za; q=0.6,en; q=0.4");
401 :
402 : // make sure the result is 3 parts
403 : //
404 1 : edhttp::string_part::vector_t & p(locale.get_parts());
405 1 : CATCH_REQUIRE(p.size() == 3);
406 :
407 : // also get the constant version
408 : //
409 1 : edhttp::string_part::vector_t const & pc(locale.get_parts());
410 1 : CATCH_REQUIRE(pc.size() == 3);
411 :
412 : // now verify that the parts are correct
413 : //
414 1 : CATCH_REQUIRE(p[0].get_name() == "fr");
415 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), 0.0f));
416 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
417 1 : CATCH_REQUIRE(p[0].to_string() == "fr; q=0");
418 :
419 1 : CATCH_REQUIRE(p[1].get_name() == "za");
420 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), 0.6f));
421 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
422 1 : CATCH_REQUIRE(p[1].to_string() == "za; q=0.6");
423 :
424 1 : CATCH_REQUIRE(p[2].get_name() == "en");
425 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), 0.4f));
426 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
427 1 : CATCH_REQUIRE(p[2].to_string() == "en; q=0.4");
428 :
429 1 : CATCH_REQUIRE(pc[0].get_name() == "fr");
430 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[0].get_level(), 0.0f));
431 1 : CATCH_REQUIRE(pc[0].get_parameter("test") == "");
432 1 : CATCH_REQUIRE(pc[0].to_string() == "fr; q=0");
433 :
434 1 : CATCH_REQUIRE(pc[1].get_name() == "za");
435 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[1].get_level(), 0.6f));
436 1 : CATCH_REQUIRE(pc[1].get_parameter("test") == "");
437 1 : CATCH_REQUIRE(pc[1].to_string() == "za; q=0.6");
438 :
439 1 : CATCH_REQUIRE(pc[2].get_name() == "en");
440 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[2].get_level(), 0.4f));
441 1 : CATCH_REQUIRE(pc[2].get_parameter("test") == "");
442 1 : CATCH_REQUIRE(pc[2].to_string() == "en; q=0.4");
443 : }
444 : CATCH_END_SECTION()
445 :
446 54 : CATCH_START_SECTION("weighted_http_strings: sort by level (weight)")
447 : {
448 2 : edhttp::weighted_http_string locale("fr;q=0, za; q=0.6,en; q=0.4");
449 :
450 1 : locale.sort_by_level();
451 :
452 : // make sure the result is 3 parts
453 : //
454 1 : edhttp::string_part::vector_t & p(locale.get_parts());
455 1 : CATCH_REQUIRE(p.size() == 3);
456 :
457 : // now verify that the parts are sorted by level
458 : // "fr; q=0, za; q=0.6, en; q=0.4"
459 : //
460 1 : CATCH_REQUIRE(p[0].get_name() == "za");
461 1 : CATCH_REQUIRE(p[1].get_name() == "en");
462 1 : CATCH_REQUIRE(p[2].get_name() == "fr");
463 :
464 : // convert back to a string in the new order and with spaces
465 : //
466 1 : CATCH_REQUIRE(locale.to_string() == "za; q=0.6, en; q=0.4, fr; q=0");
467 : }
468 : CATCH_END_SECTION()
469 :
470 54 : CATCH_START_SECTION("weighted_http_strings: verify object")
471 : {
472 2 : edhttp::weighted_http_string locale(" fr; q=0, za; q=0.6, en; q=0.4 ");
473 :
474 : // no error occurred
475 : //
476 1 : CATCH_REQUIRE(locale.error_messages().empty());
477 :
478 : // original string does not change
479 : //
480 1 : CATCH_REQUIRE(locale.get_string() == " fr; q=0, za; q=0.6, en; q=0.4 ");
481 :
482 : // get_level() with correct and wrong names
483 : //
484 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), 0.0f));
485 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("za"), 0.6f));
486 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), 0.4f));
487 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), -1.0f));
488 :
489 : // convert back to a string
490 : //
491 1 : CATCH_REQUIRE(locale.to_string() == "fr; q=0, za; q=0.6, en; q=0.4");
492 : }
493 : CATCH_END_SECTION()
494 :
495 54 : CATCH_START_SECTION("weighted_http_strings: verify part")
496 : {
497 2 : edhttp::weighted_http_string locale(" fr; q=0, za; q=0.6, en; q=0.4 ");
498 :
499 : // make sure the result is 3 parts
500 : //
501 1 : edhttp::string_part::vector_t & p(locale.get_parts());
502 1 : CATCH_REQUIRE(p.size() == 3);
503 :
504 : // also get the constant version
505 : //
506 1 : edhttp::string_part::vector_t const & pc(locale.get_parts());
507 1 : CATCH_REQUIRE(pc.size() == 3);
508 :
509 : // now verify that the parts are correct
510 : //
511 1 : CATCH_REQUIRE(p[0].get_name() == "fr");
512 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), 0.0f));
513 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
514 1 : CATCH_REQUIRE(p[0].to_string() == "fr; q=0");
515 :
516 1 : CATCH_REQUIRE(p[1].get_name() == "za");
517 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), 0.6f));
518 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
519 1 : CATCH_REQUIRE(p[1].to_string() == "za; q=0.6");
520 :
521 1 : CATCH_REQUIRE(p[2].get_name() == "en");
522 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), 0.4f));
523 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
524 1 : CATCH_REQUIRE(p[2].to_string() == "en; q=0.4");
525 :
526 1 : CATCH_REQUIRE(pc[0].get_name() == "fr");
527 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[0].get_level(), 0.0f));
528 1 : CATCH_REQUIRE(pc[0].get_parameter("test") == "");
529 1 : CATCH_REQUIRE(pc[0].to_string() == "fr; q=0");
530 :
531 1 : CATCH_REQUIRE(pc[1].get_name() == "za");
532 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[1].get_level(), 0.6f));
533 1 : CATCH_REQUIRE(pc[1].get_parameter("test") == "");
534 1 : CATCH_REQUIRE(pc[1].to_string() == "za; q=0.6");
535 :
536 1 : CATCH_REQUIRE(pc[2].get_name() == "en");
537 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[2].get_level(), 0.4f));
538 1 : CATCH_REQUIRE(pc[2].get_parameter("test") == "");
539 1 : CATCH_REQUIRE(pc[2].to_string() == "en; q=0.4");
540 : }
541 : CATCH_END_SECTION()
542 :
543 54 : CATCH_START_SECTION("weighted_http_strings: sort by level (weight)")
544 : {
545 2 : edhttp::weighted_http_string locale(" fr; q=0, za; q=0.6, en; q=0.4 ");
546 :
547 1 : locale.sort_by_level();
548 :
549 : // make sure the result is 3 parts
550 : //
551 1 : edhttp::string_part::vector_t & p(locale.get_parts());
552 1 : CATCH_REQUIRE(p.size() == 3);
553 :
554 : // now verify that the parts are sorted by level
555 : //
556 1 : CATCH_REQUIRE(p[0].get_name() == "za");
557 1 : CATCH_REQUIRE(p[1].get_name() == "en");
558 1 : CATCH_REQUIRE(p[2].get_name() == "fr");
559 : }
560 : CATCH_END_SECTION()
561 :
562 54 : CATCH_START_SECTION("weighted_http_strings: verify object")
563 : {
564 2 : edhttp::weighted_http_string locale(" fr; q=0, za, en; q=0.4 ,es;q=1.0");
565 :
566 : // no error occurred
567 : //
568 1 : CATCH_REQUIRE(locale.error_messages().empty());
569 :
570 : // original string does not change
571 : //
572 1 : CATCH_REQUIRE(locale.get_string() == " fr; q=0, za, en; q=0.4 ,es;q=1.0");
573 :
574 : // get_level() with correct and wrong names
575 : //
576 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), 0.0f));
577 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("za"), edhttp::string_part::DEFAULT_LEVEL()));
578 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), 0.4f));
579 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), 1.0f));
580 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), -1.0f));
581 :
582 : // convert back to a string
583 : //
584 1 : CATCH_REQUIRE(locale.to_string() == "fr; q=0, za, en; q=0.4, es; q=1.0");
585 : }
586 : CATCH_END_SECTION()
587 :
588 54 : CATCH_START_SECTION("weighted_http_strings: verify part")
589 : {
590 2 : edhttp::weighted_http_string locale(" fr; q=0, za, en; q=0.4 ,es;q=1.0");
591 :
592 : // make sure the result is 3 parts
593 : //
594 1 : edhttp::string_part::vector_t & p(locale.get_parts());
595 1 : CATCH_REQUIRE(p.size() == 4);
596 :
597 : // also get the constant version
598 : //
599 1 : edhttp::string_part::vector_t const & pc(locale.get_parts());
600 1 : CATCH_REQUIRE(pc.size() == 4);
601 :
602 : // now verify that the parts are correct
603 : //
604 1 : CATCH_REQUIRE(p[0].get_name() == "fr");
605 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), 0.0f));
606 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
607 1 : CATCH_REQUIRE(p[0].to_string() == "fr; q=0");
608 :
609 1 : CATCH_REQUIRE(p[1].get_name() == "za");
610 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
611 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
612 1 : CATCH_REQUIRE(p[1].to_string() == "za");
613 :
614 1 : CATCH_REQUIRE(p[2].get_name() == "en");
615 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), 0.4f));
616 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
617 1 : CATCH_REQUIRE(p[2].to_string() == "en; q=0.4");
618 :
619 1 : CATCH_REQUIRE(p[3].get_name() == "es");
620 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[3].get_level(), 1.0f));
621 1 : CATCH_REQUIRE(p[3].get_parameter("test") == "");
622 1 : CATCH_REQUIRE(p[3].to_string() == "es; q=1.0");
623 :
624 1 : CATCH_REQUIRE(pc[0].get_name() == "fr");
625 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[0].get_level(), 0.0f));
626 1 : CATCH_REQUIRE(pc[0].get_parameter("test") == "");
627 1 : CATCH_REQUIRE(pc[0].to_string() == "fr; q=0");
628 :
629 1 : CATCH_REQUIRE(pc[1].get_name() == "za");
630 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[1].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
631 1 : CATCH_REQUIRE(pc[1].get_parameter("test") == "");
632 1 : CATCH_REQUIRE(pc[1].to_string() == "za");
633 :
634 1 : CATCH_REQUIRE(pc[2].get_name() == "en");
635 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(pc[2].get_level(), 0.4f));
636 1 : CATCH_REQUIRE(pc[2].get_parameter("test") == "");
637 1 : CATCH_REQUIRE(pc[2].to_string() == "en; q=0.4");
638 :
639 1 : CATCH_REQUIRE(p[3].get_name() == "es");
640 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[3].get_level(), 1.0f));
641 1 : CATCH_REQUIRE(p[3].get_parameter("test") == "");
642 1 : CATCH_REQUIRE(p[3].to_string() == "es; q=1.0");
643 : }
644 : CATCH_END_SECTION()
645 :
646 54 : CATCH_START_SECTION("weighted_http_strings: sort by level (weight)")
647 : {
648 2 : edhttp::weighted_http_string locale(" fr; q=0, za, en; q=0.4 ,es;q=1.0");
649 :
650 1 : locale.sort_by_level();
651 :
652 : // make sure the result is 3 parts
653 : //
654 1 : edhttp::string_part::vector_t & p(locale.get_parts());
655 1 : CATCH_REQUIRE(p.size() == 4);
656 :
657 : // now verify that the parts are sorted by level
658 : //
659 1 : CATCH_REQUIRE(p[0].get_name() == "za");
660 1 : CATCH_REQUIRE(p[1].get_name() == "es");
661 1 : CATCH_REQUIRE(p[2].get_name() == "en");
662 1 : CATCH_REQUIRE(p[3].get_name() == "fr");
663 : }
664 : CATCH_END_SECTION()
665 :
666 54 : CATCH_START_SECTION("weighted_http_strings: verify object")
667 : {
668 2 : edhttp::weighted_http_string locale("de");
669 :
670 : // no error occurred
671 : //
672 1 : CATCH_REQUIRE(locale.error_messages().empty());
673 :
674 : // original string does not change
675 : //
676 1 : CATCH_REQUIRE(locale.get_string() == "de");
677 :
678 : // get_level() with correct and wrong names
679 : //
680 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::DEFAULT_LEVEL()));
681 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::UNDEFINED_LEVEL()));
682 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), edhttp::string_part::UNDEFINED_LEVEL()));
683 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::UNDEFINED_LEVEL()));
684 :
685 : // convert back to a string
686 : //
687 1 : CATCH_REQUIRE(locale.to_string() == "de");
688 :
689 : // make sure the result is 1 part
690 : //
691 1 : edhttp::string_part::vector_t & p(locale.get_parts());
692 1 : CATCH_REQUIRE(p.size() == 1);
693 :
694 : // now verify that the parts are correct
695 : //
696 1 : CATCH_REQUIRE(p[0].get_name() == "de");
697 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
698 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
699 1 : CATCH_REQUIRE(p[0].to_string() == "de");
700 : }
701 : CATCH_END_SECTION()
702 :
703 54 : CATCH_START_SECTION("weighted_http_strings: add \"en\"")
704 : {
705 2 : edhttp::weighted_http_string locale("de");
706 :
707 : // the parse is expected to work (return true)
708 : //
709 1 : CATCH_REQUIRE(locale.parse("en"));
710 :
711 : // no error occurred
712 : //
713 1 : CATCH_REQUIRE(locale.error_messages().empty());
714 :
715 : // original string does not change
716 : //
717 1 : CATCH_REQUIRE(locale.get_string() == "de,en");
718 :
719 : // get_level() with correct and wrong names
720 : //
721 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::DEFAULT_LEVEL()));
722 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::DEFAULT_LEVEL()));
723 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), edhttp::string_part::UNDEFINED_LEVEL()));
724 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::UNDEFINED_LEVEL()));
725 :
726 : // convert back to a string
727 : //
728 1 : CATCH_REQUIRE(locale.to_string() == "de, en");
729 :
730 : // make sure the result is 2 parts
731 : //
732 1 : edhttp::string_part::vector_t & p(locale.get_parts());
733 1 : CATCH_REQUIRE(p.size() == 2);
734 :
735 : // now verify that the parts are correct
736 : //
737 1 : CATCH_REQUIRE(p[0].get_name() == "de");
738 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
739 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
740 1 : CATCH_REQUIRE(p[0].to_string() == "de");
741 :
742 1 : CATCH_REQUIRE(p[1].get_name() == "en");
743 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
744 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
745 1 : CATCH_REQUIRE(p[1].to_string() == "en");
746 : }
747 : CATCH_END_SECTION()
748 :
749 54 : CATCH_START_SECTION("weighted_http_strings: add \"en\" and then \"fr\"")
750 : {
751 2 : edhttp::weighted_http_string locale("de");
752 :
753 : // the parse is expected to work (return true)
754 : //
755 1 : CATCH_REQUIRE(locale.parse("en"));
756 1 : CATCH_REQUIRE(locale.parse("fr"));
757 :
758 : // no error occurred
759 : //
760 1 : CATCH_REQUIRE(locale.error_messages().empty());
761 :
762 : // original string does not change
763 : //
764 1 : CATCH_REQUIRE(locale.get_string() == "de,en,fr");
765 :
766 : // get_level() with correct and wrong names
767 : //
768 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::DEFAULT_LEVEL()));
769 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::DEFAULT_LEVEL()));
770 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), edhttp::string_part::DEFAULT_LEVEL()));
771 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::UNDEFINED_LEVEL()));
772 :
773 : // convert back to a string
774 : //
775 1 : CATCH_REQUIRE(locale.to_string() == "de, en, fr");
776 :
777 : // make sure the result is 3 parts
778 : //
779 1 : edhttp::string_part::vector_t & p(locale.get_parts());
780 1 : CATCH_REQUIRE(p.size() == 3);
781 :
782 : // now verify that the parts are correct
783 : //
784 1 : CATCH_REQUIRE(p[0].get_name() == "de");
785 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
786 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
787 1 : CATCH_REQUIRE(p[0].to_string() == "de");
788 :
789 1 : CATCH_REQUIRE(p[1].get_name() == "en");
790 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
791 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
792 1 : CATCH_REQUIRE(p[1].to_string() == "en");
793 :
794 1 : CATCH_REQUIRE(p[2].get_name() == "fr");
795 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
796 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
797 1 : CATCH_REQUIRE(p[2].to_string() == "fr");
798 : }
799 : CATCH_END_SECTION()
800 :
801 54 : CATCH_START_SECTION("weighted_http_strings: replace with \"mo\"")
802 : {
803 2 : edhttp::weighted_http_string locale("de");
804 :
805 : // the parse is expected to work (return true)
806 : //
807 1 : CATCH_REQUIRE(locale.parse(" mo ", true));
808 :
809 : // no error occurred
810 : //
811 1 : CATCH_REQUIRE(locale.error_messages().empty());
812 :
813 : // original string does not change
814 : //
815 1 : CATCH_REQUIRE(locale.get_string() == " mo ");
816 :
817 : // get_level() with correct and wrong names
818 : //
819 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("mo"), edhttp::string_part::DEFAULT_LEVEL()));
820 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::UNDEFINED_LEVEL()));
821 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("fr"), edhttp::string_part::UNDEFINED_LEVEL()));
822 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::UNDEFINED_LEVEL()));
823 :
824 : // convert back to a string
825 : //
826 1 : CATCH_REQUIRE(locale.to_string() == "mo");
827 :
828 : // make sure the result is 1 part
829 : //
830 1 : edhttp::string_part::vector_t & p(locale.get_parts());
831 1 : CATCH_REQUIRE(p.size() == 1);
832 :
833 : // now verify that the parts are correct
834 : //
835 1 : CATCH_REQUIRE(p[0].get_name() == "mo");
836 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
837 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
838 1 : CATCH_REQUIRE(p[0].to_string() == "mo");
839 : }
840 : CATCH_END_SECTION()
841 :
842 54 : CATCH_START_SECTION("weighted_http_strings: set parameters with a value")
843 : {
844 2 : edhttp::weighted_http_string locale("de=123");
845 1 : CATCH_REQUIRE(locale.error_messages().empty());
846 1 : CATCH_REQUIRE(locale.get_string() == "de=123");
847 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("mo"), edhttp::string_part::UNDEFINED_LEVEL()));
848 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::UNDEFINED_LEVEL()));
849 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::DEFAULT_LEVEL()));
850 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::UNDEFINED_LEVEL()));
851 1 : CATCH_REQUIRE(locale.to_string() == "de=123");
852 :
853 1 : CATCH_REQUIRE(locale.parse(" mo = \"555\" ", true));
854 1 : CATCH_REQUIRE(locale.error_messages().empty());
855 1 : CATCH_REQUIRE(locale.get_string() == " mo = \"555\" ");
856 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("mo"), edhttp::string_part::DEFAULT_LEVEL()));
857 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::UNDEFINED_LEVEL()));
858 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::UNDEFINED_LEVEL()));
859 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::UNDEFINED_LEVEL()));
860 1 : CATCH_REQUIRE(locale.to_string() == "mo=555");
861 :
862 1 : CATCH_REQUIRE(locale.parse(" en = ' 555 ' ", true));
863 1 : CATCH_REQUIRE(locale.error_messages().empty());
864 1 : CATCH_REQUIRE(locale.get_string() == " en = ' 555 ' ");
865 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("mo"), edhttp::string_part::UNDEFINED_LEVEL()));
866 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::DEFAULT_LEVEL()));
867 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::UNDEFINED_LEVEL()));
868 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::UNDEFINED_LEVEL()));
869 1 : CATCH_REQUIRE(locale.to_string() == "en=\" 555 \"");
870 :
871 1 : CATCH_REQUIRE(locale.parse(" es = 555 ", true));
872 1 : CATCH_REQUIRE(locale.error_messages().empty());
873 1 : CATCH_REQUIRE(locale.get_string() == " es = 555 ");
874 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("mo"), edhttp::string_part::UNDEFINED_LEVEL()));
875 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("en"), edhttp::string_part::UNDEFINED_LEVEL()));
876 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("de"), edhttp::string_part::UNDEFINED_LEVEL()));
877 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(locale.get_level("es"), edhttp::string_part::DEFAULT_LEVEL()));
878 1 : CATCH_REQUIRE(locale.to_string() == "es=555");
879 : }
880 : CATCH_END_SECTION()
881 :
882 54 : CATCH_START_SECTION("weighted_http_strings: verify multiple parts")
883 : {
884 2 : edhttp::weighted_http_string locale("fr;q=0;r=3.2;z=fancy, za; q = 0.6 ; h = \"angry\" ; object = 'color of the wand',en; f=0.4");
885 :
886 : // make sure the result is 3 parts
887 : //
888 1 : edhttp::string_part::vector_t const & p(locale.get_parts());
889 1 : CATCH_REQUIRE(p.size() == 3);
890 :
891 : // now verify that the parts are correct
892 : //
893 1 : CATCH_REQUIRE(p[0].get_name() == "fr");
894 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
895 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), 0.0f));
896 1 : CATCH_REQUIRE(p[0].get_parameter("r") == "3.2");
897 1 : CATCH_REQUIRE(p[0].get_parameter("z") == "fancy");
898 1 : CATCH_REQUIRE(p[0].to_string() == "fr; q=0; r=3.2; z=fancy");
899 :
900 1 : CATCH_REQUIRE(p[1].get_name() == "za");
901 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
902 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), 0.6f));
903 1 : CATCH_REQUIRE(p[1].get_parameter("h") == "angry");
904 1 : CATCH_REQUIRE(p[1].get_parameter("object") == "color of the wand");
905 1 : CATCH_REQUIRE(p[1].to_string() == "za; h=angry; object=\"color of the wand\"; q=0.6");
906 :
907 1 : CATCH_REQUIRE(p[2].get_name() == "en");
908 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
909 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
910 1 : CATCH_REQUIRE(p[2].get_parameter("f") == "0.4");
911 1 : CATCH_REQUIRE(p[2].to_string() == "en; f=0.4");
912 : }
913 : CATCH_END_SECTION()
914 :
915 54 : CATCH_START_SECTION("weighted_http_strings: verify other quotation requirements")
916 : {
917 2 : edhttp::weighted_http_string locale("fr=francais;q=0;r=3.2;z=\"c'est necessaire\", za=\"South Africa\"; q = 0.6 ; h = \"angry\" ; object = 'color of the wand',en; f=0.4; cute='girls \"are\" dancing'");
918 :
919 : // make sure the result is 3 parts
920 : //
921 1 : edhttp::string_part::vector_t const & p(locale.get_parts());
922 1 : CATCH_REQUIRE(p.size() == 3);
923 :
924 : // now verify that the parts are correct
925 : //
926 1 : CATCH_REQUIRE(p[0].get_name() == "fr");
927 1 : CATCH_REQUIRE(p[0].get_value() == "francais");
928 1 : CATCH_REQUIRE(p[0].get_parameter("test") == "");
929 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[0].get_level(), 0.0f));
930 1 : CATCH_REQUIRE(p[0].get_parameter("r") == "3.2");
931 1 : CATCH_REQUIRE(p[0].get_parameter("z") == "c'est necessaire");
932 1 : CATCH_REQUIRE(p[0].to_string() == "fr=francais; q=0; r=3.2; z=\"c'est necessaire\"");
933 :
934 1 : CATCH_REQUIRE(p[1].get_name() == "za");
935 1 : CATCH_REQUIRE(p[1].get_value() == "South Africa");
936 1 : CATCH_REQUIRE(p[1].get_parameter("test") == "");
937 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[1].get_level(), 0.6f));
938 1 : CATCH_REQUIRE(p[1].get_parameter("h") == "angry");
939 1 : CATCH_REQUIRE(p[1].get_parameter("object") == "color of the wand");
940 1 : CATCH_REQUIRE(p[1].to_string() == "za=\"South Africa\"; h=angry; object=\"color of the wand\"; q=0.6");
941 :
942 1 : CATCH_REQUIRE(p[2].get_name() == "en");
943 1 : CATCH_REQUIRE(p[2].get_parameter("test") == "");
944 1 : CATCH_REQUIRE(SNAP_CATCH2_NAMESPACE::nearly_equal(p[2].get_level(), edhttp::string_part::DEFAULT_LEVEL()));
945 1 : CATCH_REQUIRE(p[2].get_parameter("f") == "0.4");
946 1 : CATCH_REQUIRE(p[2].get_parameter("cute") == "girls \"are\" dancing");
947 1 : CATCH_REQUIRE(p[2].to_string() == "en; cute='girls \"are\" dancing'; f=0.4");
948 : }
949 : CATCH_END_SECTION()
950 27 : }
951 :
952 :
953 9 : CATCH_TEST_CASE("invalid_weighted_http_string", "[invalid][string]")
954 : {
955 14 : CATCH_START_SECTION("invalid_weighted_http_strings: main name too long")
956 : {
957 2 : edhttp::weighted_http_string locale;
958 :
959 1 : CATCH_REQUIRE_FALSE(locale.parse("deutsch_ist_zu-schwierig"));
960 :
961 1 : CATCH_REQUIRE(locale.error_messages() == "part name is empty or too long (limit is '8-8' characters).\n");
962 : }
963 : CATCH_END_SECTION()
964 :
965 14 : CATCH_START_SECTION("invalid_weighted_http_strings: sub-name too long")
966 : {
967 2 : edhttp::weighted_http_string locale;
968 :
969 1 : CATCH_REQUIRE_FALSE(locale.parse("deutsch-ist_zu_schwierig"));
970 :
971 1 : CATCH_REQUIRE(locale.error_messages() == "part sub-name is empty or too long (limit is '8-8' characters).\n");
972 : }
973 : CATCH_END_SECTION()
974 :
975 14 : CATCH_START_SECTION("invalid_weighted_http_strings: too many dashes in part name")
976 : {
977 2 : edhttp::weighted_http_string locale;
978 :
979 1 : CATCH_REQUIRE_FALSE(locale.parse("deutsch-ist-zu-schwierig"));
980 :
981 1 : CATCH_REQUIRE(locale.error_messages() == "part name cannot include more than one '-'.\n");
982 : }
983 : CATCH_END_SECTION()
984 :
985 14 : CATCH_START_SECTION("invalid_weighted_http_strings: negative quality is not acceptable")
986 : {
987 2 : edhttp::weighted_http_string locale;
988 :
989 1 : CATCH_REQUIRE_FALSE(locale.parse("fr-FR;q=-1.0"));
990 :
991 1 : CATCH_REQUIRE(locale.error_messages() == "the quality value (q=...) cannot be a negative number.\n");
992 : }
993 : CATCH_END_SECTION()
994 :
995 14 : CATCH_START_SECTION("invalid_weighted_http_strings: quality must be a valid double")
996 : {
997 2 : edhttp::weighted_http_string locale;
998 :
999 1 : CATCH_REQUIRE_FALSE(locale.parse("fr-FR;q=joke"));
1000 :
1001 1 : CATCH_REQUIRE(locale.error_messages() == "the quality value (q=...) is not a valid floating point.\n");
1002 : }
1003 : CATCH_END_SECTION()
1004 :
1005 14 : CATCH_START_SECTION("invalid_weighted_http_strings: spurious data")
1006 : {
1007 2 : edhttp::weighted_http_string locale;
1008 :
1009 1 : CATCH_REQUIRE_FALSE(locale.parse("fr-FR;joke=\"it is\" not"));
1010 :
1011 1 : CATCH_REQUIRE(locale.error_messages() == "found a spurious character in a weighted string.\n");
1012 : }
1013 : CATCH_END_SECTION()
1014 :
1015 14 : CATCH_START_SECTION("invalid_weighted_http_strings: expected value separator (,) or EOS")
1016 : {
1017 2 : edhttp::weighted_http_string locale;
1018 :
1019 : // TODO: the space should not be require to get the error!
1020 1 : CATCH_REQUIRE_FALSE(locale.parse("fr-FR |"));
1021 :
1022 1 : CATCH_REQUIRE(locale.error_messages() == "part not ended by a comma or end of string.\n");
1023 : }
1024 : CATCH_END_SECTION()
1025 7 : }
1026 :
1027 :
1028 4 : CATCH_TEST_CASE("invalid_string_part", "[invalid][string]")
1029 : {
1030 4 : CATCH_START_SECTION("invalid_string_part: part value includes ' and \"")
1031 : {
1032 2 : edhttp::string_part p("invalid");
1033 1 : CATCH_REQUIRE(p.get_name() == "invalid");
1034 1 : CATCH_REQUIRE(p.get_value().empty());
1035 1 : p.set_value("c'est pas \"possible\"");
1036 1 : CATCH_REQUIRE(p.get_value() == "c'est pas \"possible\"");
1037 1 : CATCH_REQUIRE_THROWS_MATCHES(
1038 : p.to_string()
1039 : , edhttp::unquotable_string
1040 : , Catch::Matchers::ExceptionMessage(
1041 : "edhttp_exception: string [c'est pas \"possible\"] includes single and double quotes."));
1042 : }
1043 : CATCH_END_SECTION()
1044 :
1045 4 : CATCH_START_SECTION("invalid_string_part: part value includes \" and '")
1046 : {
1047 2 : edhttp::string_part p("invalid");
1048 1 : CATCH_REQUIRE(p.get_name() == "invalid");
1049 1 : CATCH_REQUIRE(p.get_value().empty());
1050 1 : p.set_value("\"c'est pas possible\"");
1051 1 : CATCH_REQUIRE(p.get_value() == "\"c'est pas possible\"");
1052 1 : CATCH_REQUIRE_THROWS_MATCHES(
1053 : p.to_string()
1054 : , edhttp::unquotable_string
1055 : , Catch::Matchers::ExceptionMessage(
1056 : "edhttp_exception: string [\"c'est pas possible\"] includes single and double quotes."));
1057 : }
1058 : CATCH_END_SECTION()
1059 8 : }
1060 :
1061 :
1062 :
1063 : // vim: ts=4 sw=4 et
|