Lumiera  0.pre.03
»edit your freedom«
text-template-test.cpp
Go to the documentation of this file.
1 /*
2  TextTemplate(Test) - verify the minimalistic text substitution engine
3 
4  Copyright (C)
5  2024, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
20 #include "lib/test/run.hpp"
21 #include "lib/test/test-helper.hpp"
22 #include "lib/text-template.hpp"
24 
25 #include <map>
26 
27 //using std::array;
28 using std::regex_search;
29 using std::smatch;
30 using util::_Fmt;
31 using util::join;
32 using lib::diff::Rec;
33 using lib::diff::MakeRec;
34 using lib::diff::GenNode;
35 
36 
37 namespace lib {
38 namespace test {
39 
40  using MapS = std::map<string, string>;
41  using LERR_(ITER_EXHAUST);
42 
43  using text_template::ACCEPT_MARKUP;
44  using text_template::TagSyntax;
45 
46 
47  /***************************************************************************/
57  class TextTemplate_test : public Test
58  {
59 
60  virtual void
61  run (Arg)
62  {
63  simpeUsage();
70 
71  }
72 
73 
75  void
77  {
78  MapS snaps{{"whatever", "cruel world"}
79  ,{"greeting", "farewell"}};
80  CHECK (TextTemplate::apply ("${greeting} ${whatever} ↯", snaps)
81  == "farewell cruel world ↯"_expect);
82  }
83 
84 
94  void
96  {
97  smatch mat;
98  string input;
99  CHECK (not regex_search (input, mat, ACCEPT_MARKUP));
100 
101  input = " Hallelujah ";
102  CHECK (not regex_search (input, mat, ACCEPT_MARKUP)); // walk away ... nothing to see here...
103 
104  input = " stale${beer}forever";
105  CHECK (regex_search (input, mat, ACCEPT_MARKUP));
106  CHECK (mat.position() == 6);
107  CHECK (mat.length() == 7);
108  CHECK (mat.prefix() == " stale"_expect);
109  CHECK (mat.suffix() == "forever"_expect);
110  CHECK (mat[0] == "${beer}"_expect); // so this first example demonstrates placeholder recognition
111  CHECK (not mat[1].matched); // Sub-1 : this is not an escaped pattern
112  CHECK (not mat[2].matched); // Sub-2 : this pattern does not start with "else"
113  CHECK (not mat[3].matched); // Sub-3 : no "end" keyword
114  CHECK (not mat[4].matched); // Sub-4 : no further logic syntax
115  CHECK (mat[5] == "beer"_expect); // Sub-5 : extracts the Key ID
116 
117  input = " watch ${for stale}${beer} whatever ";
118  CHECK (regex_search (input, mat, ACCEPT_MARKUP));
119  CHECK (mat.position() == 7);
120  CHECK (mat.length() == 12);
121  CHECK (mat.prefix() == " watch "_expect);
122  CHECK (mat.suffix() == "${beer} whatever "_expect); // (performing only one search here...)
123  CHECK (mat[0] == "${for stale}"_expect); // Matched a regular opening iteration tag
124  CHECK (not mat[2].matched); // Sub-2 does not trigger, since there is no "else" mark
125  CHECK (not mat[3].matched); // Sub-3 does not trigger, no end mark either
126  CHECK (mat[4] == "for"_expect); // Sub-4 picks the "for" keyword
127  CHECK (mat[5] == "stale"_expect); // Sub-5 extracts a simple Key ≡ "stale"
128 
129  input = " work ${ end if beer \t } however ";
130  CHECK (regex_search (input, mat, ACCEPT_MARKUP));
131  CHECK (mat.position() == 6);
132  CHECK (mat.length() == 19);
133  CHECK (mat.prefix() == " work "_expect);
134  CHECK (mat.suffix() == " however "_expect);
135  CHECK (mat[0] == "${ end if beer \t }"_expect); // A regular end marker of an conditional
136  CHECK (mat[3] == "end "_expect); // Sub-3 triggers on the "end" token
137  CHECK (mat[4] == "if"_expect); // Sub-4 picks the "if" keyword
138  CHECK (mat[5] == "beer"_expect); // Sub-5 extracts a simple Key ≡ "beer"
139 
140  input = " catch ${endgame stale}${endfor brown.beer} ever ";
141  CHECK (regex_search (input, mat, ACCEPT_MARKUP));
142  CHECK (mat.position() == 23);
143  CHECK (mat.length() == 20);
144  CHECK (mat.prefix() == " catch ${endgame stale}"_expect);// "while" is no valid keyword at the second position of the syntax
145  CHECK (mat.suffix() == " ever "_expect);
146  CHECK (mat[0] == "${endfor brown.beer}"_expect); // ...thus search proceeds to match on the second pattern installment
147  CHECK (mat[3] == "end"_expect); // Sub-3 triggers on the "end" token
148  CHECK (mat[4] == "for"_expect); // Sub-4 picks the "for" keyword
149  CHECK (mat[5] == "brown.beer"_expect); // Sub-5 extracts a hierarchical key ID
150 
151  input = " catch ${else} ever ";
152  CHECK (regex_search (input, mat, ACCEPT_MARKUP));
153  CHECK (mat.position() == 7);
154  CHECK (mat.length() == 7);
155  CHECK (mat.prefix() == " catch "_expect);
156  CHECK (mat.suffix() == " ever "_expect);
157  CHECK (mat[0] == "${else}"_expect); // Standard match on an "else"-tag
158  CHECK (mat[2] == "else"_expect); // Sub-2 confirmed a solitary "else" keyword
159  CHECK (not mat[1].matched);
160  CHECK (not mat[3].matched);
161  CHECK (not mat[4].matched);
162  CHECK (not mat[5].matched);
163 
164  input = " catch ${else if} fever \\${can.beer} ";
165  CHECK (regex_search (input, mat, ACCEPT_MARKUP));
166  CHECK (mat.position() == 24);
167  CHECK (mat.length() == 2);
168  CHECK (mat.prefix() == " catch ${else if} fever "_expect); // Note: first pattern does not match as "else" must be solitary
169  CHECK (mat.suffix() == "{can.beer} "_expect); // Note: the following braced expression is tossed aside
170  CHECK (mat[0] == "\\$"_expect); // Only the escaped pattern mark opening is picked up
171  CHECK (not mat[2].matched);
172  CHECK (not mat[3].matched);
173  CHECK (not mat[4].matched);
174  CHECK (not mat[5].matched);
175  CHECK (mat[1] == "\\$"_expect); // Sub-1 picks the escaped mark (and the remainder is no complete tag)
176 
177 
178 
179  // Demonstration: can use this regular expression in a matching pipeline....
180  input = "one ${two} three \\${four} ${if high} five";
181  CHECK (util::join(
182  explore (util::RegexSearchIter{input, ACCEPT_MARKUP})
183  .transform ([](smatch mat){ return mat.str(); }))
184  ==
185  "${two}, \\$, ${if high}"_expect);
186 
187 
188  // Parse matches of this regexp into well defined syntax elements
189  auto parser = text_template::parse (input);
190  CHECK (not isnil(parser));
191  CHECK (parser->syntax == TagSyntax::KEYID);
192  CHECK (parser->lead == "one "_expect);
193  CHECK (parser->key == "two"_expect); // extract "two" as key for data lookup
194  ++parser;
195  CHECK (parser);
196  CHECK (parser->syntax == TagSyntax::ESCAPE);
197  CHECK (parser->lead == " three "_expect);
198  CHECK (parser->key == ""_expect); // empty since this tag has been escaped
199  ++parser;
200  CHECK (parser);
201  CHECK (parser->syntax == TagSyntax::IF);
202  CHECK (parser->lead == "${four} "_expect); // note: leading escape sign removed
203  CHECK (parser->key == "high"_expect); // key ≡ "high" used to evaluate conditional
204  ++parser;
205  CHECK (isnil (parser)); // note: the /parser/ stops right behind last token
206  VERIFY_ERROR (ITER_EXHAUST, *parser);
207  VERIFY_ERROR (ITER_EXHAUST, ++parser);
208 
209 
210 
211  // Generate sequence of Action tokens from parsing results
212  input = R"~(
213  Prefix-1 ${some.key} next one is \${escaped}
214  Prefix-2 ${if cond1} active ${else} inactive ${end if
215 }Prefix-3 ${if cond2} active2${end if cond2} more
216  Prefix-4 ${for data} fixed ${embedded}
217  Pre-5 ${if nested}nested-active${
218  else }nested-inactive${ end
219  if nested}loop-suffix${else}${end
220 for} tail...
221 )~";
222  auto actions = TextTemplate::compile (input);
223  CHECK (25 == actions.size());
224 
225  CHECK (actions[ 0].code == TextTemplate::Code::TEXT);
226  CHECK (actions[ 0].val == "\n Prefix-1 "_expect); // static text prefix
227  CHECK (actions[ 0].refIDX == 0);
228 
229  CHECK (actions[ 1].code == TextTemplate::Code::KEY); // a placeholder to be substituted
230  CHECK (actions[ 1].val == "some.key"_expect); // use "some.key" for data retrieval
231 
232  CHECK (actions[ 2].code == TextTemplate::Code::TEXT); // static text between active fields
233  CHECK (actions[ 2].val == " next one is "_expect);
234 
235  CHECK (actions[ 3].code == TextTemplate::Code::TEXT); // since next tag was escaped, it appears in static segment,
236  CHECK (actions[ 3].val == "${escaped}\n Prefix-2 "_expect); // yet without the leading escape, which has been absorbed.
237 
238  CHECK (actions[ 4].code == TextTemplate::Code::COND); // start of an if-bracket construct
239  CHECK (actions[ 4].val == "cond1"_expect); // data marked with "cond1" will be used to determine true/false
240  CHECK (actions[ 4].refIDX == 7 ); // IDX ≡ 7 marks start of the else-branch
241 
242  CHECK (actions[ 5].code == TextTemplate::Code::TEXT); // this static block will only be included if "cond1" evaluates to true
243  CHECK (actions[ 5].val == " active "_expect);
244 
245  CHECK (actions[ 6].code == TextTemplate::Code::JUMP); // unconditional jump at the end of the if-true-block
246  CHECK (actions[ 6].val == ""_expect);
247  CHECK (actions[ 6].refIDX == 8 ); // IDX ≡ 8 points to the next element after the conditional construct
248 
249  CHECK (actions[ 7].code == TextTemplate::Code::TEXT); // this static (else)-block will be included if "cond1" does not hold
250  CHECK (actions[ 7].val == " inactive "_expect);
251 
252  CHECK (actions[ 8].code == TextTemplate::Code::TEXT); // again a static segment, displayed unconditionally
253  CHECK (actions[ 8].val == "Prefix-3 "_expect); // Note: no newline, since the closing bracket was placed at line start
254 
255  CHECK (actions[ 9].code == TextTemplate::Code::COND); // again a conditional (but this time without else-branch)
256  CHECK (actions[ 9].val == "cond2"_expect); // data marked with "cond2" will be evaluated as condition
257  CHECK (actions[ 9].refIDX == 11 ); // IDX ≡ 11 is the alternative route, this time pointing behind the conditional
258 
259  CHECK (actions[10].code == TextTemplate::Code::TEXT); // static text block to be displayed as content of the conditional
260  CHECK (actions[10].val == " active2"_expect);
261 
262  CHECK (actions[11].code == TextTemplate::Code::TEXT); // again an unconditional static segment (behind end of preceding conditional)
263  CHECK (actions[11].val == " more\n Prefix-4 "_expect);
264 
265  CHECK (actions[12].code == TextTemplate::Code::ITER); // Start of a for-construct (iteration)
266  CHECK (actions[12].val == "data"_expect); // data marked with "data" will be used to find and iterate nested elements
267  CHECK (actions[12].refIDX == 23 ); // IDX ≡ 23 points to the alternative "else" block, in case no iteration takes place
268 
269  CHECK (actions[13].code == TextTemplate::Code::TEXT); // static block to appear for each nested "data" element
270  CHECK (actions[13].val == " fixed "_expect);
271 
272  CHECK (actions[14].code == TextTemplate::Code::KEY); // placeholder to be substituted
273  CHECK (actions[14].val == "embedded"_expect); // _typically_ the data "embedded" will live in the iterated, nested elements
274 
275  CHECK (actions[15].code == TextTemplate::Code::TEXT); // again a static block, which however lives within the iterated segment
276  CHECK (actions[15].val == "\n Pre-5 "_expect);
277 
278  CHECK (actions[16].code == TextTemplate::Code::COND); // a nested conditional, thus nested on second level within the iteration construct
279  CHECK (actions[16].val == "nested"_expect); // data marked with "nested" will control the conditional (typically from iterated data elements)
280  CHECK (actions[16].refIDX == 19 ); // IDX ≡ 19 points to the else-block of this nested conditional
281 
282  CHECK (actions[17].code == TextTemplate::Code::TEXT); // static content to appear as nested if-true-section
283  CHECK (actions[17].val == "nested-active"_expect);
284 
285  CHECK (actions[18].code == TextTemplate::Code::JUMP); // jump code at end of the true-section
286  CHECK (actions[18].val == ""_expect);
287  CHECK (actions[18].refIDX == 20 ); // IDX ≡ 20 points behind the end of this nested conditional construct
288 
289  CHECK (actions[19].code == TextTemplate::Code::TEXT); // static content comprising the else-section
290  CHECK (actions[19].val == "nested-inactive"_expect); // Note: no whitespace due to placement of the tag brackets of "else" / "end if"
291 
292  CHECK (actions[20].code == TextTemplate::Code::TEXT); // again an unconditional static segment, yet still within the looping construct
293  CHECK (actions[20].val == "loop-suffix"_expect);
294 
295  CHECK (actions[21].code == TextTemplate::Code::LOOP); // the loop-end code, where evaluation will consider the next iteration
296  CHECK (actions[21].val == ""_expect);
297  CHECK (actions[21].refIDX == 12 ); // IDX ≡ 12 points back to the opening ITER code
298 
299  CHECK (actions[22].code == TextTemplate::Code::JUMP); // if however the iteration is complete, evaluation will jump over the "else" section
300  CHECK (actions[22].val == ""_expect);
301  CHECK (actions[22].refIDX == 24 );
302 
303  CHECK (actions[23].code == TextTemplate::Code::TEXT); // this static else-segment will appear whenever no iteration takes place
304  CHECK (actions[23].val == ""_expect); // Note: in this example there is an ${else}-tag, yet the content is empty
305 
306  CHECK (actions[24].code == TextTemplate::Code::TEXT); // a final static segment after the last active tag
307  CHECK (actions[24].val == " tail...\n"_expect);
308  CHECK (actions[24].refIDX == 0);
309 
310 
311 
312  VERIFY_FAIL ("TextTemplate spec without active placeholders"
313  , TextTemplate::compile("O tempora O mores"));
314 
315  VERIFY_FAIL ("Tag without key: ...horror ${<placeholder> |↯|}"
316  , TextTemplate::compile("horror ${ } vacui"));
317 
318  VERIFY_FAIL (" ...horror ${if <conditional> |↯|}"
319  , TextTemplate::compile("horror ${if} late"));
320 
321  VERIFY_FAIL (" ...horror ${for <data-id> |↯|}"
322  , TextTemplate::compile("horror ${for} all"));
323 
324  VERIFY_FAIL ("Misplaced ...horror |↯|${else}"
325  , TextTemplate::compile("horror ${else} deaf"));
326 
327  VERIFY_FAIL ("unqualified \"end\" without logic-keyword"
328  , TextTemplate::compile("horror without ${end}"));
329 
330  VERIFY_FAIL ("Unbalanced Logic: expect ${end ?? } -- found ...horror ${end |↯|for }"
331  , TextTemplate::compile("horror ${end for} ever"));
332 
333  VERIFY_FAIL ("Unbalanced Logic: expect ${end for free} -- found ... horror ${end |↯|if }"
334  , TextTemplate::compile("${for free} horror ${end if}"));
335 
336  VERIFY_FAIL ("Unbalanced Logic: expect ${end for free} -- found ... yet ${end |↯|for me}"
337  , TextTemplate::compile("${if wee} horror ${for free} yet ${end for me}"));
338 
339  VERIFY_FAIL ("Conflicting ... precipitous ${else} ⟷ ... callous |↯|${else}"
340  , TextTemplate::compile("${if smarmy} precipitous ${else} callous ${else} horror"));
341 
342  VERIFY_FAIL ("Unclosed Logic tags: |↯|${end if sleazy} missing"
343  , TextTemplate::compile("${if sleazy} precipitous ${else} horror"));
344 
345  VERIFY_FAIL ("Unclosed Logic tags: |↯|${end for horror} missing"
346  , TextTemplate::compile("${for horror}${if flimsy} atrocious ${end if} precipitous"));
347  }
348 
349 
350 
352  void
354  {
355  string wonder = "${a} / ${b} = (${a} + ${b})/${a} ≕ ${phi}";
356  TextTemplate temple{wonder};
357  CHECK (join(temple.keys()) == "a, b, a, b, a, phi"_expect);
358 
359  auto insta = temple.submit (string{"phi=Φ, b=b, a=a"});
360  CHECK (not isnil(insta));
361  CHECK (join(insta,"⁐") == "⁐a⁐ / ⁐b⁐ = (⁐a⁐ + ⁐b⁐)/⁐a⁐ ≕ ⁐Φ⁐"_expect);
362 
363  CHECK (temple.render("phi=Φ,a=μ,b=ν") == "μ / ν = (μ + ν)/μ ≕ Φ"_expect );
364  CHECK (temple.render("phi=schmuh,a=8,b=5") == "8 / 5 = (8 + 5)/8 ≕ schmuh"_expect);
365  CHECK (temple.render("phi=1.6180,a=55,b=34") == "55 / 34 = (55 + 34)/55 ≕ 1.6180"_expect);
366  }
367 
368 
369 
373  void
375  {
376  TextTemplate t1{"Value ${if val}= ${val} ${else}missing${endif}..."};
377 
378  CHECK (t1.render("val=55") == "Value = 55 ..."_expect );
379  CHECK (t1.render("val=\"\"") == "Value missing..."_expect); // empty value counts as false
380  CHECK (t1.render("val=\" \"") == "Value = ..."_expect ); // one space counts as content (=true)
381  CHECK (t1.render("val=false") == "Value missing..."_expect); // various bool-false tokens recognised
382  CHECK (t1.render("val=NO" ) == "Value missing..."_expect);
383  CHECK (t1.render("val= 0 " ) == "Value missing..."_expect);
384  CHECK (t1.render("val=true") == "Value = true ..."_expect); // bool true token treated as content
385  CHECK (t1.render("vol=high") == "Value missing..."_expect); // missing key treated as false
386 
387 
388  TextTemplate t2{"Solution${if val} is ${val} ${endif val}..."};
389  CHECK (t2.render("val=42") == "Solution is 42 ..."_expect );
390  CHECK (t2.render("nil=42") == "Solution..."_expect );
391 
392 
393  TextTemplate t3{" 1 ${if a} 2 ${if b} 3 ${else} ${b} ${endif b} 4 ${else}${if a} 5 ${else} ${a} ${endif a}${endif a} 6 "};
394  CHECK (t3.render("a=2,b=3") == " 1 2 3 4 6 "_expect ); // ^^^^^ Note can never be true here
395  CHECK (t3.render("a=2,b=0") == " 1 2 0 4 6 "_expect );
396  CHECK (t3.render("a=0,b=3") == " 1 0 6 "_expect ); // thus if a ≙ false we see only 1 ${a} 6
397  CHECK (t3.render("a=0,b=0") == " 1 0 6 "_expect );
398  }
399 
400 
401 
412  void
414  {
415  TextTemplate t1{"▶${for i} ${x} ▷${else} ∅${end for} ◇ ${i} ▶"};
416 
417  CHECK (t1.render("i=\"1,2,3\", i.1.x=3, i.2.x=5, i.3.x=8 ") == "▶ 3 ▷ 5 ▷ 8 ▷ ◇ 1,2,3 ▶"_expect ); // fully defined
418  CHECK (t1.render("i=\"3,1,2\", i.1.x=3, i.2.x=5, i.3.x=8 ") == "▶ 8 ▷ 3 ▷ 5 ▷ ◇ 3,1,2 ▶"_expect ); // order changed
419  CHECK (t1.render("i=\"3,2,3\", i.1.x=3, i.2.x=5, i.3.x=8 ") == "▶ 8 ▷ 5 ▷ 8 ▷ ◇ 3,2,3 ▶"_expect ); // duplicate entities
420  CHECK (t1.render("i=\"3,2,1\", i.2.x=5, i.3.x=8 ") == "▶ 8 ▷ 5 ▷ ▷ ◇ 3,2,1 ▶"_expect ); // missing key for entity-1
421  CHECK (t1.render("i=\"3,2,1\", x=↯, i.2.x=5, i.3.x=8 ") == "▶ 8 ▷ 5 ▷ ↯ ▷ ◇ 3,2,1 ▶"_expect ); // top-level key "x" partially shadowed
422  CHECK (t1.render("i=\"p,q,r\", x=↯, i.q.x=5, i.3.x=8 ") == "▶ ↯ ▷ 5 ▷ ↯ ▷ ◇ p,q,r ▶"_expect ); // arbitrary names for the entities
423  CHECK (t1.render("i= 0 , x=↯, i.q.x=5, i.3.x=8 ") == "▶ ∅ ◇ 0 ▶"_expect ); // "0" is false, thus no iteration
424  CHECK (t1.render(" x=↯, i.q.x=5, i.3.x=8 ") == "▶ ∅ ◇ ▶"_expect ); // no binding for iteration-control key i
425 
426 
427  TextTemplate t2{"▶${for i}${if x}${for j}${x}▷${else}${x}●${end for j}${end if x} 🔁 ${end for i} ▶"};
428 
429  CHECK (t2.render("i=\"1,2\",j=\"1,2\", x=1 , i.1.j.1.x=11, i.1.j.2.x=12, i.2.j.1.x=21, i.2.j.2.x=22") == "▶11▷12▷ 🔁 21▷22▷ 🔁 ▶"_expect );
430  CHECK (t2.render("i=\"1,2\",j=\"1,2\", i.1.x=1, i.1.j.1.x=11, i.1.j.2.x=12, i.2.j.1.x=21, i.2.j.2.x=22") == "▶11▷12▷ 🔁 🔁 ▶"_expect );
431  CHECK (t2.render("i=\"1,2\" , x=00 , i.1.j.1.x=11, i.1.j.2.x=12, i.2.j.1.x=21, i.2.j.2.x=22") == "▶00● 🔁 00● 🔁 ▶"_expect );
432  CHECK (t2.render("i=\"1,2\" , x=00 , i.1.x =10, i.2.x =20, ") == "▶10● 🔁 20● 🔁 ▶"_expect );
433  CHECK (t2.render(" j=\"1,2\" ") == "▶ ▶"_expect );
434  CHECK (t2.render(" ") == "▶ ▶"_expect );
435  }
436 
437 
438 
443  void
445  {
446  MapS data{{"a","5"}
447  ,{"i","p,q,r"}
448  ,{"i.p.a","11"}
449  ,{"i.q.a","22"}
450  ,{"i.q.aa","222"}};
451 
452  auto binding = text_template::DataSource{data};
453  CHECK (meta::typeStr(binding) == "text_template::DataSource<map<string, string>, void>"_expect );
454  CHECK ( binding.contains("a"));
455  CHECK (not binding.contains("b"));
456  CHECK (binding.retrieveContent("a") == "5"_expect );
457  CHECK (binding.retrieveContent("i") == "p,q,r"_expect );
458  CHECK (binding.retrieveContent("i.q.aa") == "222"_expect );
459  CHECK (not binding.isSubScope());
460 
461  auto it = binding.getSequence("i");
462  CHECK (it);
463  CHECK (*it == "i.p."_expect );
464  CHECK (meta::typeStr(it) == "IterExplorer<IterableDecorator<CheckedCore<iter_explorer::Transformer<iter_explorer::BaseAdapter<RegexSearchIter>, string> > > >"_expect );
465 
466  auto subBind = binding.openContext(it);
467  CHECK (subBind.isSubScope());
468  CHECK ((meta::is_same<decltype(binding),decltype(subBind)>()));
469  CHECK ( subBind.contains("a"));
470  CHECK (not subBind.contains("b"));
471  CHECK (not subBind.contains("aa"));
472  CHECK ( subBind.contains("i"));
473  CHECK (subBind.retrieveContent("i") == "p,q,r"_expect );
474  CHECK (subBind.retrieveContent("a") == "11"_expect );
475 
476  ++it;
477  CHECK (it);
478  CHECK (*it == "i.q."_expect );
479 
480  // Note: existing sub-ctx is not automatically linked to the Iterator
481  CHECK (subBind.retrieveContent("a") == "11"_expect );
482  // ...rather need to open a new sub-ctx explicitly
483  subBind = binding.openContext(it);
484  CHECK (subBind.isSubScope());
485  CHECK (subBind.contains("a"));
486  CHECK (subBind.contains("aa"));
487  CHECK (subBind.retrieveContent("a") == "22"_expect );
488  CHECK (subBind.retrieveContent("aa") == "222"_expect);
489  CHECK (subBind.retrieveContent("i.p.a") == "11"_expect );
490  CHECK (subBind.retrieveContent("i.q.a") == "22"_expect );
491 
492  ++it;
493  CHECK (it);
494  CHECK (*it == "i.r."_expect );
495 
496  subBind = binding.openContext(it);
497  CHECK ( subBind.contains("a"));
498  CHECK (not subBind.contains("aa"));
499  CHECK (subBind.retrieveContent("a") == "5"_expect );
500  CHECK (subBind.retrieveContent("i.p.a") == "11"_expect );
501  CHECK (subBind.retrieveContent("i.q.a") == "22"_expect );
502 
503  ++it;
504  CHECK (isnil (it));
505  VERIFY_ERROR (ITER_EXHAUST, *it);
506  }
507 
508 
519  void
521  {
522  auto root = MakeRec()
523  .set("a", 5)
524  .set("i", MakeRec()
525  .scope( MakeRec()
526  .set("a", 11)
527  .genNode()
528  , MakeRec()
529  .set("a", 22)
530  .set("aa", 222)
531  .genNode()
532  , MakeRec()
533  /*——empty——*/
534  .genNode()
535  ))
536  .genNode();
537 
538  auto binding = text_template::DataSource{root};
539  CHECK (meta::typeStr(binding) == "text_template::DataSource<GenNode, void>"_expect );
540  CHECK ( binding.contains("a"));
541  CHECK (not binding.contains("b"));
542  CHECK (binding.retrieveContent("a") == "5"_expect );
543  CHECK (binding.retrieveContent("i") == "{|{a=11}, {a=22, aa=222}, {}}"_expect );
544  CHECK (not binding.isSubScope());
545 
546  auto it = binding.getSequence("i");
547  CHECK (it);
548  CHECK (renderCompact(*it) == "{a=11}");
549  CHECK (*it == root.data.get<Rec>().get("i").data.get<Rec>().child(0));
550 
551  auto subBind = binding.openContext(it);
552  CHECK (subBind.isSubScope());
553  CHECK ((meta::is_same<decltype(binding),decltype(subBind)>()));
554  CHECK ( subBind.contains("a"));
555  CHECK (not subBind.contains("b"));
556  CHECK (not subBind.contains("aa"));
557  CHECK ( subBind.contains("i"));
558  CHECK (subBind.retrieveContent("i") == "{|{a=11}, {a=22, aa=222}, {}}"_expect );
559  CHECK (subBind.retrieveContent("a") == "11"_expect );
560 
561  ++it;
562  CHECK (it);
563  CHECK (renderCompact(*it) == "{a=22, aa=222}");
564  CHECK (subBind.retrieveContent("a") == "11"_expect );
565 
566  subBind = binding.openContext(it);
567  CHECK (subBind.isSubScope());
568  CHECK (subBind.contains("a"));
569  CHECK (subBind.contains("aa"));
570  CHECK (subBind.retrieveContent("a") == "22"_expect );
571  CHECK (subBind.retrieveContent("aa") == "222"_expect);
572 
573  ++it;
574  CHECK (it);
575  CHECK (renderCompact(*it) == "{}");
576 
577  subBind = binding.openContext(it);
578  CHECK ( subBind.contains("a"));
579  CHECK (not subBind.contains("aa"));
580  CHECK (subBind.retrieveContent("a") == "5"_expect );
581 
582  ++it;
583  CHECK (isnil (it));
584  VERIFY_ERROR (ITER_EXHAUST, *it);
585 
586 
587  TextTemplate tt{"${for i}a=${a} ${if aa}and aa=${aa} ${endif}${endfor}."};
588  CHECK (tt.render(root) == "a=11 a=22 and aa=222 a=5 ."_expect);
589  }
590  };
591 
592  LAUNCHER (TextTemplate_test, "unit common");
593 
594 
595 }} // namespace lib::test
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
Definition: run.hpp:40
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Text template substitution engine.
A front-end for using printf-style formatting.
Implementation namespace for support and library code.
A minimalistic text templating engine with flexible data binding.
Simplistic test class runner.
represents text content
A collection of frequently used helper functions to support unit testing.
static ActionSeq compile(string const &)
wrapped regex iterator to allow usage in foreach loops
Definition: regex.hpp:40
static string apply(string spec, DAT const &data)
one-shot shorthand: compile a template and apply it to the given data
A complement to allow instantiation of a TextTemplate with ETD data.
object-like record of data.
Definition: record.hpp:141
generic data element node within a tree
Definition: gen-node.hpp:222
#define VERIFY_FAIL(FAILURE_MSG, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises a std::exception, which additionally contains some FAI...