From: Michael Tremer Date: Sat, 1 Jun 2019 14:23:42 +0000 (+0100) Subject: libpakfire: parser: Add module tests for the parser X-Git-Tag: 0.9.28~1285^2~984 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d687eaa8623f43ffa525a3e2ad636057241d195;p=pakfire.git libpakfire: parser: Add module tests for the parser Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index 8a426ce5a..a89182f4a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ /tests/libpakfire/key /tests/libpakfire/main /tests/libpakfire/makefile +/tests/libpakfire/parser /tmp *.py[co] /*.tar.bz2 diff --git a/Makefile.am b/Makefile.am index 02a1946eb..f947573a1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -352,7 +352,8 @@ check_PROGRAMS += \ tests/libpakfire/main \ tests/libpakfire/archive \ tests/libpakfire/key \ - tests/libpakfire/makefile + tests/libpakfire/makefile \ + tests/libpakfire/parser dist_tests_libpakfire_main_SOURCES = \ tests/libpakfire/main.c @@ -397,6 +398,16 @@ tests_libpakfire_makefile_LDADD = \ $(TESTSUITE_LDADD) \ $(PAKFIRE_LIBS) +dist_tests_libpakfire_parser_SOURCES = \ + tests/libpakfire/parser.c + +tests_libpakfire_parser_CPPFLAGS = \ + $(TESTSUITE_CPPFLAGS) + +tests_libpakfire_parser_LDADD = \ + $(TESTSUITE_LDADD) \ + $(PAKFIRE_LIBS) + # ------------------------------------------------------------------------------ lib_LTLIBRARIES += \ diff --git a/tests/libpakfire/parser.c b/tests/libpakfire/parser.c new file mode 100644 index 000000000..b47161f03 --- /dev/null +++ b/tests/libpakfire/parser.c @@ -0,0 +1,113 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2019 Pakfire development team # +# # +# This program 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 3 of the License, or # +# (at your option) any later version. # +# # +# This program 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include + +#include +#include + +#include "../testsuite.h" + +int test_parser(const test_t* t) { + char* value = NULL; + + // Create a new parser + PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL); + + // Retrieve a value that does not exist + value = pakfire_parser_get(parser, "null"); + assert_return(!value, EXIT_FAILURE); + + // Set a value + int r = pakfire_parser_set(parser, "a", "a"); + assert_return(r == 0, EXIT_FAILURE); + + // Retrieve the value again + value = pakfire_parser_get(parser, "a"); + assert_compare(value, "a", EXIT_FAILURE); + + // Append something to the value + r = pakfire_parser_append(parser, "a", "b"); + assert_return(r == 0, EXIT_FAILURE); + + // Retrieve the value again + value = pakfire_parser_get(parser, "a"); + assert_compare(value, "a b", EXIT_FAILURE); + + // Make a child parser + PakfireParser subparser = pakfire_parser_create_child(parser, "child"); + assert_return(subparser, EXIT_FAILURE); + + // Try to get a again + value = pakfire_parser_get(subparser, "a"); + assert_compare(value, "a b", EXIT_FAILURE); + + // Append something to the subparser + r = pakfire_parser_append(subparser, "a", "c"); + assert_return(r == 0, EXIT_FAILURE); + + // The subparser should return "a b c" + value = pakfire_parser_get(subparser, "a"); + assert_compare(value, "a b c", EXIT_FAILURE); + + // The original parser should remain unchanged + value = pakfire_parser_get(parser, "a"); + assert_compare(value, "a b", EXIT_FAILURE); + + // Set another value + r = pakfire_parser_append(subparser, "b", "1"); + assert_return(r == 0, EXIT_FAILURE); + + // Merge the two parsers + pakfire_parser_merge(parser, subparser); + + // Now a should have changed to "a b c" + value = pakfire_parser_get(parser, "a"); + assert_compare(value, "a b c", EXIT_FAILURE); + + // Set a variable + r = pakfire_parser_set(parser, "c", "%{b}"); + assert_return(r == 0, EXIT_FAILURE); + + // Get the value of c + value = pakfire_parser_get(parser, "c"); + assert_compare(value, "1", EXIT_FAILURE); + + // Dump the parser + char* s = pakfire_parser_dump(parser); + printf("%s\n", s); + + // Cleanup + pakfire_parser_unref(subparser); + pakfire_parser_unref(parser); + pakfire_free(value); + + return EXIT_SUCCESS; +} + +int main(int argc, char** argv) { + testsuite_init(); + + testsuite_t* ts = testsuite_create(1); + + testsuite_add_test(ts, "test_parser", test_parser); + + return testsuite_run(ts); +} diff --git a/tests/testsuite.h b/tests/testsuite.h index 10edd4e57..686c7363a 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -63,4 +63,13 @@ int testsuite_run(testsuite_t* ts); } \ } while (0) +#define assert_compare(string, value, r) \ + do { \ + if (strcmp(string, value) != 0) { \ + LOG_ERROR("Failed assertion: " #string " != " #value " %s:%d %s\n", \ + __FILE__, __LINE__, __PRETTY_FUNCTION__); \ + return r; \ + } \ + } while (0) + #endif /* PAKFIRE_TESTSUITE_H */