From 521b3876e85e23256c3297805ec8946c02b14c16 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 26 Feb 2015 17:39:20 +0000 Subject: [PATCH 3/9] test-shell, test-printf: produce TAP output like the other installable tests --- test/internals/printf.c | 13 +++++++ test/shell-test.c | 100 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 88 insertions(+), 25 deletions(-) diff --git a/test/internals/printf.c b/test/internals/printf.c index db15151..4862297 100644 --- a/test/internals/printf.c +++ b/test/internals/printf.c @@ -68,14 +68,25 @@ main (int argc, { char buf[] = X_TIMES_1024 X_TIMES_1024 X_TIMES_1024 X_TIMES_1024; int i; + int test_num = 0; do_test (1, "%d", 0); + printf ("ok %d\n", ++test_num); + do_test (7, "%d", 1234567); + printf ("ok %d\n", ++test_num); + do_test (3, "%f", 3.5); + printf ("ok %d\n", ++test_num); do_test (0, "%s", ""); + printf ("ok %d\n", ++test_num); + do_test (1024, "%s", X_TIMES_1024); + printf ("ok %d\n", ++test_num); + do_test (1025, "%s", X_TIMES_1024 "Y"); + printf ("ok %d\n", ++test_num); for (i = 4096; i > 0; i--) { @@ -83,6 +94,8 @@ main (int argc, do_test (i, "%s", buf); do_test (i + 3, "%s:%d", buf, 42); } + printf ("ok %d\n", ++test_num); + printf ("1..%d\n", test_num); return 0; } diff --git a/test/shell-test.c b/test/shell-test.c index d1dc5b5..d7e6f8a 100644 --- a/test/shell-test.c +++ b/test/shell-test.c @@ -9,8 +9,13 @@ #include #include +static int test_num = 0; +static int num_failed = 0; + static dbus_bool_t -test_command_line (const char *arg1, ...) +test_command_line_internal (dbus_bool_t should_work, + const char *arg1, + va_list var_args) { int i, original_argc, shell_argc; char **shell_argv; @@ -18,10 +23,8 @@ test_command_line (const char *arg1, ...) char *command_line, *tmp; DBusString str; DBusList *list = NULL, *node; - va_list var_args; DBusError error; - va_start (var_args, arg1); _dbus_list_append (&list, (char *)arg1); do { @@ -30,7 +33,6 @@ test_command_line (const char *arg1, ...) break; _dbus_list_append (&list, tmp); } while (tmp); - va_end (var_args); original_argc = _dbus_list_get_length (&list); original_argv = dbus_new (char *, original_argc); @@ -46,23 +48,25 @@ test_command_line (const char *arg1, ...) _dbus_list_clear (&list); command_line = _dbus_string_get_data (&str); - printf ("\n\nTesting command line '%s'\n", command_line); + printf ("# Testing command line '%s'\n", command_line); dbus_error_init (&error); if (!_dbus_shell_parse_argv (command_line, &shell_argc, &shell_argv, &error)) { - fprintf (stderr, "Error parsing command line: %s\n", error.message ? error.message : ""); - return FALSE; + printf ("# Error%s parsing command line: %s\n", + should_work ? "" : " (as expected)", + error.message ? error.message : ""); + return !should_work; } else { if (shell_argc != original_argc) { - printf ("Number of arguments returned (%d) don't match original (%d)\n", + printf ("# Number of arguments returned (%d) don't match original (%d)\n", shell_argc, original_argc); return FALSE; } - printf ("Number of arguments: %d\n", shell_argc); + printf ("# Number of arguments: %d\n", shell_argc); for (i = 0; i < shell_argc; i++) { char *unquoted; @@ -83,27 +87,73 @@ test_command_line (const char *arg1, ...) dbus_free_string_array (shell_argv); } - + + if (!should_work) + { + printf ("# Expected an error\n"); + return FALSE; + } + _dbus_string_free (&str); - + return TRUE; } +static void +test_command_line (const char *arg1, ...) +{ + va_list var_args; + + va_start (var_args, arg1); + + if (test_command_line_internal (TRUE, arg1, var_args)) + { + printf ("ok %d\n", ++test_num); + } + else + { + printf ("not ok %d\n", ++test_num); + num_failed++; + } + + va_end (var_args); +} + +static void +test_command_line_fails (const char *arg1, ...) +{ + va_list var_args; + + va_start (var_args, arg1); + + if (test_command_line_internal (FALSE, arg1, var_args)) + { + printf ("ok %d\n", ++test_num); + } + else + { + printf ("not ok %d\n", ++test_num); + num_failed++; + } + + va_end (var_args); +} + int main (int argc, char **argv) { - if (!test_command_line ("command", "-s", "--force-shutdown", "\"a string\"", "123", NULL) - || !test_command_line ("command", "-s", NULL) - || !test_command_line ("/opt/gnome/bin/service-start", NULL) - || !test_command_line ("grep", "-l", "-r", "-i", "'whatever'", "files*.c", NULL) - || !test_command_line ("/home/boston/johnp/devel-local/dbus/test/test-segfault", NULL) - || !test_command_line ("ls", "-l", "-a", "--colors", _dbus_get_tmpdir(), NULL) - || !test_command_line ("rsync-to-server", NULL) - || !test_command_line ("test-segfault", "--no-segfault", NULL) - || !test_command_line ("evolution", "mailto:pepe@cuco.com", NULL) - || !test_command_line ("run", "\"a \n multiline\"", NULL) - || test_command_line ("ls", "\"a wrong string'", NULL) /* invalid command line */ ) - return -1; - - return 0; + test_command_line ("command", "-s", "--force-shutdown", "\"a string\"", "123", NULL); + test_command_line ("command", "-s", NULL); + test_command_line ("/opt/gnome/bin/service-start", NULL); + test_command_line ("grep", "-l", "-r", "-i", "'whatever'", "files*.c", NULL); + test_command_line ("/home/boston/johnp/devel-local/dbus/test/test-segfault", NULL); + test_command_line ("ls", "-l", "-a", "--colors", _dbus_get_tmpdir(), NULL); + test_command_line ("rsync-to-server", NULL); + test_command_line ("test-segfault", "--no-segfault", NULL); + test_command_line ("evolution", "mailto:pepe@cuco.com", NULL); + test_command_line ("run", "\"a \n multiline\"", NULL); + test_command_line_fails ("ls", "\"a wrong string'", NULL); + + printf ("1..%d\n", test_num); + return (num_failed != 0); } -- 2.1.4