]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
libpakfire: parser: Add module tests for the parser
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 1 Jun 2019 14:23:42 +0000 (15:23 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 1 Jun 2019 14:23:42 +0000 (15:23 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
tests/libpakfire/parser.c [new file with mode: 0644]
tests/testsuite.h

index 8a426ce5a48c4d3b3ad987c87f1b381fdde8f1a9..a89182f4a79b522a07e8f024858c0228ce6a50ce 100644 (file)
@@ -14,6 +14,7 @@
 /tests/libpakfire/key
 /tests/libpakfire/main
 /tests/libpakfire/makefile
+/tests/libpakfire/parser
 /tmp
 *.py[co]
 /*.tar.bz2
index 02a1946eba96a7c07b6732ec25a0c5367b0f97f5..f947573a1a1572c56d61a9fff94454c63ab8e7bf 100644 (file)
@@ -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 (file)
index 0000000..b47161f
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <string.h>
+
+#include <pakfire/parser.h>
+#include <pakfire/util.h>
+
+#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);
+}
index 10edd4e5772b9c49784d9a26cfa6559c8cd6dffd..686c7363a5e25ff499faf17997273f0f401a9d41 100644 (file)
@@ -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 */