tests: add get_test_fixture_index()
authorPekka Paalanen <pekka.paalanen@collabora.com>
Wed, 22 Jan 2020 11:37:20 +0000 (13:37 +0200)
committerPekka Paalanen <pekka.paalanen@collabora.com>
Thu, 27 Feb 2020 14:08:42 +0000 (16:08 +0200)
A future test wants to access the fixture data array for the currently running
fixture index to log the test description. This patch provides access to the
array index.

Rather than adding more gloabl variables, I changed the type of the existing
one which feels slightly cleaner.

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
tests/weston-test-runner.c
tests/weston-test-runner.h

index 8f945f94dc91309c384bc4bc52560d1791b4419c..c10f286f4fd125a605df61b4210f69464d5af003 100644 (file)
 
 extern const struct weston_test_entry __start_test_section, __stop_test_section;
 
-static const char *test_name_;
+struct weston_test_run_info {
+       char name[512];
+       int fixture_nr;
+};
+
+static const struct weston_test_run_info *test_run_info_;
 
 /** Get the test name string with counter
  *
@@ -62,7 +67,23 @@ static const char *test_name_;
 const char *
 get_test_name(void)
 {
-       return test_name_;
+       return test_run_info_->name;
+}
+
+/** Get the current fixture index
+ *
+ * Returns the current fixture index which can be used directly as an index
+ * into the array passed as an argument to DECLARE_FIXTURE_SETUP_WITH_ARG().
+ *
+ * This is only usable from code paths inside TEST(), TEST_P(), PLUGIN_TEST()
+ * etc. defined functions.
+ *
+ * \ingroup testharness
+ */
+int
+get_test_fixture_index(void)
+{
+       return test_run_info_->fixture_nr - 1;
 }
 
 /** Print into test log
@@ -100,18 +121,20 @@ static enum test_result_code
 run_test(int fixture_nr, const struct weston_test_entry *t, void *data,
         int iteration)
 {
-       char str[512];
+       struct weston_test_run_info info;
 
        if (data) {
-               snprintf(str, sizeof(str), "f%d-%s-e%d",
+               snprintf(info.name, sizeof(info.name), "f%d-%s-e%d",
                         fixture_nr, t->name, iteration);
        } else {
-               snprintf(str, sizeof(str), "f%d-%s", fixture_nr, t->name);
+               snprintf(info.name, sizeof(info.name), "f%d-%s",
+                        fixture_nr, t->name);
        }
+       info.fixture_nr = fixture_nr;
 
-       test_name_ = str;
+       test_run_info_ = &info;
        t->run(data);
-       test_name_ = NULL;
+       test_run_info_ = NULL;
 
        /*
         * XXX: We should return t->run(data); but that requires changing
index c47deba90223b0c69deb7075d9f3975781b01795..9e545cd92c028d880544a97d020f2d7947d7df8d 100644 (file)
@@ -143,6 +143,9 @@ testlog(const char *fmt, ...) WL_PRINTF(1, 2);
 const char *
 get_test_name(void);
 
+int
+get_test_fixture_index(void);
+
 /** Fixture setup array record
  *
  * Helper to store the attributes of the data array passed in to