tests: make all tests use the new conf-parsers
authorDavid Herrmann <dh.herrmann@googlemail.com>
Sat, 11 Aug 2012 20:43:13 +0000 (22:43 +0200)
committerDavid Herrmann <dh.herrmann@googlemail.com>
Sat, 11 Aug 2012 20:43:13 +0000 (22:43 +0200)
This allows the tests to have their own command-line parsers. Finally!

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
tests/test_include.h
tests/test_input.c
tests/test_output.c
tests/test_vt.c

index fbab9ea..d4e924f 100644 (file)
 #include "eloop.h"
 #include "log.h"
 
+#define TEST_HELP \
+       "\t-h, --help                  [off]   Print this help and exit\n" \
+       "\t-v, --verbose               [off]   Print verbose messages\n" \
+       "\t    --debug                 [off]   Enable debug mode\n" \
+       "\t    --silent                [off]   Suppress notices and warnings\n"
+
+struct {
+       bool help;
+       bool exit;
+       bool verbose;
+       bool debug;
+       bool silent;
+} test_conf;
+
+static int aftercheck_debug(struct conf_option *opt, int argc, char **argv,
+                           int idx)
+{
+       /* --debug implies --verbose */
+       if (test_conf.debug)
+               test_conf.verbose = 1;
+
+       return 0;
+}
+
+static int aftercheck_help(struct conf_option *opt, int argc, char **argv,
+                          int idx)
+{
+       /* exit after printing --help information */
+       if (test_conf.help) {
+               print_help();
+               test_conf.exit = true;
+       }
+
+       return 0;
+}
+
+#define TEST_OPTIONS \
+       CONF_OPTION_BOOL('h', "help", aftercheck_help, &test_conf.help, false), \
+       CONF_OPTION_BOOL('v', "verbose", NULL, &test_conf.verbose, false), \
+       CONF_OPTION_BOOL(0, "debug", aftercheck_debug, &test_conf.debug, false), \
+       CONF_OPTION_BOOL(0, "silent", NULL, &test_conf.silent, false)
+
 static void sig_generic(struct ev_eloop *p, struct signalfd_siginfo *info,
                        void *data)
 {
@@ -42,46 +84,52 @@ static void sig_generic(struct ev_eloop *p, struct signalfd_siginfo *info,
        log_info("terminating due to caught signal %d", info->ssi_signo);
 }
 
-static int test_prepare(int argc, char **argv, struct ev_eloop **out)
+static int test_prepare(struct conf_option *opts, size_t len,
+                       int argc, char **argv, struct ev_eloop **out)
 {
        int ret;
        struct ev_eloop *eloop;
 
-       ret = conf_parse_argv(argc, argv);
+       ret = conf_parse_argv(opts, len, argc, argv);
        if (ret)
-               return -EINVAL;
+               goto err_out;
 
-       if (conf_global.exit)
-               return -1;
+       if (test_conf.exit) {
+               ret = -ECANCELED;
+               goto err_out;
+       }
 
-       if (!conf_global.debug && !conf_global.verbose && conf_global.silent)
+       if (!test_conf.debug && !test_conf.verbose && test_conf.silent)
                log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
        else
-               log_set_config(&LOG_CONFIG_INFO(conf_global.debug,
-                                               conf_global.verbose));
+               log_set_config(&LOG_CONFIG_INFO(test_conf.debug,
+                                               test_conf.verbose));
 
        log_print_init(argv[0]);
 
        ret = ev_eloop_new(&eloop, log_llog);
        if (ret)
-               return ret;
+               goto err_out;
 
        ret = ev_eloop_register_signal_cb(eloop, SIGTERM, sig_generic, eloop);
-       if (ret) {
-               ev_eloop_unref(eloop);
-               return ret;
-       }
+       if (ret)
+               goto err_unref;
 
        ret = ev_eloop_register_signal_cb(eloop, SIGINT, sig_generic, eloop);
        if (ret) {
                ev_eloop_unregister_signal_cb(eloop, SIGTERM,
                                                sig_generic, eloop);
-               ev_eloop_unref(eloop);
-               return ret;
+               goto err_unref;
        }
 
        *out = eloop;
        return 0;
+
+err_unref:
+       ev_eloop_unref(eloop);
+err_out:
+       conf_free(opts, len);
+       return ret;
 }
 
 static void test_fail(int ret)
@@ -90,9 +138,11 @@ static void test_fail(int ret)
                log_err("init failed, errno %d: %s", ret, strerror(-ret));
 }
 
