From: Andrea Zagli Date: Mon, 10 Aug 2020 14:16:53 +0000 (+0200) Subject: Added test for json provider (zakcgi). X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=c406a25eb524654c416491405ec635a974e71eba;p=zakform%2Ftests Added test for json provider (zakcgi). --- diff --git a/.gitignore b/.gitignore index 3c8bf8f..ad2e828 100644 --- a/.gitignore +++ b/.gitignore @@ -61,4 +61,5 @@ get_element_validators get_validators gtk_gdaex gtk_ini -gtk_get_xml \ No newline at end of file +gtk_get_xml +json_ini \ No newline at end of file diff --git a/configure.ac b/configure.ac index 1ad9da0..52bd788 100644 --- a/configure.ac +++ b/configure.ac @@ -39,6 +39,7 @@ PKG_CHECK_MODULES(ZAKFORMTESTS, [libzakform libzakformini libzakcgi libzakformcgi + libzakformjson libzakutilsjsonxml]) AC_SUBST(ZAKFORMTESTS_CFLAGS) diff --git a/src/Makefile.am b/src/Makefile.am index 38c9b67..4bc5bcc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,4 +19,5 @@ noinst_PROGRAMS = \ get_validators \ gtk_gdaex \ gtk_ini \ - gtk_get_xml + gtk_get_xml \ + json_ini diff --git a/src/json_ini.c b/src/json_ini.c new file mode 100644 index 0000000..e18c361 --- /dev/null +++ b/src/json_ini.c @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2020 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include + +#include +#include +#include +#include + +int +main (int argc, char *argv[]) +{ + ZakCgiMain *zakcgimain; + + GString *str; + + GValue *val; + gchar *filename; + gint id; + + ZakFormCgiForm *form; + + ZakFormJsonProvider *provider; + + setlocale (LC_ALL, ""); + + zakcgimain = zak_cgi_main_new (); + + str = g_string_new ("\n" + "\n" + "\n" + "\n" + "\n" + "Form test\n" + "\n" + "\n" + "\n" + "\n" + "
\n" + "\n"); + + val = zak_cgi_main_get_parameter (zakcgimain, "filename"); + if (val != NULL) + { + filename = (gchar *)g_value_get_string (val); + } + else + { + filename = g_strdup (""); + } + + val = zak_cgi_main_get_parameter (zakcgimain, "id"); + if (val != NULL) + { + id = strtol (g_value_get_string (val), NULL, 10); + } + else + { + id = 0; + } + + form = zak_form_cgi_form_new (zakcgimain, + "method", "post", + "action", g_strdup_printf ("json_ini?filename=%s", filename), + NULL); + + if (zak_form_form_load_from_file (ZAK_FORM_FORM (form), XMLDIR "/cgi.xml")) + { + zak_form_cgi_form_set_bootstrap_version (form, "4"); + + if (zak_cgi_main_is_post (zakcgimain)) + { + /* validating the form */ + zak_form_cgi_form_bind (form); + if (zak_form_form_is_valid (ZAK_FORM_FORM (form))) + { + g_string_append (str, "
Form is valid!!!
"); + provider = zak_form_json_provider_new_from_file (filename); + if (provider != NULL) + { + zak_form_form_insert (ZAK_FORM_FORM (form), ZAK_FORM_IPROVIDER (provider)); + g_object_unref (provider); + } + } + else + { + g_string_append (str, "
Form is not valid!!!"); + + guint m; + GPtrArray *ar_messages = zak_form_form_get_messages (ZAK_FORM_FORM (form)); + if (ar_messages != NULL) + { + if (ar_messages->len > 0) + { + g_string_append (str, "\n
    \n"); + for (m = 0; m < ar_messages->len; m++) + { + g_string_append_printf (str, "\n
  • %s
  • ", (gchar *)g_ptr_array_index (ar_messages, m)); + } + g_string_append (str, "\n
\n"); + } + } + + g_string_append (str, "
"); + + g_string_append (str, zak_form_cgi_form_render (form)); + } + } + else + { + zak_form_form_clear (ZAK_FORM_FORM (form)); + if (id > 0) + { + zak_form_element_set_value (zak_form_form_get_element_by_id (ZAK_FORM_FORM (form), "id"), g_strdup_printf ("%d", id)); + + provider = zak_form_json_provider_new_from_file (filename); + if (provider != NULL) + { + zak_form_form_load (ZAK_FORM_FORM (form), ZAK_FORM_IPROVIDER (provider)); + g_object_unref (provider); + } + } + g_string_append (str, zak_form_cgi_form_render (form)); + } + } + + g_string_append (str, + "\n
\n" + "\n" + "\n" + "\n" + "\n" + ""); + + zak_cgi_main_out (NULL, str->str); + + return 0; +}