n_pretty regression: JSON, XML/HTML and JavaScript reformatting.
n_pretty regression: JSON, XML/HTML and JavaScript reformatting.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int opt = 0;
while ((opt = getopt(argc, argv, "hvV:")) != EOF) {
switch (opt) {
case 'V':
if (!strncmp("LOG_NULL", optarg, 8))
else if (!strncmp("LOG_NOTICE", optarg, 10))
else if (!strncmp("LOG_INFO", optarg, 8))
else if (!strncmp("LOG_ERR", optarg, 7))
else if (!strncmp("LOG_DEBUG", optarg, 9))
else {
fprintf(stderr, "Unknown log level %s\n", optarg);
exit(1);
}
break;
case 'v':
fprintf(stderr, "ex_pretty\n");
exit(1);
default:
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
exit(1);
}
}
}
if (!cond) {
}
}
if (!out || !out->
data || !strstr(out->
data, needle)) {
n_log(
LOG_ERR,
"%s: output does not contain '%s' (got: %s)",
label, needle, (out && out->
data) ? out->
data :
"NULL");
}
}
int main(
int argc,
char** argv) {
{
expect_true(
"json truncated returns NULL", out == NULL);
}
{
out =
n_pretty_xml(
"<div><br><!-- note --><span>x</span></div>");
out =
n_pretty_xml(
"<html><script>if(a<b){c();}</script></html>");
expect_true(
"html script returns output", out != NULL);
expect_true(
"xml quoted gt returns output", out != NULL);
expect_true(
"xml non-markup returns NULL", out == NULL);
}
{
N_STR* out =
n_pretty_js(
"function f(a){if(a){return 1;}else{return 2;}}var x=f(1);");
out =
n_pretty_js(
"var s=\"x;y{z}\";var re=/a\\/b;{/g;var t='a;b';");
expect_true(
"js template returns output", out != NULL);
out =
n_pretty_js(
"function t(a){return /x;{/.test(a);}");
expect_true(
"js keyword regex returns output", out != NULL);
expect_contains(
"js regex after return intact", out,
"return /x;{/.test(a);");
expect_true(
"js division returns output", out != NULL);
expect_true(
"js brace-semicolon returns output", out != NULL);
}
return 1;
}
printf("ex_pretty: all tests passed\n");
return 0;
}
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.