]> pd.if.org Git - ctap/commitdiff
Major rewrite to eliminate duplicate code.
authorNathan Wagner <nw@hydaspes.if.org>
Wed, 28 Mar 2012 16:56:00 +0000 (09:56 -0700)
committerNathan Wagner <nw@hydaspes.if.org>
Wed, 28 Mar 2012 16:56:00 +0000 (09:56 -0700)
Makefile
ctap.c
ctap.h
main.c

index 6da29d49dba6a1f35061e74e38b6e71769569f7d..46d37b071c4dbb7515f8441357ee090bd961bfdb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,3 +6,6 @@ main:   $(OBJS)
 
 clean:
        rm -rf *.o main
+
+test:  main
+       @prove --exec '' ./main
diff --git a/ctap.c b/ctap.c
index 6238747fd33eeb28acd2822e1e031f2cf4b0331b..9bdf9f395b541d275a2ae93453389ac972e73d93 100644 (file)
--- a/ctap.c
+++ b/ctap.c
 /* global variable testnum? */
 static int test = 0; /* the test number */
 static int planned = 0;
+static int intodo = 0;
+
+void begin_todo(void) {
+       intodo = 1;
+}
+
+void end_todo(void) {
+       intodo = 0;
+}
 
 void plan(int tests) {
        test = 0;
@@ -32,7 +41,7 @@ void skip_all(const char *why, ...) {
        printf("1..0");
        if (why) {
                va_list args;
-               printf(" # skip ");
+               printf(" # SKIP ");
                va_start(args, why);
                vfprintf(stdout, why, args);
                va_end(args);
@@ -40,79 +49,80 @@ void skip_all(const char *why, ...) {
        printf("\n");
 }
 
-void okv(int pass, const char *fmt, va_list args) {
+static void vfmtline(int pass, const char *directive, const char *fmt, va_list args) {
        printf("%sok %d", pass ? "" : "not ", ++test);
+       if (fmt && !directive) {
+               printf(" -");
+       }
+       if (directive) {
+               printf(" # %s", directive);
+       }
        if (fmt) {
-               printf(" ");
+               printf(" ");
                vfprintf(stdout, fmt, args);
        }
        printf("\n");
 }
 
+#if 0
+static void fmtline(int pass, const char *info, const char *fmt, ...) {
+       va_list args;
+
+       va_start(args,fmt);
+       vfmtline(pass, info, fmt, args);
+       va_end(args);
+}
+#endif
+
+void okv(int pass, const char *fmt, va_list args) {
+       vfmtline(pass, intodo ? "TODO" : 0, fmt, args);
+}
+
 void ok(int pass, char *fmt, ...) {
        va_list args;
 
-       printf("%sok %d", pass ? "" : "not ", ++test);
-       if (fmt) {
-               printf(" # ");
-               va_start(args, fmt);
-               vfprintf(stdout, fmt, args);
-               va_end(args);
-       }
-       printf("\n");
+       va_start(args, fmt);
+       okv(pass, fmt, args);
+       va_end(args);
 }
 
 void ok_block(unsigned long count, int pass, const char *fmt, ...) {
+       va_list args;
+       va_list copy;
+
        if (count == 0) {
                return;
        }
-       if (fmt) {
-               va_list args;
-               va_list copy;
-               va_start(args, fmt);
-               while (count--) {
-                       va_copy(copy, args);
-                       okv(pass, fmt, copy);
-                       va_end(copy);
-               }
-               va_end(args);
-       } else {
-               while (count--) {
-                       ok(pass, NULL);
-               }
+
+       va_start(args, fmt);
+       while (count--) {
+               va_copy(copy, args);
+               okv(pass, fmt, copy);
+               va_end(copy);
        }
+       va_end(args);
 }
 
 void skip(const char *why, ...) {
        va_list args;
 
-       printf("ok %d # skip", ++test);
-       if (why) {
-               printf(" ");
-               va_start(args, why);
-               vfprintf(stdout, why, args);
-               va_end(args);
-       }
-       printf("\n");
+       va_start(args, why);
+       vfmtline(1, "SKIP", why, args);
+       va_end(args);
 }
 
 void skip_block(unsigned long count, const char *why, ...) {
-       if (why) {
-               va_list args;
-               va_list copy;
-               va_start(args, why);
-               while (count--) {
-                       va_copy(copy, args);
-                       printf("ok %d # skip", ++test);
-                       printf(" ");
-                       vfprintf(stdout, why, copy);
-                       va_end(copy);
-                       printf("\n");
-               }
-               va_end(args);
-       } else {
-               printf("ok %d # skip\n", ++test);
+       va_list args;
+       va_list copy;
+       va_start(args, why);
+
+       while (count--) {
+               va_copy(copy, args);
+               vfmtline(1, "SKIP", why, args);
+               va_end(copy);
        }
+
+       va_end(args);
 }
 
 void bail(const char *fmt, ...) {
diff --git a/ctap.h b/ctap.h
index 43d8087e792720c8bba2df86958132709fd8d06e..c641d2f6f7e3e164ecbe506a53df51193b97770e 100644 (file)
--- a/ctap.h
+++ b/ctap.h
@@ -3,6 +3,8 @@
 
 #include <stdarg.h>
 
+void begin_todo(void);
+void end_todo(void);
 void plan(int tests);
 void plan_lazy(void);
 void skip_all(const char *why, ...);
diff --git a/main.c b/main.c
index da49aee908933e28ab30979af161987b43bfe899..55fbecd465948abc5b2397d1dd05d838a7f1c10b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -3,17 +3,24 @@
 int main(void) {
        /* plan(4); */
        plan_lazy();
+
        ok(1, "ok pass");
+
+       begin_todo();
        ok(0, "ok fail");
+       is_int(1,2,"is_int fail");
+       is_double(1.0,1.2,0.0,"is_double perfect fail");
+       is_string("foo", "bar", "is_string fail");
+       is_double(1.0,2.0,0.5,"is_double epsilon fail");
+       end_todo();
+
        skip("skip one");
        skip_block(2, "skip 2 block");
+
        is_int(1,1,"is_int pass");
-       is_int(1,2,"is_int fail");
        is_double(1.0,1.0,0.0,"is_double perfect pass");
-       is_double(1.0,1.2,0.0,"is_double perfect fail");
        is_double(1.0,1.1,0.5,"is_double epsilon pass");
-       is_double(1.0,2.0,0.5,"is_double epsilon fail");
        is_string("foo", "foo", "is_string pass");
-       is_string("foo", "bar", "is_string fail");
+
        return 0;
 }