Skip to content

Commit

Permalink
Add -h and --flow (on|off|keep) to run-parser-test-suite
Browse files Browse the repository at this point in the history
with `--keepflow (keep|on)` it will output:

    +MAP {}
    +SEQ []

run-emitter-test-suite will then emit flow style collections.

    echo 'foo: [bar, {x: y}]' | ./tests/run-parser-test-suite  | ./tests/run-emitter-test-suite
  • Loading branch information
perlpunk committed May 31, 2020
1 parent 9deee01 commit 7d21819
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
15 changes: 11 additions & 4 deletions tests/run-emitter-test-suite.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int main(int argc, char *argv[])
char anchor[256];
char tag[256];
int implicit;
int style;

if (strncmp(line, "+STR", 4) == 0) {
ok = yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
Expand All @@ -77,25 +78,31 @@ int main(int argc, char *argv[])
ok = yaml_stream_end_event_initialize(&event);
}
else if (strncmp(line, "+DOC", 4) == 0) {
implicit = strncmp(line, "+DOC ---", 8) != 0;
implicit = strncmp(line+4, " ---", 4) != 0;
ok = yaml_document_start_event_initialize(&event, version_directive, NULL, NULL, implicit);
}
else if (strncmp(line, "-DOC", 4) == 0) {
implicit = strncmp(line, "-DOC ...", 8) != 0;
implicit = strncmp(line+4, " ...", 4) != 0;
ok = yaml_document_end_event_initialize(&event, implicit);
}
else if (strncmp(line, "+MAP", 4) == 0) {
style = YAML_BLOCK_MAPPING_STYLE;
if (strncmp(line+5, "{}", 2) == 0)
style = YAML_FLOW_MAPPING_STYLE;
ok = yaml_mapping_start_event_initialize(&event, (yaml_char_t *)
get_anchor('&', line, anchor), (yaml_char_t *)
get_tag(line, tag), 0, YAML_BLOCK_MAPPING_STYLE);
get_tag(line, tag), 0, style);
}
else if (strncmp(line, "-MAP", 4) == 0) {
ok = yaml_mapping_end_event_initialize(&event);
}
else if (strncmp(line, "+SEQ", 4) == 0) {
style = YAML_BLOCK_SEQUENCE_STYLE;
if (strncmp(line+5, "[]", 2) == 0)
style = YAML_FLOW_SEQUENCE_STYLE;
ok = yaml_sequence_start_event_initialize(&event, (yaml_char_t *)
get_anchor('&', line, anchor), (yaml_char_t *)
get_tag(line, tag), 0, YAML_BLOCK_SEQUENCE_STYLE);
get_tag(line, tag), 0, style);
}
else if (strncmp(line, "-SEQ", 4) == 0) {
ok = yaml_sequence_end_event_initialize(&event);
Expand Down
49 changes: 43 additions & 6 deletions tests/run-parser-test-suite.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,44 @@
#include <assert.h>

void print_escaped(yaml_char_t * str, size_t length);
int usage(int ret);

int main(int argc, char *argv[])
{
FILE *input;
yaml_parser_t parser;
yaml_event_t event;
int flow = -1; // default no flow style collections
int i = 0;
int foundfile = 0;

if (argc == 1)
for (i = 1; i < argc; i++) {
if (strncmp(argv[i], "--flow", 6) == 0) {
if (i == argc)
return usage(1);
i++;
if (strncmp(argv[i], "keep", 4) == 0)
flow = 0;
else if (strncmp(argv[i], "on", 2) == 0)
flow = 1;
else if (strncmp(argv[i], "off", 3) == 0)
flow = -1;
else
return usage(1);
}
else if (strncmp(argv[i], "--help", 6) == 0)
return usage(0);
else if (strncmp(argv[i], "-h", 2) == 0)
return usage(0);
else if (!foundfile) {
input = fopen(argv[i], "rb");
foundfile = 1;
}
else
return usage(1);
}
if (!foundfile) {
input = stdin;
else if (argc == 2)
input = fopen(argv[1], "rb");
else {
fprintf(stderr, "Usage: libyaml-parser [<input-file>]\n");
return 1;
}
assert(input);

Expand Down Expand Up @@ -63,6 +87,10 @@ int main(int argc, char *argv[])
}
else if (type == YAML_MAPPING_START_EVENT) {
printf("+MAP");
if (flow == 0 && event.data.mapping_start.style == YAML_FLOW_MAPPING_STYLE)
printf(" {}");
else if (flow == 1)
printf(" {}");
if (event.data.mapping_start.anchor)
printf(" &%s", event.data.mapping_start.anchor);
if (event.data.mapping_start.tag)
Expand All @@ -73,6 +101,10 @@ int main(int argc, char *argv[])
printf("-MAP\n");
else if (type == YAML_SEQUENCE_START_EVENT) {
printf("+SEQ");
if (flow == 0 && event.data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE)
printf(" []");
else if (flow == 1)
printf(" []");
if (event.data.sequence_start.anchor)
printf(" &%s", event.data.sequence_start.anchor);
if (event.data.sequence_start.tag)
Expand Down Expand Up @@ -150,3 +182,8 @@ void print_escaped(yaml_char_t * str, size_t length)
printf("%c", c);
}
}

int usage(int ret) {
fprintf(stderr, "Usage: libyaml-parser [--flow (on|off|keep)] [<input-file>]\n");
return ret;
}

0 comments on commit 7d21819

Please sign in to comment.