Skip to content
Snippets Groups Projects
Commit 6a65c52f9b06 authored by Tina Müller (tinita)'s avatar Tina Müller (tinita)
Browse files

Add -h and --flow (on|off|keep) to run-*-test-suite (#187)

With `--flow (keep|on)` run-parser-test-suite will output:

    +MAP {}
    +SEQ []

run-emitter-test-suite will then emit flow style collections if requested:

    echo 'foo: [bar, {x: y}]' | ./tests/run-parser-test-suite | ./tests/run-emitter-test-suite
    echo 'foo: [bar, {x: y}]' | ./tests/run-parser-test-suite \
        --flow keep | ./tests/run-emitter-test-suite --flow keep

Also: add that yaml_private.h include again that I had thrown out. Needed
for printing directives.
Wonder if there is a way to create a directive without using the private api.
parent e95c59bd566c
No related branches found
No related tags found
No related merge requests found
# Testing the Parser and Emitter
There are several programs to test the parser and emitter.
## Parser
echo 'foo: bar' | ./tests/run-parser-test-suite
This will output the parsing events in yaml-test-suite format:
+STR
+DOC
+MAP
=VAL :foo
=VAL :bar
-MAP
-DOC
-STR
For flow style events, you have to enable it with the `--flow` option:
echo '{ foo: bar }' | ./tests/run-parser-test-suite --flow keep
...
+MAP {}
...
In the future, this will be the default.
You can also explicitly disable this style with `--flow off`, or output
flow style always, with `--flow on`.
## Emitter
run-emitter-test-suite takes yaml-test-suite event format and emits YAML.
./tests/run-parser-test-suite ... | ./tests/run-emitter-test-suite
## Options
* `--directive (1.1|1.2)`
Prints a version directive before every document.
* `--flow on`
Will emit the whole document in flow style.
* `--flow off`
Will emit the whole document in block style.
* `--flow keep`
Will emit block/flow style like in the original document.
Example:
```
% echo 'foo: [bar, {x: y}]' |
./tests/run-parser-test-suite --flow keep |
./tests/run-emitter-test-suite --flow keep
foo: [bar, {x: y}]
```
......@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "../src/yaml_private.h"
int get_line(FILE * input, char *line);
char *get_anchor(char sigil, char *line, char *anchor);
......@@ -23,9 +24,10 @@
int foundfile = 0;
int i = 0;
int minor = 0;
int flow = -1; /** default no flow style collections */
for (i = 1; i < argc; i++) {
if (strncmp(argv[i], "--help", 6) == 0)
return usage(0);
if (strncmp(argv[i], "-h", 2) == 0)
return usage(0);
......@@ -26,10 +28,23 @@
for (i = 1; i < argc; i++) {
if (strncmp(argv[i], "--help", 6) == 0)
return usage(0);
if (strncmp(argv[i], "-h", 2) == 0)
return usage(0);
if (strncmp(argv[i], "--directive", 11) == 0) {
if (strncmp(argv[i], "--flow", 6) == 0) {
if (i+1 == 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], "--directive", 11) == 0) {
if (i+1 == argc)
return usage(1);
i++;
......@@ -47,6 +62,7 @@
}
if (minor) {
version_directive = YAML_MALLOC_STATIC(yaml_version_directive_t);
version_directive->major = 1;
version_directive->minor = minor;
}
......@@ -69,6 +85,7 @@
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);
......@@ -77,7 +94,7 @@
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) {
......@@ -81,7 +98,7 @@
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) {
......@@ -85,5 +102,10 @@
ok = yaml_document_end_event_initialize(&event, implicit);
}
else if (strncmp(line, "+MAP", 4) == 0) {
style = YAML_BLOCK_MAPPING_STYLE;
if (flow == 1)
style = YAML_FLOW_MAPPING_STYLE;
else if (flow == 0 && 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 *)
......@@ -88,8 +110,8 @@
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) {
......@@ -91,7 +113,12 @@
}
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 (flow == 1)
style = YAML_FLOW_MAPPING_STYLE;
else if (flow == 0 && 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 *)
......@@ -96,6 +123,6 @@
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);
......@@ -258,6 +285,6 @@
}
int usage(int ret) {
fprintf(stderr, "Usage: run-emitter-test-suite [--directive (1.1|1.2)] [<input-file>]\n");
fprintf(stderr, "Usage: run-emitter-test-suite [--directive (1.1|1.2)] [--flow (on|off|keep)] [<input-file>]\n");
return ret;
}
......@@ -4,9 +4,10 @@
#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;
......@@ -7,7 +8,10 @@
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;
......@@ -13,3 +17,28 @@
if (argc == 1)
for (i = 1; i < argc; i++) {
if (strncmp(argv[i], "--flow", 6) == 0) {
if (i+1 == 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;
......@@ -15,9 +44,4 @@
input = stdin;
else if (argc == 2)
input = fopen(argv[1], "rb");
else {
fprintf(stderr, "Usage: libyaml-parser [<input-file>]\n");
return 1;
}
assert(input);
......@@ -63,6 +87,10 @@
}
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)
......@@ -73,6 +101,10 @@
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)
......@@ -150,3 +182,8 @@
printf("%c", c);
}
}
int usage(int ret) {
fprintf(stderr, "Usage: libyaml-parser [--flow (on|off|keep)] [<input-file>]\n");
return ret;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment