41 while ((opt = getopt(argc, argv,
"hvV:")) != EOF) {
44 if (!strncmp(
"LOG_NULL", optarg, 8))
46 else if (!strncmp(
"LOG_NOTICE", optarg, 10))
48 else if (!strncmp(
"LOG_INFO", optarg, 8))
50 else if (!strncmp(
"LOG_ERR", optarg, 7))
52 else if (!strncmp(
"LOG_DEBUG", optarg, 9))
55 fprintf(stderr,
"Unknown log level %s\n", optarg);
60 fprintf(stderr,
"ex_pretty\n");
63 fprintf(stderr,
"Usage: %s [-V LOG_LEVEL]\n", argv[0]);
77 if (!out || !out->
data || !strstr(out->
data, needle)) {
78 n_log(
LOG_ERR,
"%s: output does not contain '%s' (got: %s)",
79 label, needle, (out && out->
data) ? out->
data :
"NULL");
84int main(
int argc,
char** argv) {
91 expect_true(
"json valid returns output", out != NULL);
97 expect_true(
"json garbage returns NULL", out == NULL);
101 expect_true(
"json truncated returns NULL", out == NULL);
108 expect_true(
"xml valid returns output", out != NULL);
116 out =
n_pretty_xml(
"<div><br><!-- note --><span>x</span></div>");
124 out =
n_pretty_xml(
"<html><script>if(a<b){c();}</script></html>");
125 expect_true(
"html script returns output", out != NULL);
132 expect_true(
"xml quoted gt returns output", out != NULL);
133 expect_contains(
"xml quoted gt kept in tag", out,
"<a title=\"1 > 0\">");
137 expect_true(
"xml non-markup returns NULL", out == NULL);
143 N_STR* out =
n_pretty_js(
"function f(a){if(a){return 1;}else{return 2;}}var x=f(1);");
144 expect_true(
"js valid returns output", out != NULL);
148 expect_contains(
"js statement gets its own line", out,
"\nvar x=f(1);\n");
152 out =
n_pretty_js(
"var s=\"x;y{z}\";var re=/a\\/b;{/g;var t='a;b';");
153 expect_true(
"js literals return output", out != NULL);
160 out =
n_pretty_js(
"const q=`a{b}\n${x({})}`;done();");
161 expect_true(
"js template returns output", out != NULL);
167 out =
n_pretty_js(
"function t(a){return /x;{/.test(a);}");
168 expect_true(
"js keyword regex returns output", out != NULL);
169 expect_contains(
"js regex after return intact", out,
"return /x;{/.test(a);");
173 out =
n_pretty_js(
"var x=a/b;var y=f(1)/2;done();");
174 expect_true(
"js division returns output", out != NULL);
182 expect_contains(
"js for header on one line", out,
"for(i=0;i<3;i++){");
187 expect_true(
"js brace-semicolon returns output", out != NULL);
203 printf(
"ex_pretty: all tests passed\n");
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
static void expect_contains(const char *label, const N_STR *out, const char *needle)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
N_STR * n_pretty_xml(const char *text)
Re-indent an XML or HTML document (one tag or text run per line).
N_STR * n_pretty_js(const char *text)
Conservatively re-indent JavaScript (newlines at top-level braces and semicolons, two-space indent by...
N_STR * n_pretty_json(const char *text)
Reformat a JSON document with cJSON (parse + print).
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
A box including a string and his lenght.
Common headers and low-level functions & define.
Lexical pretty-printers for JSON, XML/HTML and JavaScript text.
N_STR and string function declaration.