xref: /freebsd/contrib/libxo/tests/core/test_02.c (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
1 /*
2  * Copyright (c) 2014, Juniper Networks, Inc.
3  * All rights reserved.
4  * This SOFTWARE is licensed under the LICENSE provided in the
5  * ../Copyright file. By downloading, installing, copying, or otherwise
6  * using the SOFTWARE, you agree to be bound by the terms of that
7  * LICENSE.
8  * Phil Shafer, July 2014
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdint.h>
14 #include <string.h>
15 
16 #include "xo.h"
17 
18 int
19 main (int argc, char **argv)
20 {
21     argc = xo_parse_args(argc, argv);
22     if (argc < 0)
23 	return 1;
24 
25     for (argc = 1; argv[argc]; argc++) {
26 	if (strcmp(argv[argc], "xml") == 0)
27 	    xo_set_style(NULL, XO_STYLE_XML);
28 	else if (strcmp(argv[argc], "json") == 0)
29 	    xo_set_style(NULL, XO_STYLE_JSON);
30 	else if (strcmp(argv[argc], "text") == 0)
31 	    xo_set_style(NULL, XO_STYLE_TEXT);
32 	else if (strcmp(argv[argc], "html") == 0)
33 	    xo_set_style(NULL, XO_STYLE_HTML);
34 	else if (strcmp(argv[argc], "pretty") == 0)
35 	    xo_set_flags(NULL, XOF_PRETTY);
36 	else if (strcmp(argv[argc], "xpath") == 0)
37 	    xo_set_flags(NULL, XOF_XPATH);
38 	else if (strcmp(argv[argc], "info") == 0)
39 	    xo_set_flags(NULL, XOF_INFO);
40     }
41 
42     xo_set_flags(NULL, XOF_UNITS); /* Always test w/ this */
43 
44     xo_open_container_h(NULL, "top");
45 
46     xo_open_container("data");
47 
48     xo_emit("{:mbuf-current/%u}/{:mbuf-cache/%u}/{:mbuf-total/%u} "
49 	    "{N:mbufs <&> in use (current\\/cache\\/total)}\n",
50 	    10, 20, 30);
51 
52     xo_emit("{:distance/%u}{Uw:miles} from {:location}\n", 50, "Boston");
53     xo_emit("{:memory/%u}{U:k} left out of {:total/%u}{U:kb}\n", 64, 640);
54     xo_emit("{:memory/%u}{U:/%s} left out of {:total/%u}{U:/%s}\n",
55 	    64, "k", 640, "kilobytes");
56 
57     xo_emit("{T:/before%safter:}\n", "working");
58 
59     xo_emit("{d:some/%s}{:ten/%ju}{:eleven/%ju}\n",
60 	    "string", (uintmax_t) 10, (uintmax_t) 11);
61 
62     xo_emit("{:unknown/%u} "
63 	    "{N:/packet%s here\\/there\\/everywhere}\n",
64 	    1010, "s");
65 
66     xo_emit("({[:/%d}{n:min/15}/{n:cur/20}/{:max/%d}{]:})\n", 30, 125);
67     xo_emit("({[:30}{:min/%u}/{:cur/%u}/{:max/%u}{]:})\n", 15, 20, 125);
68     xo_emit("({[:-30}{n:min/15}/{n:cur/20}/{n:max/125}{]:})\n");
69     xo_emit("({[:}{:min/%u}/{:cur/%u}/{:max/%u}{]:/%d})\n", 15, 20, 125, -30);
70 
71     xo_open_list("flag");
72     xo_emit("{lq:flag/one} {lq:flag/two} {lq:flag/three}\n");
73     xo_close_list("flag");
74 
75     xo_emit("{e:empty-tag/}");
76     xo_emit("1:{qt:t1/%*d} 2:{qt:t2/test%-*u} 3:{qt:t3/%10sx} 4:{qt:t4/x%-*.*s}\n",
77 	    6, 1000, 8, 5000, "ten-long", 10, 10, "test");
78     xo_emit("{E:this is an error}\n");
79     xo_emit("{E:/%s more error%s}\n", "two", "s" );
80     xo_emit("{W:this is an warning}\n");
81     xo_emit("{W:/%s more warning%s}\n", "two", "s" );
82     xo_emit("{L:/V1\\/V2 packet%s}: {:count/%u}\n", "s", 10);
83 
84     int test = 4;
85     xo_emit("{:test/%04d} {L:/tr%s}\n", test, (test == 1) ? "y" : "ies");
86 
87     xo_message("improper use of profanity; %s; %s",
88 	       "ten yard penalty", "first down");
89 
90     xo_error("Shut 'er down, Clancey!  She's a-pumpin' mud!  <>!,\"!<>\n");
91 
92     xo_close_container("data");
93 
94     xo_close_container_h(NULL, "top");
95 
96     xo_finish();
97 
98     return 0;
99 }
100