parse_shimmeddocument_test.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // test ParameterSet values
4 //
5 // ======================================================================
6 
7 #define BOOST_TEST_MODULE (document test)
8 
9 #include "boost/test/unit_test.hpp"
11 #include "fhiclcpp/parse.h"
12 
13 #include <string>
14 
15 using namespace fhicl;
16 using namespace std;
17 
18 BOOST_AUTO_TEST_SUITE(shimmeddocument_test)
19 
21 
22 /*
23  * Test case activates the snippet mode; it must be the fist test case in this
24  * test suite
25  */
26 BOOST_AUTO_TEST_CASE(enable_snippet_mode)
27 {
28  BOOST_TEST(::shims::isSnippetMode(true));
29 }
30 
31 /*
32  * Test case checks if values stored in prolog can be accessed
33  */
34 BOOST_AUTO_TEST_CASE(can_access_prolog_01)
35 {
36  BOOST_TEST(::shims::isSnippetMode());
37 
38  std::string const document = "BEGIN_PROLOG\n"
39  " a: 1\n"
40  " b: \"bb\"\n"
41  "END_PROLOG\n"
42  "c: 3\n";
43  auto fhicl_table = ::fhicl::parse_document(document);
44 
45  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 3);
46 
47  BOOST_TEST(fhicl_table.exists("a"));
48  auto const& a = fhicl_table.find("a");
49  BOOST_TEST(a.in_prolog == true);
50  BOOST_TEST(std::any_cast<std::string>(a.value) == "1");
51 
52  BOOST_TEST(fhicl_table.exists("b"));
53  auto const& b = fhicl_table.find("b");
54  BOOST_TEST(b.in_prolog == true);
55  BOOST_TEST(std::any_cast<std::string>(b.value) == "\"bb\"");
56 
57  BOOST_TEST(fhicl_table.exists("c"));
58  auto const& c = fhicl_table.find("c");
59  BOOST_TEST(c.in_prolog == false);
60  BOOST_TEST(std::any_cast<std::string>(c.value) == "3");
61 }
62 
63 /*
64  * Test case checks if values can be retried in the same order as they appear
65  * in the fhicl document.
66  */
67 BOOST_AUTO_TEST_CASE(preserved_order_of_values_01)
68 {
69  BOOST_TEST(::shims::isSnippetMode());
70 
71  std::string const document = "BEGIN_PROLOG\n"
72  " z: 1.1\n"
73  " a: aa\n"
74  "END_PROLOG\n";
75  auto fhicl_table = ::fhicl::parse_document(document);
76 
77  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 2);
78 
79  auto it = fhicl_table.begin();
80 
81  BOOST_TEST(it->second.in_prolog == true);
82  BOOST_TEST(it->first == "z");
83  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "1.1");
84 
85  std::advance(it, 1);
86 
87  BOOST_TEST(it->second.in_prolog == true);
88  BOOST_TEST(it->first == "a");
89  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"aa\"");
90 }
91 
92 /*
93  * Test case checks if values can be retried in the same order as they appear
94  * in the fhicl document.
95  */
96 BOOST_AUTO_TEST_CASE(preserved_order_of_values_02)
97 {
98  BOOST_TEST(::shims::isSnippetMode());
99 
100  std::string const document = "z: 1.1\n"
101  "a: aa\n";
102  auto fhicl_table = ::fhicl::parse_document(document);
103 
104  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 2);
105 
106  auto it = fhicl_table.begin();
107 
108  BOOST_TEST(it->second.in_prolog == false);
109  BOOST_TEST(it->first == "z");
110  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "1.1");
111 
112  std::advance(it, 1);
113 
114  BOOST_TEST(it->second.in_prolog == false);
115  BOOST_TEST(it->first == "a");
116  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"aa\"");
117 }
118 
119 /*
120  * Test case checks if values can be retried in the same order as they appear
121  * in the fhicl document.
122  */
123 BOOST_AUTO_TEST_CASE(preserved_order_of_values_03)
124 {
125  BOOST_TEST(::shims::isSnippetMode());
126 
127  std::string const document = "BEGIN_PROLOG\n"
128  " z: 1.1\n"
129  " a: aa\n"
130  "END_PROLOG\n"
131  "x:xx\n"
132  "c:cc\n";
133  auto fhicl_table = ::fhicl::parse_document(document);
134 
135  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 4);
136 
137  auto it = fhicl_table.begin();
138  BOOST_TEST(it->second.in_prolog == true);
139  BOOST_TEST(it->first == "z");
140  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "1.1");
141 
142  std::advance(it, 1);
143  BOOST_TEST(it->second.in_prolog == true);
144  BOOST_TEST(it->first == "a");
145  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"aa\"");
146 
147  std::advance(it, 1);
148  BOOST_TEST(it->second.in_prolog == false);
149  BOOST_TEST(it->first == "x");
150  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"xx\"");
151 
152  std::advance(it, 1);
153  BOOST_TEST(it->second.in_prolog == false);
154  BOOST_TEST(it->first == "c");
155  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"cc\"");
156 }
157 
158 /*
159  * Test case checks if the resolving of @local::-style references can be turned
160  * off
161  */
162 BOOST_AUTO_TEST_CASE(keep_unresolved_references_01)
163 {
164  BOOST_TEST(::shims::isSnippetMode());
165 
166  std::string const document = "z: 1.1\n"
167  "ss: {rr: zz}\n"
168  "mm: @local::ss\n";
169  auto fhicl_table = ::fhicl::parse_document(document);
170 
171  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 3);
172 
173  BOOST_TEST(fhicl_table.exists("mm"));
174  auto const& mm = fhicl_table.find("mm");
175  BOOST_TEST(mm.in_prolog == false);
176  BOOST_TEST(std::any_cast<std::string>(mm.value) == "\"@local::ss\"");
177 }
178 
179 /*
180  * Test case checks if the resolving of @local::-style references can be turned
181  * off
182  */
183 BOOST_AUTO_TEST_CASE(keep_unresolved_references_02)
184 {
185  BOOST_TEST(::shims::isSnippetMode());
186 
187  std::string const document = "BEGIN_PROLOG\n"
188  " z: 1.1\n"
189  " ss: {rr: zz}\n"
190  "END_PROLOG\n"
191  "mm: @local::ss\n";
192  auto fhicl_table = ::fhicl::parse_document(document);
193 
194  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 3);
195 
196  BOOST_TEST(fhicl_table.exists("mm"));
197  auto const& mm = fhicl_table.find("mm");
198  BOOST_TEST(mm.in_prolog == false);
199  BOOST_TEST(std::any_cast<std::string>(mm.value) == "\"@local::ss\"");
200 }
201 
202 /*
203  * Test case checks if the resolving of @table::-style references can be turned
204  * off
205  */
206 BOOST_AUTO_TEST_CASE(keep_unresolved_references_03)
207 {
208  BOOST_TEST(::shims::isSnippetMode());
209 
210  std::string const document = "BEGIN_PROLOG\n"
211  " tt: { a:1 b: 2 }\n"
212  "END_PROLOG\n"
213  "placeholder_table_001:@table::tt\n";
214  auto fhicl_table = ::fhicl::parse_document(document);
215 
216  BOOST_TEST(fhicl_table.exists("placeholder_table_001"));
217 
218  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 2);
219 
220  auto const& tt = fhicl_table.find("placeholder_table_001");
221  BOOST_TEST(tt.in_prolog == false);
222  BOOST_TEST(std::any_cast<std::string>(tt.value) == "\"@table::tt\"");
223 }
224 
225 /*
226  * Test case checks if the resolving of @table::-style references can be turned
227  * off
228  */
229 BOOST_AUTO_TEST_CASE(keep_unresolved_references_04)
230 {
231  BOOST_TEST(::shims::isSnippetMode());
232 
233  std::string const document = "BEGIN_PROLOG\n"
234  " tt: { a:1 b: 2 }\n"
235  " placeholder_table_001:@table::tt\n"
236  "END_PROLOG\n";
237  auto fhicl_table = ::fhicl::parse_document(document);
238 
239  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 2);
240 
241  BOOST_TEST(fhicl_table.exists("placeholder_table_001"));
242 
243  auto const& tt = fhicl_table.find("placeholder_table_001");
244  BOOST_TEST(tt.in_prolog == true);
245  BOOST_TEST(std::any_cast<std::string>(tt.value) == "\"@table::tt\"");
246 }
247 
248 /*
249  * Test case checks if the resolving of @sequence::-style references can be
250  * turned off
251  */
252 BOOST_AUTO_TEST_CASE(keep_unresolved_references_05)
253 {
254  BOOST_TEST(::shims::isSnippetMode());
255 
256  std::string const document = "BEGIN_PROLOG\n"
257  " z: 1.1\n"
258  " ss: [23,24,25]\n"
259  "END_PROLOG\n"
260  "sss:[@sequence::ss, 34, 35]\n";
261  auto fhicl_table = ::fhicl::parse_document(document);
262 
263  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 3);
264 
265  auto const& sss = fhicl_table.find("sss");
266  BOOST_TEST(sss.in_prolog == false);
267  sequence_t seq = sss;
268  BOOST_TEST(std::any_cast<std::string>(seq.at(0).value) ==
269  "\"@sequence::ss\"");
270 }
271 
272 /*
273  * Test case checks if the resolving of @sequence::-style references can be
274  * turned off
275  */
276 BOOST_AUTO_TEST_CASE(keep_unresolved_references_06)
277 {
278  BOOST_TEST(::shims::isSnippetMode());
279 
280  std::string const document = "BEGIN_PROLOG\n"
281  " z: 1.1\n"
282  " ss: [23,24,25]\n"
283  "sss:[@sequence::ss, 34, 35]\n"
284  "END_PROLOG\n";
285  auto fhicl_table = ::fhicl::parse_document(document);
286  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 3);
287 
288  auto const& sss = fhicl_table.find("sss");
289  BOOST_TEST(sss.in_prolog == true);
290  sequence_t seq = sss;
291  BOOST_TEST(std::any_cast<std::string>(seq.at(0).value) ==
292  "\"@sequence::ss\"");
293 }
294 
295 /*
296  * Test case checks if the @erase directive can be turned off
297  */
298 BOOST_AUTO_TEST_CASE(keep_unresolved_references_07)
299 {
300  BOOST_TEST(::shims::isSnippetMode());
301 
302  std::string const document = "BEGIN_PROLOG\n"
303  " z: 1.1\n"
304  " ss: {rr: zz}\n"
305  "END_PROLOG\n"
306  "mm:@local::ss\n"
307  "mm:@erase\n";
308  auto fhicl_table = ::fhicl::parse_document(document);
309  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 4);
310 
311  BOOST_TEST(fhicl_table.exists("mm"));
312 
313  auto const& mm = fhicl_table.find("mm");
314  BOOST_TEST(mm.in_prolog == false);
315  BOOST_TEST(std::any_cast<std::string>(mm.value) == "\"@local::ss\"");
316 
317  auto it = fhicl_table.begin();
318  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 1);
319  BOOST_TEST(it->second.in_prolog == false);
320  BOOST_TEST(it->first == "mm");
321  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"@erase\"");
322 }
323 
324 /*
325  * Test case checks if the "@nil" values are not affected by the parsing shim
326  * code
327  */
329 {
330  BOOST_TEST(::shims::isSnippetMode());
331 
332  std::string const document = "BEGIN_PROLOG\n"
333  " a: @nil\n"
334  " ss: {rr: zz}\n"
335  "END_PROLOG\n"
336  "mm:@nil\n";
337  auto fhicl_table = ::fhicl::parse_document(document);
338 
339  BOOST_TEST(fhicl_table.exists("mm"));
340  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 3);
341 
342  auto const& mm = fhicl_table.find("mm");
343  BOOST_TEST(mm.in_prolog == false);
344  BOOST_TEST(mm.is_a(::fhicl::NIL));
345 
346  auto const& a = fhicl_table.find("a");
347  BOOST_TEST(a.in_prolog == true);
348  BOOST_TEST(a.is_a(::fhicl::NIL));
349 }
350 
351 /*
352  * Test case checks if the dot delimiter processing can be turned off
353  */
354 BOOST_AUTO_TEST_CASE(keep_dots_in_keynames_01)
355 {
356  BOOST_TEST(::shims::isSnippetMode());
357 
358  std::string const document = "BEGIN_PROLOG\n"
359  " z: 1.1\n"
360  " ss: {rr: zz\n"
361  " h: { hh: {hhh:hhhh } } }\n"
362  "END_PROLOG\n"
363  "h.hh.hhh:h\n";
364  auto fhicl_table = ::fhicl::parse_document(document);
365  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 3);
366 
367  BOOST_TEST(fhicl_table.exists("h.hh.hhh"));
368 
369  auto const& h = fhicl_table.find("h.hh.hhh");
370  BOOST_TEST(h.in_prolog == false);
371  BOOST_TEST(std::any_cast<std::string>(h.value) == "\"h\"");
372 }
373 
374 /*
375  * Test case checks if the dot delimiter processing can be turned off
376  */
377 BOOST_AUTO_TEST_CASE(keep_dots_in_keynames_02)
378 {
379  BOOST_TEST(::shims::isSnippetMode());
380 
381  std::string const document = "BEGIN_PROLOG\n"
382  " z: 1.1\n"
383  " c:c1\n"
384  "END_PROLOG\n"
385  "h.hh.hhh:h\n"
386  "c.cc.ccc:@local::c\n";
387  auto fhicl_table = ::fhicl::parse_document(document);
388  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 4);
389 
390  BOOST_TEST(fhicl_table.exists("c"));
391 
392  auto const& c = fhicl_table.find("c");
393  BOOST_TEST(c.in_prolog == true);
394  BOOST_TEST(std::any_cast<std::string>(c.value) == "\"c1\"");
395 
396  auto it = fhicl_table.begin();
397  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 2);
398  BOOST_TEST(it->second.in_prolog == false);
399  BOOST_TEST(it->first == "h.hh.hhh");
400  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"h\"");
401 
402  it = fhicl_table.begin();
403  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 1);
404  BOOST_TEST(it->second.in_prolog == false);
405  BOOST_TEST(it->first == "c.cc.ccc");
406  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"@local::c\"");
407 }
408 
409 /*
410  * Test case checks if duplicate key value pairs allowed
411  */
412 BOOST_AUTO_TEST_CASE(allow_duplicate_kvp_01)
413 {
414  BOOST_TEST(::shims::isSnippetMode());
415 
416  std::string const document = "c:c1\n"
417  "c:c2\n"
418  "h:h1\n"
419  "c:c3\n";
420  auto fhicl_table = ::fhicl::parse_document(document);
421  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 4);
422 
423  BOOST_TEST(fhicl_table.exists("c"));
424 
425  auto const& c = fhicl_table.find("c");
426  BOOST_TEST(c.in_prolog == false);
427  BOOST_TEST(std::any_cast<std::string>(c.value) == "\"c1\"");
428 
429  auto it = fhicl_table.begin();
430  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 3);
431  BOOST_TEST(it->second.in_prolog == false);
432  BOOST_TEST(it->first == "c");
433  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"c2\"");
434 
435  it = fhicl_table.begin();
436  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 2);
437  BOOST_TEST(it->second.in_prolog == false);
438  BOOST_TEST(it->first == "h");
439  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"h1\"");
440 
441  it = fhicl_table.begin();
442  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 1);
443  BOOST_TEST(it->second.in_prolog == false);
444  BOOST_TEST(it->first == "c");
445  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"c3\"");
446 }
447 
448 /*
449  * Test case checks if duplicate key value pairs allowed
450  */
451 BOOST_AUTO_TEST_CASE(allow_duplicate_kvp_02)
452 {
453  BOOST_TEST(::shims::isSnippetMode());
454 
455  std::string const document = "c.cc:c1\n"
456  "c.cc:c2\n"
457  "h:h1\n"
458  "c.cc:c3\n";
459  auto fhicl_table = ::fhicl::parse_document(document);
460  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 4);
461 
462  BOOST_TEST(fhicl_table.exists("c.cc"));
463 
464  auto const& c = fhicl_table.find("c.cc");
465  BOOST_TEST(c.in_prolog == false);
466  BOOST_TEST(std::any_cast<std::string>(c.value) == "\"c1\"");
467 
468  auto it = fhicl_table.begin();
469  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 3);
470  BOOST_TEST(it->second.in_prolog == false);
471  BOOST_TEST(it->first == "c.cc");
472  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"c2\"");
473 
474  it = fhicl_table.begin();
475  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 2);
476  BOOST_TEST(it->second.in_prolog == false);
477  BOOST_TEST(it->first == "h");
478  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"h1\"");
479 
480  it = fhicl_table.begin();
481  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 1);
482  BOOST_TEST(it->second.in_prolog == false);
483  BOOST_TEST(it->first == "c.cc");
484  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"c3\"");
485 }
486 
487 /*
488  * Test case checks if duplicate key value pairs allowed
489  */
490 BOOST_AUTO_TEST_CASE(allow_duplicate_kvp_03)
491 {
492  BOOST_TEST(::shims::isSnippetMode());
493 
494  std::string const document = "c.cc:c1\n"
495  "c.cc:c2\n"
496  "h:h1\n"
497  "c.cc:@local::h\n";
498  auto fhicl_table = ::fhicl::parse_document(document);
499  BOOST_TEST(std::distance(fhicl_table.begin(), fhicl_table.end()) == 4);
500 
501  BOOST_TEST(fhicl_table.exists("c.cc"));
502 
503  auto const& c = fhicl_table.find("c.cc");
504  BOOST_TEST(c.in_prolog == false);
505  BOOST_TEST(std::any_cast<std::string>(c.value) == "\"c1\"");
506 
507  auto it = fhicl_table.begin();
508  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 3);
509  BOOST_TEST(it->second.in_prolog == false);
510  BOOST_TEST(it->first == "c.cc");
511  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"c2\"");
512 
513  it = fhicl_table.begin();
514  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 2);
515  BOOST_TEST(it->second.in_prolog == false);
516  BOOST_TEST(it->first == "h");
517  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"h1\"");
518 
519  it = fhicl_table.begin();
520  std::advance(it, std::distance(fhicl_table.begin(), fhicl_table.end()) - 1);
521  BOOST_TEST(it->second.in_prolog == false);
522  BOOST_TEST(it->first == "c.cc");
523  BOOST_TEST(std::any_cast<std::string>(it->second.value) == "\"@local::h\"");
524 }
525 
526 BOOST_AUTO_TEST_SUITE_END()
bool isSnippetMode(bool m)
Definition: parse_shims.cc:9
std::string string
Definition: nybbler.cc:12
STL namespace.
Definition: type_traits.h:61
const double a
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
fhicl::extended_value::sequence_t sequence_t
BOOST_AUTO_TEST_CASE(enable_snippet_mode)
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
Definition: parse.cc:720
static constexpr double mm
Definition: Units.h:65
static bool * b
Definition: config.cpp:1043