test: refactor litest_assert_event_type logic
authorJosé Expósito <jose.exposito89@gmail.com>
Sat, 24 Jul 2021 11:54:24 +0000 (13:54 +0200)
committerPeter Hutterer <peter.hutterer@who-t.net>
Tue, 31 Aug 2021 04:42:15 +0000 (14:42 +1000)
Extract the logic in litest_assert_event_type to a generic function,
litest_assert_event_type_is_one_of, that takes a variable number of
expected event types.

Refactor, no functional changes.

Signed-off-by: José Expósito <jose.exposito89@gmail.com>
test/litest.c

index 54278bfa849dfc11d4df18ea2f4bc0bad006c963..b000fe8b1782181009c595dbdb7dae9e1f9c43e0 100644 (file)
@@ -3268,26 +3268,60 @@ litest_print_event(struct libinput_event *event)
        fprintf(stderr, "\n");
 }
 
-void
-litest_assert_event_type(struct libinput_event *event,
-                        enum libinput_event_type want)
+#define litest_assert_event_type_is_one_of(...) \
+    _litest_assert_event_type_is_one_of(__VA_ARGS__, -1)
+
+static void
+_litest_assert_event_type_is_one_of(struct libinput_event *event, ...)
 {
-       if (libinput_event_get_type(event) == want)
+       va_list args;
+       enum libinput_event_type expected_type;
+       enum libinput_event_type actual_type = libinput_event_get_type(event);
+       bool match = false;
+
+       va_start(args, event);
+       expected_type = va_arg(args, int);
+       while ((int)expected_type != -1 && !match) {
+               match = (actual_type == expected_type);
+               expected_type = va_arg(args, int);
+       }
+       va_end(args);
+
+       if (match)
                return;
 
        fprintf(stderr,
-               "FAILED EVENT TYPE: %s: have %s (%d) but want %s (%d)\n",
+               "FAILED EVENT TYPE: %s: have %s (%d) but want ",
                libinput_device_get_name(libinput_event_get_device(event)),
                litest_event_get_type_str(event),
-               libinput_event_get_type(event),
-               litest_event_type_str(want),
-               want);
-       fprintf(stderr, "Wrong event is: ");
+               libinput_event_get_type(event));
+
+       va_start(args, event);
+       expected_type = va_arg(args, int);
+       while ((int)expected_type != -1) {
+               fprintf(stderr,
+                       "%s (%d)",
+                       litest_event_type_str(expected_type),
+                       expected_type);
+               expected_type = va_arg(args, int);
+
+               if ((int)expected_type != -1)
+                       fprintf(stderr, " || ");
+       }
+
+       fprintf(stderr, "\nWrong event is: ");
        litest_print_event(event);
        litest_backtrace();
        abort();
 }
 
+void
+litest_assert_event_type(struct libinput_event *event,
+                        enum libinput_event_type want)
+{
+       litest_assert_event_type_is_one_of(event, want);
+}
+
 void
 litest_assert_empty_queue(struct libinput *li)
 {