]> xenbits.xensource.com Git - libvirt.git/commitdiff
test: utils: Add helpers for automatic numbering of test cases
authorPeter Krempa <pkrempa@redhat.com>
Thu, 15 Jan 2015 10:44:58 +0000 (11:44 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Sat, 31 Jan 2015 07:53:21 +0000 (08:53 +0100)
Adding or reordering test cases is usually a pain due to static test
case names that are then passed to virtTestRun(). To ease the numbering
of test cases, this patch adds two simple helpers that generate the test
names according to the order they are run. The test name can be
configured via the reset function.

This will allow us to freely add test cases in middle of test groups
without the need to re-number the rest of test cases.

tests/testutils.c
tests/testutils.h

index 9a79f98018c15c58fe8b02d58fa032dae13090ae..f2db4a052347d8b84b68bf5e22ba3125eeb7484f 100644 (file)
@@ -986,3 +986,55 @@ virDomainXMLOptionPtr virTestGenericDomainXMLConfInit(void)
                                  &virTestGenericPrivateDataCallbacks,
                                  NULL);
 }
+
+
+static int virtTestCounter;
+static char virtTestCounterStr[128];
+static char *virtTestCounterPrefixEndOffset;
+
+
+/**
+ * virtTestCounterReset:
+ * @prefix: name of the test group
+ *
+ * Resets the counter and sets up the test group name to use with
+ * virtTestCounterNext(). This function is not thread safe.
+ *
+ * Note: The buffer for the assembled message is 128 bytes long. Longer test
+ * case names (including the number index) will be silently truncated.
+ */
+void
+virtTestCounterReset(const char *prefix)
+{
+    virtTestCounter = 0;
+
+    ignore_value(virStrcpyStatic(virtTestCounterStr, prefix));
+    virtTestCounterPrefixEndOffset = strchrnul(virtTestCounterStr, '\0');
+}
+
+
+/**
+ * virtTestCounterNext:
+ *
+ * This function is designed to ease test creation and reordering by adding
+ * a way to do automagic test case numbering.
+ *
+ * Returns string consisting of test name prefix configured via
+ * virtTestCounterReset() and a number that increments in every call of this
+ * function. This function is not thread safe.
+ *
+ * Note: The buffer for the assembled message is 128 bytes long. Longer test
+ * case names (including the number index) will be silently truncated.
+ */
+const char
+*virtTestCounterNext(void)
+{
+    size_t len = ARRAY_CARDINALITY(virtTestCounterStr);
+
+    /* calculate length of the rest of the string */
+    len -= (virtTestCounterPrefixEndOffset - virtTestCounterStr);
+
+    snprintf(virtTestCounterPrefixEndOffset, len, "%d", ++virtTestCounter);
+
+    return virtTestCounterStr;
+}
index d78818d34e66e13ff8e42c365981e5de7a90c820..155b30f259ecdf03cd37f00cd80a1d74d2f74253 100644 (file)
@@ -82,6 +82,9 @@ char *virtTestLogContentAndReset(void);
 
 void virtTestQuiesceLibvirtErrors(bool always);
 
+void virtTestCounterReset(const char *prefix);
+const char *virtTestCounterNext(void);
+
 int virtTestMain(int argc,
                  char **argv,
                  int (*func)(void));