-static void test_exit(struct ev_eloop *eloop)
+static void test_exit(struct conf_option *opts, size_t len,
+                     struct ev_eloop *eloop)
 {
        ev_eloop_unregister_signal_cb(eloop, SIGINT, sig_generic, eloop);
        ev_eloop_unregister_signal_cb(eloop, SIGTERM, sig_generic, eloop);
        ev_eloop_unref(eloop);
+       conf_free(opts, len);
 }
index 1db58a5..ef71cb7 100644 (file)
@@ -24,6 +24,8 @@
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+static void print_help();
+
 #include <errno.h>
 #include <linux/input.h>
 #include <locale.h>
 static struct ev_eloop *eloop;
 static struct uterm_input *input;
 
+struct {
+       char *xkb_layout;
+       char *xkb_variant;
+       char *xkb_options;
+} input_conf;
+
 /* Pressing Ctrl-\ should toggle the capturing. */
 static void sig_quit(struct ev_eloop *p,
                        struct signalfd_siginfo *info,
@@ -109,7 +117,10 @@ static void monitor_event(struct uterm_monitor *mon,
                if (strcmp(ev->seat_name, "seat0"))
                        return;
 
-               ret = uterm_input_new(&input, eloop);
+               ret = uterm_input_new(&input, eloop,
+                                     input_conf.xkb_layout,
+                                     input_conf.xkb_variant,
+                                     input_conf.xkb_options);
                if (ret)
                        return;
                ret = uterm_input_register_cb(input, input_arrived, NULL);
@@ -128,12 +139,60 @@ static void monitor_event(struct uterm_monitor *mon,
        }
 }
 
+static void print_help()
+{
+       /*
+        * Usage/Help information
+        * This should be scaled to a maximum of 80 characters per line:
+        *
+        * 80 char line:
+        *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        * 80 char line starting with tab:
+        *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        */
+       fprintf(stderr,
+               "Usage:\n"
+               "\t%1$s [options]\n"
+               "\t%1$s -h [options]\n"
+               "\n"
+               "You can prefix boolean options with \"no-\" to negate it. If an argument is\n"
+               "given multiple times, only the last argument matters if not otherwise stated.\n"
+               "\n"
+               "General Options:\n"
+               TEST_HELP
+               "\n"
+               "Input Device Options:\n"
+               "\t    --xkb-layout <layout>   [us]    Set XkbLayout for input devices\n"
+               "\t    --xkb-variant <variant> [-]     Set XkbVariant for input devices\n"
+               "\t    --xkb-options <options> [-]     Set XkbOptions for input devices\n",
+               "test_input");
+       /*
+        * 80 char line:
+        *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        * 80 char line starting with tab:
+        *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        */
+}
+
+struct conf_option options[] = {
+       TEST_OPTIONS,
+       CONF_OPTION_STRING(0, "xkb-layout", NULL, &input_conf.xkb_layout, "us"),
+       CONF_OPTION_STRING(0, "xkb-variant", NULL, &input_conf.xkb_variant, ""),
+       CONF_OPTION_STRING(0, "xkb-options", NULL, &input_conf.xkb_options, ""),
+};
+
 int main(int argc, char **argv)
 {
        int ret;
        struct uterm_monitor *mon;
+       size_t onum;
 
-       ret = test_prepare(argc, argv, &eloop);
+       onum = sizeof(options) / sizeof(*options);
+       ret = test_prepare(options, onum, argc, argv, &eloop);
        if (ret)
                goto err_fail;
 
@@ -160,8 +219,9 @@ int main(int argc, char **argv)
 err_mon:
        uterm_monitor_unref(mon);
 err_exit:
-       test_exit(eloop);
+       test_exit(options, onum, eloop);
 err_fail:
-       test_fail(ret);
+       if (ret != -ECANCELED)
+               test_fail(ret);
        return abs(ret);
 }
index 025a3a3..8f8e0b7 100644 (file)
@@ -37,6 +37,8 @@
  * $ ./test_output something
  */
 
+static void print_help();
+
 #include <errno.h>
 #include <inttypes.h>
 #include <stdio.h>
 /* eloop object */
 static struct ev_eloop *eloop;
 
+struct {
+       bool fbdev;
+       bool test;
+} output_conf;
+
 static int blit_outputs(struct uterm_video *video)
 {
        struct uterm_display *iter;
@@ -143,18 +150,64 @@ static int list_outputs(struct uterm_video *video)
        return 0;
 }
 
+static void print_help()
+{
+       /*
+        * Usage/Help information
+        * This should be scaled to a maximum of 80 characters per line:
+        *
+        * 80 char line:
+        *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        * 80 char line starting with tab:
+        *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        */
+       fprintf(stderr,
+               "Usage:\n"
+               "\t%1$s [options]\n"
+               "\t%1$s -h [options]\n"
+               "\n"
+               "You can prefix boolean options with \"no-\" to negate it. If an argument is\n"
+               "given multiple times, only the last argument matters if not otherwise stated.\n"
+               "\n"
+               "General Options:\n"
+               TEST_HELP
+               "\n"
+               "Video Options:\n"
+               "\t    --fbdev                 [off]   Use fbdev instead of DRM\n"
+               "\t    --test                  [off]   Try displaying content instead of listing devices\n",
+               "test_input");
+       /*
+        * 80 char line:
+        *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        * 80 char line starting with tab:
+        *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        */
+}
+
+struct conf_option options[] = {
+       TEST_OPTIONS,
+       CONF_OPTION_BOOL(0, "fbdev", NULL, &output_conf.fbdev, false),
+       CONF_OPTION_BOOL(0, "test", NULL, &output_conf.test, false),
+};
+
 int main(int argc, char **argv)
 {
        struct uterm_video *video;
        int ret;
        unsigned int mode;
        const char *node;
+       size_t onum;
 
-       ret = test_prepare(argc, argv, &eloop);
+       onum = sizeof(options) / sizeof(*options);
+       ret = test_prepare(options, onum, argc, argv, &eloop);
        if (ret)
                goto err_fail;
 
-       if (conf_global.use_fbdev) {
+       if (output_conf.fbdev) {
                mode = UTERM_VIDEO_FBDEV;
                node = "/dev/fb0";
        } else {
@@ -182,7 +235,7 @@ int main(int argc, char **argv)
        if (ret < 0)
                goto err_unref;
 
-       if (argc < 2) {
+       if (!output_conf.test) {
                ret = list_outputs(video);
                if (ret) {
                        log_err("Cannot list outputs: %d", ret);
@@ -199,8 +252,9 @@ int main(int argc, char **argv)
 err_unref:
        uterm_video_unref(video);
 err_exit:
-       test_exit(eloop);
+       test_exit(options, onum, eloop);
 err_fail:
-       test_fail(ret);
+       if (ret != -ECANCELED)
+               test_fail(ret);
        return abs(ret);
 }
index 2b347fa..e835904 100644 (file)
  * ctrl+alt+FX (or some equivalent) to switch back to X/VT.
  */
 
+static void print_help();
+
 #include <errno.h>
 #include <signal.h>
 #include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-
 #include "eloop.h"
 #include "log.h"
 #include "uterm.h"
 #include "test_include.h"
 
+static void print_help()
+{
+       /*
+        * Usage/Help information
+        * This should be scaled to a maximum of 80 characters per line:
+        *
+        * 80 char line:
+        *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        * 80 char line starting with tab:
+        *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        */
+       fprintf(stderr,
+               "Usage:\n"
+               "\t%1$s [options]\n"
+               "\t%1$s -h [options]\n"
+               "\n"
+               "You can prefix boolean options with \"no-\" to negate it. If an argument is\n"
+               "given multiple times, only the last argument matters if not otherwise stated.\n"
+               "\n"
+               "General Options:\n"
+               TEST_HELP,
+               "test_vt");
+       /*
+        * 80 char line:
+        *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        * 80 char line starting with tab:
+        *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
+        *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
+        */
+}
+
+struct conf_option options[] = {
+       TEST_OPTIONS
+};
+
 int main(int argc, char **argv)
 {
        int ret;
        struct ev_eloop *eloop;
        struct uterm_vt_master *vtm;
        struct uterm_vt *vt;
+       size_t onum;
 
-       ret = test_prepare(argc, argv, &eloop);
+       onum = sizeof(options) / sizeof(*options);
+       ret = test_prepare(options, onum, argc, argv, &eloop);
        if (ret)
                goto err_fail;
 
@@ -81,8 +123,9 @@ int main(int argc, char **argv)
 err_vtm:
        uterm_vt_master_unref(vtm);
 err_exit:
-       test_exit(eloop);
+       test_exit(options, onum, eloop);
 err_fail:
-       test_fail(ret);
+       if (ret != -ECANCELED)
+               test_fail(ret);
        return abs(ret);
 }