]> granicus.if.org Git - p11-kit/commitdiff
common: Add assert_skip() and assert_todo()
authorDaiki Ueno <dueno@redhat.com>
Tue, 27 Mar 2018 13:19:07 +0000 (15:19 +0200)
committerDaiki Ueno <ueno@gnu.org>
Thu, 29 Mar 2018 09:25:11 +0000 (11:25 +0200)
common/test.c
common/test.h

index e9177015386d1043667f19c28ed12bd828e1ed5f..18cb50188ad4e00ca42b927f141e36aba018e2f2 100644 (file)
@@ -89,6 +89,34 @@ struct {
        jmp_buf jump;
 } gl = { NULL, NULL, 0, };
 
+static void
+print_diagnostics (const char *filename,
+                  int line,
+                  const char *function,
+                  char *output)
+{
+       const char *pos;
+       char *from;
+       char *next;
+
+       for (from = output; from != NULL; ) {
+               next = strchr (from, '\n');
+               if (next) {
+                       next[0] = '\0';
+                       next += 1;
+               }
+
+               printf ("# %s\n", from);
+               from = next;
+       }
+
+       pos = strrchr (filename, '/');
+       if (pos != NULL && pos[1] != '\0')
+               filename = pos + 1;
+
+       printf ("# in %s() at %s:%d\n", function, filename, line);
+}
+
 void
 p11_test_fail (const char *filename,
                int line,
@@ -96,10 +124,7 @@ p11_test_fail (const char *filename,
                const char *message,
                ...)
 {
-       const char *pos;
        char *output;
-       char *from;
-       char *next;
        va_list va;
 
        assert (gl.last != NULL);
@@ -113,23 +138,89 @@ p11_test_fail (const char *filename,
                assert (0 && "vasprintf() failed");
        va_end (va);
 
-       for (from = output; from != NULL; ) {
-               next = strchr (from, '\n');
-               if (next) {
-                       next[0] = '\0';
-                       next += 1;
-               }
+       print_diagnostics (filename, line, function, output);
+       free (output);
 
-               printf ("# %s\n", from);
-               from = next;
+       /* Let coverity know we're not supposed to return from here */
+#ifdef __COVERITY__
+       abort();
+#endif
+
+       longjmp (gl.jump, 1);
+}
+
+void
+p11_test_skip (const char *filename,
+               int line,
+               const char *function,
+               const char *message,
+               ...)
+{
+       char *output;
+       char *pos;
+       va_list va;
+
+       assert (gl.last != NULL);
+       assert (gl.last->type == TEST);
+       gl.last->x.test.failed = 1;
+
+       printf ("ok %d %s", gl.number, gl.last->x.test.name);
+
+       va_start (va, message);
+       if (vasprintf (&output, message, va) < 0)
+               assert (0 && "vasprintf() failed");
+       va_end (va);
+
+       pos = strchr (output, '\n');
+       if (pos) {
+               *pos = '\0';
+               pos++;
        }
+       printf (" # SKIP %s\n", output);
 
-       pos = strrchr (filename, '/');
-       if (pos != NULL && pos[1] != '\0')
-               filename = pos + 1;
+       if (pos)
+               print_diagnostics (filename, line, function, pos);
+       free (output);
 
-       printf ("# in %s() at %s:%d\n", function, filename, line);
+       /* Let coverity know we're not supposed to return from here */
+#ifdef __COVERITY__
+       abort();
+#endif
+
+       longjmp (gl.jump, 1);
+}
+
+void
+p11_test_todo (const char *filename,
+               int line,
+               const char *function,
+               const char *message,
+               ...)
+{
+       char *output;
+       char *pos;
+       va_list va;
+
+       assert (gl.last != NULL);
+       assert (gl.last->type == TEST);
+       gl.last->x.test.failed = 1;
+
+       printf ("not ok %d %s", gl.number, gl.last->x.test.name);
+
+       va_start (va, message);
+       if (vasprintf (&output, message, va) < 0)
+               assert (0 && "vasprintf() failed");
+       va_end (va);
+
+       pos = strchr (output, '\n');
+       if (pos) {
+               *pos = '\0';
+               pos++;
+       }
+       printf (" # TODO %s\n", output);
 
+       if (pos)
+               print_diagnostics (filename, line, function, pos);
        free (output);
 
        /* Let coverity know we're not supposed to return from here */
index e28bb55d2008153105a0436e58c4e72cea7d154f..1c952b0da481b19fe859d59686566d124cb9f7b6 100644 (file)
        do { const char *__s = (detail); \
                p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "%s%s%s", (msg), __s ? ": ": "", __s ? __s : ""); \
        } while (0)
+#define assert_skip(msg, detail) \
+       do { const char *__s = (detail); \
+               p11_test_skip (__FILE__, __LINE__, __FUNCTION__, "%s%s%s", (msg), __s ? ": ": "", __s ? __s : ""); \
+       } while (0)
+#define assert_todo(msg, detail) \
+       do { const char *__s = (detail); \
+               p11_test_todo (__FILE__, __LINE__, __FUNCTION__, "%s%s%s", (msg), __s ? ": ": "", __s ? __s : ""); \
+       } while (0)
 #define assert_not_reached(msg) \
        do { \
                p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "code should not be reached"); \
@@ -113,6 +121,18 @@ void        p11_test_fail           (const char *filename,
                                      const char *message,
                                      ...) GNUC_PRINTF(4, 5) CLANG_ANALYZER_NORETURN;
 
+void        p11_test_skip           (const char *filename,
+                                     int line,
+                                     const char *function,
+                                     const char *message,
+                                     ...) GNUC_PRINTF(4, 5) CLANG_ANALYZER_NORETURN;
+
+void        p11_test_todo           (const char *filename,
+                                     int line,
+                                     const char *function,
+                                     const char *message,
+                                     ...) GNUC_PRINTF(4, 5) CLANG_ANALYZER_NORETURN;
+
 void        p11_test                (void (* function) (void),
                                      const char *name,
                                      ...) GNUC_PRINTF(2, 3);