From: Michael Tremer Date: Wed, 13 Jan 2021 12:59:47 +0000 (+0000) Subject: tests: Give better names to assertions X-Git-Tag: 0.9.28~1285^2~876 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=89ed926c5decefc80d7330494f56f97ab99ed899;p=pakfire.git tests: Give better names to assertions Signed-off-by: Michael Tremer --- diff --git a/tests/libpakfire/arch.c b/tests/libpakfire/arch.c index 3a7aa6e46..6b2737578 100644 --- a/tests/libpakfire/arch.c +++ b/tests/libpakfire/arch.c @@ -28,14 +28,14 @@ static int test_native(const struct test* t) { // First call const char* arch1 = pakfire_arch_native(); - assert_return(arch1, EXIT_FAILURE); + ASSERT(arch1); // Second call const char* arch2 = pakfire_arch_native(); - assert_return(arch2, EXIT_FAILURE); + ASSERT(arch2); // Must be the same pointer - assert_return(arch1 == arch2, EXIT_FAILURE); + ASSERT(arch1 == arch2); return EXIT_SUCCESS; } @@ -44,14 +44,14 @@ static int test_supported(const struct test* t) { int r; r = pakfire_arch_supported("x86_64"); - assert_return(r, EXIT_FAILURE); + ASSERT(r); r = pakfire_arch_supported("i686"); - assert_return(r, EXIT_FAILURE); + ASSERT(r); // Check non-existant architecture r = pakfire_arch_supported("ABC"); - assert_return(!r, EXIT_FAILURE); + ASSERT(!r); return EXIT_SUCCESS; } @@ -61,23 +61,23 @@ static int test_compatible(const struct test* t) { // x86_64 can build i686 r = pakfire_arch_is_compatible("x86_64", "i686"); - assert_return(r, EXIT_FAILURE); + ASSERT(r); // i686 can NOT build i686 r = pakfire_arch_is_compatible("i686", "x86_64"); - assert_return(!r, EXIT_FAILURE); + ASSERT(!r); // x86_64 can build itself r = pakfire_arch_is_compatible("x86_64", "x86_64"); - assert_return(r, EXIT_FAILURE); + ASSERT(r); // x86_64 can NOT build a non-existant architecture r = pakfire_arch_is_compatible("x86_64", "ABC"); - assert_return(!r, EXIT_FAILURE); + ASSERT(!r); // A non-existant architecture cannot build anything r = pakfire_arch_is_compatible("ABC", "x86_64"); - assert_return(!r, EXIT_FAILURE); + ASSERT(!r); return EXIT_SUCCESS; } @@ -86,10 +86,10 @@ static int test_machine(const struct test* t) { char* machine; machine = pakfire_arch_machine("x86_64", "ipfire"); - assert_compare(machine, "x86_64-ipfire-linux-gnu", EXIT_FAILURE); + ASSERT_STRING_EQUALS(machine, "x86_64-ipfire-linux-gnu"); machine = pakfire_arch_machine("x86_64", "IPFIRE"); - assert_compare(machine, "x86_64-ipfire-linux-gnu", EXIT_FAILURE); + ASSERT_STRING_EQUALS(machine, "x86_64-ipfire-linux-gnu"); return EXIT_SUCCESS; } diff --git a/tests/libpakfire/archive.c b/tests/libpakfire/archive.c index 8f910f2bc..82074e601 100644 --- a/tests/libpakfire/archive.c +++ b/tests/libpakfire/archive.c @@ -36,11 +36,11 @@ static int test_open(const struct test* t) { // Open the archive PakfireArchive archive = pakfire_archive_open(t->pakfire, path); - assert_return(archive, EXIT_FAILURE); + ASSERT(archive); // Verify the archive pakfire_archive_verify_status_t verify = pakfire_archive_verify(archive); - assert_return(verify == PAKFIRE_ARCHIVE_VERIFY_OK, EXIT_FAILURE); + ASSERT(verify == PAKFIRE_ARCHIVE_VERIFY_OK); pakfire_archive_unref(archive); pakfire_free(path); @@ -56,11 +56,11 @@ static int test_extract(const struct test* t) { // Extract the archive payload int r = pakfire_archive_extract(archive, NULL, PAKFIRE_ARCHIVE_USE_PAYLOAD); - assert_return(r == 0, EXIT_FAILURE); + ASSERT(r == 0); // Check if test file from the archive exists - assert_return(pakfire_access(t->pakfire, pakfire_get_path(t->pakfire), - TEST_PKG1_FILE, F_OK) == 0, EXIT_FAILURE); + ASSERT(pakfire_access(t->pakfire, pakfire_get_path(t->pakfire), + TEST_PKG1_FILE, F_OK) == 0); pakfire_archive_unref(archive); @@ -74,10 +74,10 @@ static int test_import(const struct test* t) { pakfire_free(path); PakfireRepo repo = pakfire_repo_create(t->pakfire, "tmp"); - assert_return(repo, EXIT_FAILURE); + ASSERT(repo); PakfirePackage pkg = pakfire_repo_add_archive(repo, archive); - assert_return(pkg, EXIT_FAILURE); + ASSERT(pkg); pakfire_repo_unref(repo); pakfire_package_unref(pkg); diff --git a/tests/libpakfire/execute.c b/tests/libpakfire/execute.c index e6ae7dcb5..5b8358aa9 100644 --- a/tests/libpakfire/execute.c +++ b/tests/libpakfire/execute.c @@ -29,7 +29,7 @@ static int test_does_not_exist(const struct test* t) { const char* cmd = "/usr/bin/does-not-exist"; int r = pakfire_execute(t->pakfire, cmd, NULL, NULL, 0); - assert_return(r != 0, EXIT_FAILURE); + ASSERT(r != 0); return EXIT_SUCCESS; } diff --git a/tests/libpakfire/key.c b/tests/libpakfire/key.c index 2da8c7edd..2b615ecc8 100644 --- a/tests/libpakfire/key.c +++ b/tests/libpakfire/key.c @@ -40,7 +40,7 @@ static int test_init(const struct test* t) { keys = pakfire_key_list(t->pakfire); // Must be empty now - assert_return(keys == NULL, EXIT_FAILURE); + ASSERT(keys == NULL); return EXIT_SUCCESS; } @@ -58,16 +58,16 @@ static int test_import(const struct test* t) { PakfireKey* keys = pakfire_key_import(t->pakfire, TEST_KEY_DATA); // We should have a list with precisely one key object - assert_return(keys, EXIT_FAILURE); - assert_return(keys[0] != NULL, EXIT_FAILURE); - assert_return(keys[1] == NULL, EXIT_FAILURE); + ASSERT(keys); + ASSERT(keys[0] != NULL); + ASSERT(keys[1] == NULL); // Get the imported key key = *keys; // Check the fingerprint const char* fingerprint = pakfire_key_get_fingerprint(key); - assert_return(strcmp(fingerprint, TEST_KEY_FINGERPRINT) == 0, EXIT_FAILURE); + ASSERT(strcmp(fingerprint, TEST_KEY_FINGERPRINT) == 0); pakfire_key_unref(key); @@ -76,16 +76,16 @@ static int test_import(const struct test* t) { static int test_export(const struct test* t) { PakfireKey key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT); - assert_return(key, EXIT_FAILURE); + ASSERT(key); // Dump key description char* dump = pakfire_key_dump(key); - assert_return(dump, EXIT_FAILURE); + ASSERT(dump); LOG("%s\n", dump); pakfire_free(dump); char* data = pakfire_key_export(key, 0); - assert_return(data, EXIT_FAILURE); + ASSERT(data); LOG("Exported key:\n%s\n", data); pakfire_free(data); diff --git a/tests/libpakfire/main.c b/tests/libpakfire/main.c index 315a46578..001edf3ad 100644 --- a/tests/libpakfire/main.c +++ b/tests/libpakfire/main.c @@ -18,8 +18,6 @@ # # #############################################################################*/ -#include - #include #include "../testsuite.h" @@ -32,7 +30,7 @@ static int test_init(const struct test* t) { static int test_path(const struct test* t) { const char* path = pakfire_get_path(t->pakfire); - assert_return(strcmp(path, TEST_ROOTFS) == 0, EXIT_FAILURE); + ASSERT_STRING_EQUALS(path, TEST_ROOTFS); return EXIT_SUCCESS; } diff --git a/tests/libpakfire/makefile.c b/tests/libpakfire/makefile.c index 5dadedef9..02205b0c0 100644 --- a/tests/libpakfire/makefile.c +++ b/tests/libpakfire/makefile.c @@ -30,16 +30,16 @@ static int test_parse(const struct test* t) { // Open file FILE* f = fopen(path, "r"); - assert_return(f, EXIT_FAILURE); + ASSERT(f); PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL); int r = pakfire_parser_read(parser, f); - assert_return(r == 0, EXIT_FAILURE); + ASSERT(r == 0); // Try to retrieve some value char* value = pakfire_parser_get(parser, "sources"); - assert_return(value, EXIT_FAILURE); + ASSERT(value); printf("VALUE: sources = %s\n", value); pakfire_free(value); diff --git a/tests/libpakfire/parser.c b/tests/libpakfire/parser.c index 32c22629d..cb23b2fed 100644 --- a/tests/libpakfire/parser.c +++ b/tests/libpakfire/parser.c @@ -33,62 +33,62 @@ static int test_parser(const struct test* t) { // Retrieve a value that does not exist value = pakfire_parser_get(parser, "null"); - assert_return(!value, EXIT_FAILURE); + ASSERT(!value); // Set a value int r = pakfire_parser_set(parser, "a", "a"); - assert_return(r == 0, EXIT_FAILURE); + ASSERT(r == 0); // Retrieve the value again value = pakfire_parser_get(parser, "a"); - assert_compare(value, "a", EXIT_FAILURE); + ASSERT_STRING_EQUALS(value, "a"); // Append something to the value r = pakfire_parser_append(parser, "a", "b"); - assert_return(r == 0, EXIT_FAILURE); + ASSERT(r == 0); // Retrieve the value again value = pakfire_parser_get(parser, "a"); - assert_compare(value, "a b", EXIT_FAILURE); + ASSERT_STRING_EQUALS(value, "a b"); // Make a child parser PakfireParser subparser = pakfire_parser_create_child(parser, "child"); - assert_return(subparser, EXIT_FAILURE); + ASSERT(subparser); // Try to get a again value = pakfire_parser_get(subparser, "a"); - assert_compare(value, "a b", EXIT_FAILURE); + ASSERT_STRING_EQUALS(value, "a b"); // Append something to the subparser r = pakfire_parser_append(subparser, "a", "c"); - assert_return(r == 0, EXIT_FAILURE); + ASSERT(r == 0); // The subparser should return "a b c" value = pakfire_parser_get(subparser, "a"); - assert_compare(value, "a b c", EXIT_FAILURE); + ASSERT_STRING_EQUALS(value, "a b c"); // The original parser should remain unchanged value = pakfire_parser_get(parser, "a"); - assert_compare(value, "a b", EXIT_FAILURE); + ASSERT_STRING_EQUALS(value, "a b"); // Set another value r = pakfire_parser_append(subparser, "b", "1"); - assert_return(r == 0, EXIT_FAILURE); + ASSERT(r == 0); // 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); + ASSERT_STRING_EQUALS(value, "a b c"); // Set a variable r = pakfire_parser_set(parser, "c", "%{b}"); - assert_return(r == 0, EXIT_FAILURE); + ASSERT(r == 0); // Get the value of c value = pakfire_parser_get(parser, "c"); - assert_compare(value, "1", EXIT_FAILURE); + ASSERT_STRING_EQUALS(value, "1"); // Dump the parser char* s = pakfire_parser_dump(parser); diff --git a/tests/libpakfire/util.c b/tests/libpakfire/util.c index 32b4fec1a..5021801a4 100644 --- a/tests/libpakfire/util.c +++ b/tests/libpakfire/util.c @@ -29,7 +29,7 @@ static int test_basename(const struct test* t) { const char* dir = "/a/b/c"; char* output = pakfire_basename(dir); - assert_compare(output, "c", EXIT_FAILURE); + ASSERT_STRING_EQUALS(output, "c"); pakfire_free(output); return EXIT_SUCCESS; @@ -39,7 +39,7 @@ static int test_dirname(const struct test* t) { const char* dir = "/a/b/c"; char* output = pakfire_dirname(dir); - assert_compare(output, "/a/b", EXIT_FAILURE); + ASSERT_STRING_EQUALS(output, "/a/b"); pakfire_free(output); return EXIT_SUCCESS; @@ -49,10 +49,10 @@ static int test_string_startswith(const struct test* t) { int r; r = pakfire_string_startswith("ABC", "A"); - assert_return(r, EXIT_FAILURE); + ASSERT(r); r = pakfire_string_startswith("ABC", "B"); - assert_return(!r, EXIT_FAILURE); + ASSERT(!r); return EXIT_SUCCESS; } diff --git a/tests/testsuite.c b/tests/testsuite.c index 72d2e36ab..e21d76251 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -31,7 +31,7 @@ static int test_run(struct test* t) { LOG("running %s\n", t->name); t->pakfire = pakfire_create(TEST_ROOTFS, NULL); - assert_return(t->pakfire, EXIT_FAILURE); + ASSERT(t->pakfire); // Log to stderr pakfire_log_set_function(t->pakfire, pakfire_log_stderr); diff --git a/tests/testsuite.h b/tests/testsuite.h index 33d4c9a20..2723314ba 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -54,21 +54,21 @@ int testsuite_run(); #define testsuite_add_test(func) __testsuite_add_test(#func, func) -#define assert_return(expr, r) \ +#define ASSERT(expr) \ do { \ if ((!(expr))) { \ LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \ __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return r; \ + return EXIT_FAILURE; \ } \ } while (0) -#define assert_compare(string, value, r) \ +#define ASSERT_STRING_EQUALS(string, value) \ do { \ if (strcmp(string, value) != 0) { \ LOG_ERROR("Failed assertion: " #string " != " #value " %s:%d %s\n", \ __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return r; \ + return EXIT_FAILURE; \ } \ } while (0)