1 /* Copyright (c) 2014, Vsevolod Stakhov
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 read_stdin (char **buf)
32 int size = BUFSIZ, remain, ret;
43 while ((ret = read (STDIN_FILENO, p, remain - 1)) > 0) {
48 *buf = realloc (*buf, size * 2);
65 perform_test (const ucl_object_t *schema, const ucl_object_t *obj,
66 struct ucl_schema_error *err)
68 const ucl_object_t *valid, *data, *description;
71 data = ucl_object_find_key (obj, "data");
72 description = ucl_object_find_key (obj, "description");
73 valid = ucl_object_find_key (obj, "valid");
75 if (data == NULL || description == NULL || valid == NULL) {
76 fprintf (stdout, "Bad test case\n");
80 match = ucl_object_validate (schema, data, err);
81 if (match != ucl_object_toboolean (valid)) {
82 fprintf (stdout, "Test case '%s' failed (expected %s): '%s'\n",
83 ucl_object_tostring (description),
84 ucl_object_toboolean (valid) ? "valid" : "invalid",
86 fprintf (stdout, "%s\n", ucl_object_emit (data, UCL_EMIT_CONFIG));
87 fprintf (stdout, "%s\n", ucl_object_emit (schema, UCL_EMIT_CONFIG));
95 perform_tests (const ucl_object_t *obj)
97 struct ucl_schema_error err;
98 ucl_object_iter_t iter = NULL;
99 const ucl_object_t *schema, *tests, *description, *test;
101 if (obj->type != UCL_OBJECT) {
102 fprintf (stdout, "Bad test case\n");
106 schema = ucl_object_find_key (obj, "schema");
107 tests = ucl_object_find_key (obj, "tests");
108 description = ucl_object_find_key (obj, "description");
110 if (schema == NULL || tests == NULL || description == NULL) {
111 fprintf (stdout, "Bad test case\n");
115 memset (&err, 0, sizeof (err));
117 while ((test = ucl_iterate_object (tests, &iter, true)) != NULL) {
118 if (!perform_test (schema, test, &err)) {
119 fprintf (stdout, "Test suite '%s' failed\n",
120 ucl_object_tostring (description));
129 main (int argc, char **argv)
132 struct ucl_parser *parser;
133 ucl_object_t *obj = NULL;
134 const ucl_object_t *elt;
135 ucl_object_iter_t iter = NULL;
138 if (read_stdin (&buf) == -1) {
142 parser = ucl_parser_new (0);
144 ucl_parser_add_string (parser, buf, 0);
146 if (ucl_parser_get_error (parser) != NULL) {
147 fprintf (stdout, "Error occurred: %s\n", ucl_parser_get_error (parser));
151 obj = ucl_parser_get_object (parser);
152 ucl_parser_free (parser);
154 while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) {
155 ret = perform_tests (elt);
161 ucl_object_unref (obj);