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)) > 0) {
47 *buf = realloc (*buf, size * 2);
61 perform_test (const ucl_object_t *schema, const ucl_object_t *obj,
62 struct ucl_schema_error *err)
64 const const ucl_object_t *valid, *data, *description;
67 data = ucl_object_find_key (obj, "data");
68 description = ucl_object_find_key (obj, "description");
69 valid = ucl_object_find_key (obj, "valid");
71 if (data == NULL || description == NULL || valid == NULL) {
72 fprintf (stdout, "Bad test case\n");
76 match = ucl_object_validate (schema, data, err);
77 if (match != ucl_object_toboolean (valid)) {
78 fprintf (stdout, "Test case '%s' failed (expected %s): '%s'\n",
79 ucl_object_tostring (description),
80 ucl_object_toboolean (valid) ? "valid" : "invalid",
89 perform_tests (const ucl_object_t *obj)
91 struct ucl_schema_error err;
92 ucl_object_iter_t iter = NULL;
93 const ucl_object_t *schema, *tests, *description, *test;
95 if (obj->type != UCL_OBJECT) {
96 fprintf (stdout, "Bad test case\n");
100 schema = ucl_object_find_key (obj, "schema");
101 tests = ucl_object_find_key (obj, "tests");
102 description = ucl_object_find_key (obj, "description");
104 if (schema == NULL || tests == NULL || description == NULL) {
105 fprintf (stdout, "Bad test case\n");
109 memset (&err, 0, sizeof (err));
111 while ((test = ucl_iterate_object (tests, &iter, true)) != NULL) {
112 if (!perform_test (schema, test, &err)) {
113 fprintf (stdout, "Test suite '%s' failed\n",
114 ucl_object_tostring (description));
123 main (int argc, char **argv)
126 struct ucl_parser *parser;
127 ucl_object_t *obj = NULL;
128 const ucl_object_t *elt;
129 ucl_object_iter_t iter = NULL;
132 if (read_stdin (&buf) == -1) {
136 parser = ucl_parser_new (0);
138 ucl_parser_add_string (parser, buf, 0);
140 if (ucl_parser_get_error (parser) != NULL) {
141 fprintf (stdout, "Error occurred: %s\n", ucl_parser_get_error (parser));
145 obj = ucl_parser_get_object (parser);
146 ucl_parser_free (parser);
148 while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) {
149 ret = perform_tests (elt);
155 ucl_object_unref (obj);