xref: /freebsd/contrib/libxo/doc/example.rst (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1
2Examples
3========
4
5Unit Test
6---------
7
8Here is one of the unit tests as an example::
9
10    int
11    main (int argc, char **argv)
12    {
13        static char base_grocery[] = "GRO";
14        static char base_hardware[] = "HRD";
15        struct item {
16            const char *i_title;
17            int i_sold;
18            int i_instock;
19            int i_onorder;
20            const char *i_sku_base;
21            int i_sku_num;
22        };
23        struct item list[] = {
24            { "gum", 1412, 54, 10, base_grocery, 415 },
25            { "rope", 85, 4, 2, base_hardware, 212 },
26            { "ladder", 0, 2, 1, base_hardware, 517 },
27            { "bolt", 4123, 144, 42, base_hardware, 632 },
28            { "water", 17, 14, 2, base_grocery, 2331 },
29            { NULL, 0, 0, 0, NULL, 0 }
30        };
31        struct item list2[] = {
32            { "fish", 1321, 45, 1, base_grocery, 533 },
33        };
34        struct item *ip;
35        xo_info_t info[] = {
36            { "in-stock", "number", "Number of items in stock" },
37            { "name", "string", "Name of the item" },
38            { "on-order", "number", "Number of items on order" },
39            { "sku", "string", "Stock Keeping Unit" },
40            { "sold", "number", "Number of items sold" },
41            { NULL, NULL, NULL },
42        };
43        int info_count = (sizeof(info) / sizeof(info[0])) - 1;
44
45        argc = xo_parse_args(argc, argv);
46        if (argc < 0)
47            exit(EXIT_FAILURE);
48
49        xo_set_info(NULL, info, info_count);
50
51        xo_open_container_h(NULL, "top");
52
53        xo_open_container("data");
54        xo_open_list("item");
55
56        for (ip = list; ip->i_title; ip++) {
57            xo_open_instance("item");
58
59            xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
60            xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
61                    ip->i_sold, ip->i_sold ? ".0" : "");
62            xo_emit("{P:   }{Lwc:In stock}{:in-stock/%u}\n",
63                    ip->i_instock);
64            xo_emit("{P:   }{Lwc:On order}{:on-order/%u}\n",
65                    ip->i_onorder);
66            xo_emit("{P:   }{L:SKU}: {q:sku/%s-000-%u}\n",
67                    ip->i_sku_base, ip->i_sku_num);
68
69            xo_close_instance("item");
70        }
71
72        xo_close_list("item");
73        xo_close_container("data");
74
75        xo_open_container("data");
76        xo_open_list("item");
77
78        for (ip = list2; ip->i_title; ip++) {
79            xo_open_instance("item");
80
81            xo_emit("{L:Item} '{:name/%s}':\n", ip->i_title);
82            xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
83                    ip->i_sold, ip->i_sold ? ".0" : "");
84            xo_emit("{P:   }{Lwc:In stock}{:in-stock/%u}\n",
85                    ip->i_instock);
86            xo_emit("{P:   }{Lwc:On order}{:on-order/%u}\n",
87                    ip->i_onorder);
88            xo_emit("{P:   }{L:SKU}: {q:sku/%s-000-%u}\n",
89                    ip->i_sku_base, ip->i_sku_num);
90
91            xo_close_instance("item");
92        }
93
94        xo_close_list("item");
95        xo_close_container("data");
96
97        xo_close_container_h(NULL, "top");
98
99        return 0;
100    }
101
102Text output::
103
104    % ./testxo --libxo text
105    Item 'gum':
106       Total sold: 1412.0
107       In stock: 54
108       On order: 10
109       SKU: GRO-000-415
110    Item 'rope':
111       Total sold: 85.0
112       In stock: 4
113       On order: 2
114       SKU: HRD-000-212
115    Item 'ladder':
116       Total sold: 0
117       In stock: 2
118       On order: 1
119       SKU: HRD-000-517
120    Item 'bolt':
121       Total sold: 4123.0
122       In stock: 144
123       On order: 42
124       SKU: HRD-000-632
125    Item 'water':
126       Total sold: 17.0
127       In stock: 14
128       On order: 2
129       SKU: GRO-000-2331
130    Item 'fish':
131       Total sold: 1321.0
132       In stock: 45
133       On order: 1
134       SKU: GRO-000-533
135
136JSON output::
137
138    % ./testxo --libxo json,pretty
139    "top": {
140      "data": {
141        "item": [
142          {
143            "name": "gum",
144            "sold": 1412.0,
145            "in-stock": 54,
146            "on-order": 10,
147            "sku": "GRO-000-415"
148          },
149          {
150            "name": "rope",
151            "sold": 85.0,
152            "in-stock": 4,
153            "on-order": 2,
154            "sku": "HRD-000-212"
155          },
156          {
157            "name": "ladder",
158            "sold": 0,
159            "in-stock": 2,
160            "on-order": 1,
161            "sku": "HRD-000-517"
162          },
163          {
164            "name": "bolt",
165            "sold": 4123.0,
166            "in-stock": 144,
167            "on-order": 42,
168            "sku": "HRD-000-632"
169          },
170          {
171            "name": "water",
172            "sold": 17.0,
173            "in-stock": 14,
174            "on-order": 2,
175            "sku": "GRO-000-2331"
176          }
177        ]
178      },
179      "data": {
180        "item": [
181          {
182            "name": "fish",
183            "sold": 1321.0,
184            "in-stock": 45,
185            "on-order": 1,
186            "sku": "GRO-000-533"
187          }
188        ]
189      }
190    }
191
192XML output::
193
194    % ./testxo --libxo pretty,xml
195    <top>
196      <data>
197        <item>
198          <name>gum</name>
199          <sold>1412.0</sold>
200          <in-stock>54</in-stock>
201          <on-order>10</on-order>
202          <sku>GRO-000-415</sku>
203        </item>
204        <item>
205          <name>rope</name>
206          <sold>85.0</sold>
207          <in-stock>4</in-stock>
208          <on-order>2</on-order>
209          <sku>HRD-000-212</sku>
210        </item>
211        <item>
212          <name>ladder</name>
213          <sold>0</sold>
214          <in-stock>2</in-stock>
215          <on-order>1</on-order>
216          <sku>HRD-000-517</sku>
217        </item>
218        <item>
219          <name>bolt</name>
220          <sold>4123.0</sold>
221          <in-stock>144</in-stock>
222          <on-order>42</on-order>
223          <sku>HRD-000-632</sku>
224        </item>
225        <item>
226          <name>water</name>
227          <sold>17.0</sold>
228          <in-stock>14</in-stock>
229          <on-order>2</on-order>
230          <sku>GRO-000-2331</sku>
231        </item>
232      </data>
233      <data>
234        <item>
235          <name>fish</name>
236          <sold>1321.0</sold>
237          <in-stock>45</in-stock>
238          <on-order>1</on-order>
239          <sku>GRO-000-533</sku>
240        </item>
241      </data>
242    </top>
243
244HMTL output::
245
246    % ./testxo --libxo pretty,html
247    <div class="line">
248      <div class="label">Item</div>
249      <div class="text"> '</div>
250      <div class="data" data-tag="name">gum</div>
251      <div class="text">':</div>
252    </div>
253    <div class="line">
254      <div class="padding">   </div>
255      <div class="label">Total sold</div>
256      <div class="text">: </div>
257      <div class="data" data-tag="sold">1412.0</div>
258    </div>
259    <div class="line">
260      <div class="padding">   </div>
261      <div class="label">In stock</div>
262      <div class="decoration">:</div>
263      <div class="padding"> </div>
264      <div class="data" data-tag="in-stock">54</div>
265    </div>
266    <div class="line">
267      <div class="padding">   </div>
268      <div class="label">On order</div>
269      <div class="decoration">:</div>
270      <div class="padding"> </div>
271      <div class="data" data-tag="on-order">10</div>
272    </div>
273    <div class="line">
274      <div class="padding">   </div>
275      <div class="label">SKU</div>
276      <div class="text">: </div>
277      <div class="data" data-tag="sku">GRO-000-415</div>
278    </div>
279    <div class="line">
280      <div class="label">Item</div>
281      <div class="text"> '</div>
282      <div class="data" data-tag="name">rope</div>
283      <div class="text">':</div>
284    </div>
285    <div class="line">
286      <div class="padding">   </div>
287      <div class="label">Total sold</div>
288      <div class="text">: </div>
289      <div class="data" data-tag="sold">85.0</div>
290    </div>
291    <div class="line">
292      <div class="padding">   </div>
293      <div class="label">In stock</div>
294      <div class="decoration">:</div>
295      <div class="padding"> </div>
296      <div class="data" data-tag="in-stock">4</div>
297    </div>
298    <div class="line">
299      <div class="padding">   </div>
300      <div class="label">On order</div>
301      <div class="decoration">:</div>
302      <div class="padding"> </div>
303      <div class="data" data-tag="on-order">2</div>
304    </div>
305    <div class="line">
306      <div class="padding">   </div>
307      <div class="label">SKU</div>
308      <div class="text">: </div>
309      <div class="data" data-tag="sku">HRD-000-212</div>
310    </div>
311    <div class="line">
312      <div class="label">Item</div>
313      <div class="text"> '</div>
314      <div class="data" data-tag="name">ladder</div>
315      <div class="text">':</div>
316    </div>
317    <div class="line">
318      <div class="padding">   </div>
319      <div class="label">Total sold</div>
320      <div class="text">: </div>
321      <div class="data" data-tag="sold">0</div>
322    </div>
323    <div class="line">
324      <div class="padding">   </div>
325      <div class="label">In stock</div>
326      <div class="decoration">:</div>
327      <div class="padding"> </div>
328      <div class="data" data-tag="in-stock">2</div>
329    </div>
330    <div class="line">
331      <div class="padding">   </div>
332      <div class="label">On order</div>
333      <div class="decoration">:</div>
334      <div class="padding"> </div>
335      <div class="data" data-tag="on-order">1</div>
336    </div>
337    <div class="line">
338      <div class="padding">   </div>
339      <div class="label">SKU</div>
340      <div class="text">: </div>
341      <div class="data" data-tag="sku">HRD-000-517</div>
342    </div>
343    <div class="line">
344      <div class="label">Item</div>
345      <div class="text"> '</div>
346      <div class="data" data-tag="name">bolt</div>
347      <div class="text">':</div>
348    </div>
349    <div class="line">
350      <div class="padding">   </div>
351      <div class="label">Total sold</div>
352      <div class="text">: </div>
353      <div class="data" data-tag="sold">4123.0</div>
354    </div>
355    <div class="line">
356      <div class="padding">   </div>
357      <div class="label">In stock</div>
358      <div class="decoration">:</div>
359      <div class="padding"> </div>
360      <div class="data" data-tag="in-stock">144</div>
361    </div>
362    <div class="line">
363      <div class="padding">   </div>
364      <div class="label">On order</div>
365      <div class="decoration">:</div>
366      <div class="padding"> </div>
367      <div class="data" data-tag="on-order">42</div>
368    </div>
369    <div class="line">
370      <div class="padding">   </div>
371      <div class="label">SKU</div>
372      <div class="text">: </div>
373      <div class="data" data-tag="sku">HRD-000-632</div>
374    </div>
375    <div class="line">
376      <div class="label">Item</div>
377      <div class="text"> '</div>
378      <div class="data" data-tag="name">water</div>
379      <div class="text">':</div>
380    </div>
381    <div class="line">
382      <div class="padding">   </div>
383      <div class="label">Total sold</div>
384      <div class="text">: </div>
385      <div class="data" data-tag="sold">17.0</div>
386    </div>
387    <div class="line">
388      <div class="padding">   </div>
389      <div class="label">In stock</div>
390      <div class="decoration">:</div>
391      <div class="padding"> </div>
392      <div class="data" data-tag="in-stock">14</div>
393    </div>
394    <div class="line">
395      <div class="padding">   </div>
396      <div class="label">On order</div>
397      <div class="decoration">:</div>
398      <div class="padding"> </div>
399      <div class="data" data-tag="on-order">2</div>
400    </div>
401    <div class="line">
402      <div class="padding">   </div>
403      <div class="label">SKU</div>
404      <div class="text">: </div>
405      <div class="data" data-tag="sku">GRO-000-2331</div>
406    </div>
407    <div class="line">
408      <div class="label">Item</div>
409      <div class="text"> '</div>
410      <div class="data" data-tag="name">fish</div>
411      <div class="text">':</div>
412    </div>
413    <div class="line">
414      <div class="padding">   </div>
415      <div class="label">Total sold</div>
416      <div class="text">: </div>
417      <div class="data" data-tag="sold">1321.0</div>
418    </div>
419    <div class="line">
420      <div class="padding">   </div>
421      <div class="label">In stock</div>
422      <div class="decoration">:</div>
423      <div class="padding"> </div>
424      <div class="data" data-tag="in-stock">45</div>
425    </div>
426    <div class="line">
427      <div class="padding">   </div>
428      <div class="label">On order</div>
429      <div class="decoration">:</div>
430      <div class="padding"> </div>
431      <div class="data" data-tag="on-order">1</div>
432    </div>
433    <div class="line">
434      <div class="padding">   </div>
435      <div class="label">SKU</div>
436      <div class="text">: </div>
437      <div class="data" data-tag="sku">GRO-000-533</div>
438    </div>
439
440HTML output with xpath and info flags::
441
442    % ./testxo --libxo pretty,html,xpath,info
443    <div class="line">
444      <div class="label">Item</div>
445      <div class="text"> '</div>
446      <div class="data" data-tag="name"
447           data-xpath="/top/data/item/name" data-type="string"
448           data-help="Name of the item">gum</div>
449      <div class="text">':</div>
450    </div>
451    <div class="line">
452      <div class="padding">   </div>
453      <div class="label">Total sold</div>
454      <div class="text">: </div>
455      <div class="data" data-tag="sold"
456           data-xpath="/top/data/item/sold" data-type="number"
457           data-help="Number of items sold">1412.0</div>
458    </div>
459    <div class="line">
460      <div class="padding">   </div>
461      <div class="label">In stock</div>
462      <div class="decoration">:</div>
463      <div class="padding"> </div>
464      <div class="data" data-tag="in-stock"
465           data-xpath="/top/data/item/in-stock" data-type="number"
466           data-help="Number of items in stock">54</div>
467    </div>
468    <div class="line">
469      <div class="padding">   </div>
470      <div class="label">On order</div>
471      <div class="decoration">:</div>
472      <div class="padding"> </div>
473      <div class="data" data-tag="on-order"
474           data-xpath="/top/data/item/on-order" data-type="number"
475           data-help="Number of items on order">10</div>
476    </div>
477    <div class="line">
478      <div class="padding">   </div>
479      <div class="label">SKU</div>
480      <div class="text">: </div>
481      <div class="data" data-tag="sku"
482           data-xpath="/top/data/item/sku" data-type="string"
483           data-help="Stock Keeping Unit">GRO-000-415</div>
484    </div>
485    <div class="line">
486      <div class="label">Item</div>
487      <div class="text"> '</div>
488      <div class="data" data-tag="name"
489           data-xpath="/top/data/item/name" data-type="string"
490           data-help="Name of the item">rope</div>
491      <div class="text">':</div>
492    </div>
493    <div class="line">
494      <div class="padding">   </div>
495      <div class="label">Total sold</div>
496      <div class="text">: </div>
497      <div class="data" data-tag="sold"
498           data-xpath="/top/data/item/sold" data-type="number"
499           data-help="Number of items sold">85.0</div>
500    </div>
501    <div class="line">
502      <div class="padding">   </div>
503      <div class="label">In stock</div>
504      <div class="decoration">:</div>
505      <div class="padding"> </div>
506      <div class="data" data-tag="in-stock"
507           data-xpath="/top/data/item/in-stock" data-type="number"
508           data-help="Number of items in stock">4</div>
509    </div>
510    <div class="line">
511      <div class="padding">   </div>
512      <div class="label">On order</div>
513      <div class="decoration">:</div>
514      <div class="padding"> </div>
515      <div class="data" data-tag="on-order"
516           data-xpath="/top/data/item/on-order" data-type="number"
517           data-help="Number of items on order">2</div>
518    </div>
519    <div class="line">
520      <div class="padding">   </div>
521      <div class="label">SKU</div>
522      <div class="text">: </div>
523      <div class="data" data-tag="sku"
524           data-xpath="/top/data/item/sku" data-type="string"
525           data-help="Stock Keeping Unit">HRD-000-212</div>
526    </div>
527    <div class="line">
528      <div class="label">Item</div>
529      <div class="text"> '</div>
530      <div class="data" data-tag="name"
531           data-xpath="/top/data/item/name" data-type="string"
532           data-help="Name of the item">ladder</div>
533      <div class="text">':</div>
534    </div>
535    <div class="line">
536      <div class="padding">   </div>
537      <div class="label">Total sold</div>
538      <div class="text">: </div>
539      <div class="data" data-tag="sold"
540           data-xpath="/top/data/item/sold" data-type="number"
541           data-help="Number of items sold">0</div>
542    </div>
543    <div class="line">
544      <div class="padding">   </div>
545      <div class="label">In stock</div>
546      <div class="decoration">:</div>
547      <div class="padding"> </div>
548      <div class="data" data-tag="in-stock"
549           data-xpath="/top/data/item/in-stock" data-type="number"
550           data-help="Number of items in stock">2</div>
551    </div>
552    <div class="line">
553      <div class="padding">   </div>
554      <div class="label">On order</div>
555      <div class="decoration">:</div>
556      <div class="padding"> </div>
557      <div class="data" data-tag="on-order"
558           data-xpath="/top/data/item/on-order" data-type="number"
559           data-help="Number of items on order">1</div>
560    </div>
561    <div class="line">
562      <div class="padding">   </div>
563      <div class="label">SKU</div>
564      <div class="text">: </div>
565      <div class="data" data-tag="sku"
566           data-xpath="/top/data/item/sku" data-type="string"
567           data-help="Stock Keeping Unit">HRD-000-517</div>
568    </div>
569    <div class="line">
570      <div class="label">Item</div>
571      <div class="text"> '</div>
572      <div class="data" data-tag="name"
573           data-xpath="/top/data/item/name" data-type="string"
574           data-help="Name of the item">bolt</div>
575      <div class="text">':</div>
576    </div>
577    <div class="line">
578      <div class="padding">   </div>
579      <div class="label">Total sold</div>
580      <div class="text">: </div>
581      <div class="data" data-tag="sold"
582           data-xpath="/top/data/item/sold" data-type="number"
583           data-help="Number of items sold">4123.0</div>
584    </div>
585    <div class="line">
586      <div class="padding">   </div>
587      <div class="label">In stock</div>
588      <div class="decoration">:</div>
589      <div class="padding"> </div>
590      <div class="data" data-tag="in-stock"
591           data-xpath="/top/data/item/in-stock" data-type="number"
592           data-help="Number of items in stock">144</div>
593    </div>
594    <div class="line">
595      <div class="padding">   </div>
596      <div class="label">On order</div>
597      <div class="decoration">:</div>
598      <div class="padding"> </div>
599      <div class="data" data-tag="on-order"
600           data-xpath="/top/data/item/on-order" data-type="number"
601           data-help="Number of items on order">42</div>
602    </div>
603    <div class="line">
604      <div class="padding">   </div>
605      <div class="label">SKU</div>
606      <div class="text">: </div>
607      <div class="data" data-tag="sku"
608           data-xpath="/top/data/item/sku" data-type="string"
609           data-help="Stock Keeping Unit">HRD-000-632</div>
610    </div>
611    <div class="line">
612      <div class="label">Item</div>
613      <div class="text"> '</div>
614      <div class="data" data-tag="name"
615           data-xpath="/top/data/item/name" data-type="string"
616           data-help="Name of the item">water</div>
617      <div class="text">':</div>
618    </div>
619    <div class="line">
620      <div class="padding">   </div>
621      <div class="label">Total sold</div>
622      <div class="text">: </div>
623      <div class="data" data-tag="sold"
624           data-xpath="/top/data/item/sold" data-type="number"
625           data-help="Number of items sold">17.0</div>
626    </div>
627    <div class="line">
628      <div class="padding">   </div>
629      <div class="label">In stock</div>
630      <div class="decoration">:</div>
631      <div class="padding"> </div>
632      <div class="data" data-tag="in-stock"
633           data-xpath="/top/data/item/in-stock" data-type="number"
634           data-help="Number of items in stock">14</div>
635    </div>
636    <div class="line">
637      <div class="padding">   </div>
638      <div class="label">On order</div>
639      <div class="decoration">:</div>
640      <div class="padding"> </div>
641      <div class="data" data-tag="on-order"
642           data-xpath="/top/data/item/on-order" data-type="number"
643           data-help="Number of items on order">2</div>
644    </div>
645    <div class="line">
646      <div class="padding">   </div>
647      <div class="label">SKU</div>
648      <div class="text">: </div>
649      <div class="data" data-tag="sku"
650           data-xpath="/top/data/item/sku" data-type="string"
651           data-help="Stock Keeping Unit">GRO-000-2331</div>
652    </div>
653    <div class="line">
654      <div class="label">Item</div>
655      <div class="text"> '</div>
656      <div class="data" data-tag="name"
657           data-xpath="/top/data/item/name" data-type="string"
658           data-help="Name of the item">fish</div>
659      <div class="text">':</div>
660    </div>
661    <div class="line">
662      <div class="padding">   </div>
663      <div class="label">Total sold</div>
664      <div class="text">: </div>
665      <div class="data" data-tag="sold"
666           data-xpath="/top/data/item/sold" data-type="number"
667           data-help="Number of items sold">1321.0</div>
668    </div>
669    <div class="line">
670      <div class="padding">   </div>
671      <div class="label">In stock</div>
672      <div class="decoration">:</div>
673      <div class="padding"> </div>
674      <div class="data" data-tag="in-stock"
675           data-xpath="/top/data/item/in-stock" data-type="number"
676           data-help="Number of items in stock">45</div>
677    </div>
678    <div class="line">
679      <div class="padding">   </div>
680      <div class="label">On order</div>
681      <div class="decoration">:</div>
682      <div class="padding"> </div>
683      <div class="data" data-tag="on-order"
684           data-xpath="/top/data/item/on-order" data-type="number"
685           data-help="Number of items on order">1</div>
686    </div>
687    <div class="line">
688      <div class="padding">   </div>
689      <div class="label">SKU</div>
690      <div class="text">: </div>
691      <div class="data" data-tag="sku"
692           data-xpath="/top/data/item/sku" data-type="string"
693           data-help="Stock Keeping Unit">GRO-000-533</div>
694    </div>
695