Debian Codegraph @

yaml-filter/tests/test-paths.c

raw symbols SCIP
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2020 Red Hat Inc., Durham, North Carolina.
3
4 #include <ctype.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "yaml-path.h"
9
10 static void
11 rstrip (char *s)
12 {
13 // This will strip whitespace and explicit document ending
14 // to cope with emitter behavior for libyaml < 0.2
15 char *pos = s + strlen(s) - 1;
16 while (pos > s && (isspace((unsigned char)*pos) || *pos == '.')) {
17 *pos = 0;
18 pos--;
19 }
20 }
21
22 static const char*
23 yp_event_name (yaml_event_type_t event_type)
24 {
25 switch (event_type) {
26 case YAML_NO_EVENT:
27 return "no-event";
28 case YAML_STREAM_START_EVENT:
29 return "stream-start-event";
30 case YAML_STREAM_END_EVENT:
31 return "stream-end-event";
32 case YAML_DOCUMENT_START_EVENT:
33 return "document-start-event";
34 case YAML_DOCUMENT_END_EVENT:
35 return "document-end-event";
36 case YAML_ALIAS_EVENT:
37 return "alias-event";
38 case YAML_SCALAR_EVENT:
39 return "scalar-event";
40 case YAML_SEQUENCE_START_EVENT:
41 return "sequence-start-event";
42 case YAML_SEQUENCE_END_EVENT:
43 return "sequence-end-event";
44 case YAML_MAPPING_START_EVENT:
45 return "mapping-start-event";
46 case YAML_MAPPING_END_EVENT:
47 return "mapping-end-event";
48 default:
49 return "unknown-event";
50 }
51 }
52
53
54 static char*
55 yaml;
56
57 #define YAML_STRING_LEN 2048
58 static char
59 yaml_out[YAML_STRING_LEN] = {0};
60
61 static size_t
62 yaml_out_len = 0;
63
64 static int
65 test_result = 0;
66
67
68 static int
69 yp_run (char *path)
70 {
71 yaml_parser_t parser;
72 yaml_emitter_t emitter;
73 int res = 0;
74
75 yaml_path_t *yp = yaml_path_create();
76 if (yaml_path_parse(yp, path)) {
77 printf("Path error: %s\n", yaml_path_error_get(yp)->message);
78 yaml_path_destroy(yp);
79 return 1;
80 }
81
82 //char spath[YAML_STRING_LEN] = {0};
83 //yaml_path_snprint(yp, spath, YAML_STRING_LEN);
84 //printf("(%s) ", spath);
85
86 yaml_emitter_initialize(&emitter);
87 yaml_parser_initialize(&parser);
88
89 yaml_parser_set_input_string(&parser, (const unsigned char *)yaml, strlen(yaml));
90 memset(yaml_out, 0, YAML_STRING_LEN);
91 yaml_emitter_set_output_string(&emitter, (unsigned char *)yaml_out, YAML_STRING_LEN, &yaml_out_len);
92 yaml_emitter_set_width(&emitter, -1);
93
94 yaml_event_t event;
95 yaml_event_type_t event_type, prev_event_type = YAML_NO_EVENT;
96 yaml_path_filter_result_t result, prev_result = 0;
97
98 do {
99 if (!yaml_parser_parse(&parser, &event)) {
100 switch (parser.error) {
101 case YAML_MEMORY_ERROR:
102 printf("Memory error: Not enough memory for parsing\n");
103 break;
104 case YAML_READER_ERROR:
105 if (parser.problem_value != -1) {
106 printf("Reader error: %s: #%X at %ld\n", parser.problem, parser.problem_value, (long)parser.problem_offset);
107 } else {
108 printf("Reader error: %s at %ld\n", parser.problem, (long)parser.problem_offset);
109 }
110 break;
111 case YAML_SCANNER_ERROR:
112 if (parser.context) {
113 printf("Scanner error: %s at line %d, column %d\n%s at line %d, column %d\n", parser.context, (int)parser.context_mark.line+1, (int)parser.context_mark.column+1, parser.problem, (int)parser.problem_mark.line+1, (int)parser.problem_mark.column+1);
114 } else {
115 printf("Scanner error: %s at line %d, column %d\n", parser.problem, (int)parser.problem_mark.line+1, (int)parser.problem_mark.column+1);
116 }
117 break;
118 case YAML_PARSER_ERROR:
119 if (parser.context) {
120 printf("Parser error: %s at line %d, column %d\n%s at line %d, column %d\n", parser.context, (int)parser.context_mark.line+1, (int)parser.context_mark.column+1, parser.problem, (int)parser.problem_mark.line+1, (int)parser.problem_mark.column+1);
121 } else {
122 printf("Parser error: %s at line %d, column %d\n", parser.problem, (int)parser.problem_mark.line+1, (int)parser.problem_mark.column+1);
123 }
124 break;
125 default:
126 printf("Internal error\n");
127 break;
128 }
129 res = 1;
130 goto error;
131 } else {
132 event_type = event.type;
133 result = yaml_path_filter_event(yp, &parser, &event);
134 if (result == YAML_PATH_FILTER_RESULT_OUT) {
135 yaml_event_delete(&event);
136 } else {
137 if ((prev_event_type == YAML_DOCUMENT_START_EVENT && event_type == YAML_DOCUMENT_END_EVENT)
138 || (prev_result == YAML_PATH_FILTER_RESULT_IN_DANGLING_KEY
139 && (event_type == YAML_MAPPING_END_EVENT || event_type == YAML_SEQUENCE_END_EVENT || result == YAML_PATH_FILTER_RESULT_IN_DANGLING_KEY))) {
140 yaml_event_t null_event= {0};
141 yaml_scalar_event_initialize(&null_event, NULL, (yaml_char_t *)"!!null", (yaml_char_t *)"null", 4, 1, 0, YAML_ANY_SCALAR_STYLE);
142 yaml_emitter_emit(&emitter, &null_event);
143 }
144 prev_result = result;
145 prev_event_type = event_type;
146 if (!yaml_emitter_emit(&emitter, &event)) {
147 yaml_emitter_flush(&emitter);
148 printf("%s --> Error after '%s': ", yaml_out, yp_event_name(event.type));
149 switch (emitter.error)
150 {
151 case YAML_MEMORY_ERROR:
152 printf("Memory error (Not enough memory for emitting)");
153 break;
154 case YAML_WRITER_ERROR:
155 printf("Writer error (%s)", emitter.problem);
156 break;
157 case YAML_EMITTER_ERROR:
158 printf("Emitter error (%s)", emitter.problem);
159 break;
160 default:
161 printf("Internal error");
162 break;
163 }
164 res = 2;
165 goto error;
166 }
167 }
168 }
169 } while (event_type != YAML_STREAM_END_EVENT);
170
171 error:
172 yaml_parser_delete(&parser);
173 yaml_emitter_delete(&emitter);
174
175 yaml_path_destroy(yp);
176
177 return res;
178 }
179
180 #define ASCII_ERR "\033[0;33m"
181 #define ASCII_RST "\033[0;0m"
182
183 static void
184 yp_test (char *path, char *yaml_exp)
185 {
186 printf("%s "ASCII_ERR, path);
187 if (!yp_run(path)) {
188 rstrip(yaml_out);
189 if (!strcmp(yaml_exp, yaml_out)) {
190 printf(ASCII_RST"(%s): OK\n", yaml_exp);
191 return;
192 }
193 printf("(%s != %s)"ASCII_RST": FAILED\n", yaml_exp, yaml_out);
194 } else {
195 printf(ASCII_RST": ERROR\n");
196 }
197 test_result++;
198 }
199
200
201 int main (int argc, char *argv[])
202 {
203 (void) argc; (void) argv; // Yep, we don't need them
204
205 yaml =
206 "{"
207 "first: {"
208 "'Map': {1: '1'},"
209 "'Nop': 0,"
210 "'Yep': '1',"
211 "'Arr': ["
212 "[11, 12],"
213 "2,"
214 "['31', '32'],"
215 "[4, 5, 6, 7, 8, 9],"
216 "{'k': 'val', 0: 0}"
217 "]"
218 "},"
219 "second: ["
220 "{'abc': &anc [1, 2], 'def': [11, 22], 'abcdef': 2, 'z': *anc, 'q': 'Q'},"
221 "{'abc': [3, 4], 'def': {'z': '!'}, 'abcdef': 4, 'z': 'zzz'}"
222 "],"
223 "3rd: ["
224 "{'a': {'A': [0, 1], 'AA': [2, 3]}, 'b': {'A': [10, 11], 'BB': [9, 8]}},"
225 "{'z': {'A': [0, 1], 'BB': [22, 33]}},"
226 "&x {'q': [1, 2]},"
227 "]"
228 "}";
229
230 // Path Expected filtered YAML result
231
232 yp_test("$.first.Map", "{1: '1'}");
233 yp_test(".first", "{'Map': {1: '1'}, 'Nop': 0, 'Yep': '1', 'Arr': [[11, 12], 2, ['31', '32'], [4, 5, 6, 7, 8, 9], {'k': 'val', 0: 0}]}");
234 yp_test(".first.Nop", "0");
235 yp_test(".first.Arr", "[[11, 12], 2, ['31', '32'], [4, 5, 6, 7, 8, 9], {'k': 'val', 0: 0}]");
236 yp_test(".first.Arr[0]", "[11, 12]");
237 yp_test(".first.Arr[1]", "2");
238 yp_test(".first.Arr[2][0]", "'31'");
239 yp_test(".first.Arr[3][:]", "[4, 5, 6, 7, 8, 9]");
240 yp_test(".first.Arr[:][:]", "[[11, 12], ['31', '32'], [4, 5, 6, 7, 8, 9]]");
241 yp_test(".first.Arr[4].k", "'val'");
242 yp_test(".first.Arr[:][0]", "[11, '31', 4]");
243 yp_test(".first.Arr[:].k", "['val']");
244 yp_test(".first.Arr[:][2]", "[6]");
245 yp_test(".first.Arr[:][0,1]", "[[11, 12], ['31', '32'], [4, 5]]");
246 yp_test(".first.Arr[:][1]", "[12, '32', 5]");
247 yp_test(".second[2].abc", "null");
248 yp_test(".second[0].z", "*anc");
249 yp_test("&anc", "&anc [1, 2]");
250 yp_test("&anc[0]", "1");
251 yp_test(".first['Nop','Yep']", "{'Nop': 0, 'Yep': '1'}");
252 yp_test(".second[0]['abc','def'][0]","{'abc': 1, 'def': 11}");
253 yp_test(".second[:]['abc','def'][0]","[{'abc': 1, 'def': 11}, {'abc': 3, 'def': null}]");
254 yp_test(".second[:]['abc','def'].z", "[{'abc': null, 'def': null}, {'abc': null, 'def': '!'}]");
255 yp_test(".second[:][*].z", "[{'abc': null, 'def': null, 'abcdef': null, 'z': null, 'q': null}, {'abc': null, 'def': '!', 'abcdef': null, 'z': null}]");
256 yp_test(".second[:]['abc','q']", "[{'abc': &anc [1, 2], 'q': 'Q'}, {'abc': [3, 4]}]");
257 yp_test(".second[:]['abc','def'][:]","[{'abc': &anc [1, 2], 'def': [11, 22]}, {'abc': [3, 4], 'def': null}]");
258 yp_test(".second[0]['abc','def']", "{'abc': &anc [1, 2], 'def': [11, 22]}");
259 yp_test(".3rd[:].*.*[:]", "[{'a': {'A': [0, 1], 'AA': [2, 3]}, 'b': {'A': [10, 11], 'BB': [9, 8]}}, {'z': {'A': [0, 1], 'BB': [22, 33]}}, &x {'q': null}]");
260
261 return test_result;
262 }