The world's most popular open source database
00001 /* Copyright (C) 2006 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00016 00017 Library for providing TAP support for testing C and C++ was written 00018 by Mats Kindahl <mats@mysql.com>. 00019 */ 00020 00021 #include "tap.h" 00022 00023 #include <stdlib.h> 00024 #include <stdarg.h> 00025 #include <stdio.h> 00026 #include <string.h> 00027 00035 static TEST_DATA g_test = { 0, 0, 0, "" }; 00036 00042 #define tapout stdout 00043 00058 static void 00059 emit_tap(int pass, char const *fmt, va_list ap) 00060 { 00061 fprintf(tapout, "%sok %d%s", 00062 pass ? "" : "not ", 00063 ++g_test.last, 00064 (fmt && *fmt) ? " - " : ""); 00065 if (fmt && *fmt) 00066 vfprintf(tapout, fmt, ap); 00067 } 00068 00069 00083 static void 00084 emit_dir(const char *dir, const char *exp) 00085 { 00086 fprintf(tapout, " # %s %s", dir, exp); 00087 } 00088 00089 00093 static void 00094 emit_endl() 00095 { 00096 fprintf(tapout, "\n"); 00097 } 00098 00099 void 00100 diag(char const *fmt, ...) 00101 { 00102 va_list ap; 00103 va_start(ap, fmt); 00104 fprintf(tapout, "# "); 00105 vfprintf(tapout, fmt, ap); 00106 fprintf(tapout, "\n"); 00107 va_end(ap); 00108 } 00109 00110 00111 void 00112 plan(int const count) 00113 { 00114 g_test.plan= count; 00115 switch (count) 00116 { 00117 case NO_PLAN: 00118 break; 00119 default: 00120 if (count > 0) 00121 fprintf(tapout, "1..%d\n", count); 00122 break; 00123 } 00124 } 00125 00126 00127 void 00128 skip_all(char const *reason, ...) 00129 { 00130 va_list ap; 00131 va_start(ap, reason); 00132 fprintf(tapout, "1..0 # skip "); 00133 vfprintf(tapout, reason, ap); 00134 va_end(ap); 00135 exit(0); 00136 } 00137 00138 void 00139 ok(int const pass, char const *fmt, ...) 00140 { 00141 va_list ap; 00142 va_start(ap, fmt); 00143 00144 if (!pass && *g_test.todo == '\0') 00145 ++g_test.failed; 00146 00147 emit_tap(pass, fmt, ap); 00148 va_end(ap); 00149 if (*g_test.todo != '\0') 00150 emit_dir("todo", g_test.todo); 00151 emit_endl(); 00152 } 00153 00154 00155 void 00156 skip(int how_many, char const *const fmt, ...) 00157 { 00158 char reason[80]; 00159 if (fmt && *fmt) 00160 { 00161 va_list ap; 00162 va_start(ap, fmt); 00163 vsnprintf(reason, sizeof(reason), fmt, ap); 00164 va_end(ap); 00165 } 00166 else 00167 reason[0] = '\0'; 00168 00169 while (how_many-- > 0) 00170 { 00171 va_list ap; 00172 emit_tap(1, NULL, ap); 00173 emit_dir("skip", reason); 00174 emit_endl(); 00175 } 00176 } 00177 00178 void 00179 todo_start(char const *message, ...) 00180 { 00181 va_list ap; 00182 va_start(ap, message); 00183 vsnprintf(g_test.todo, sizeof(g_test.todo), message, ap); 00184 va_end(ap); 00185 } 00186 00187 void 00188 todo_end() 00189 { 00190 *g_test.todo = '\0'; 00191 } 00192 00193 int exit_status() { 00194 /* 00195 If there were no plan, we write one last instead. 00196 */ 00197 if (g_test.plan == NO_PLAN) 00198 plan(g_test.last); 00199 00200 if (g_test.plan != g_test.last) 00201 { 00202 diag("%d tests planned but%s %d executed", 00203 g_test.plan, (g_test.plan > g_test.last ? " only" : ""), g_test.last); 00204 return EXIT_FAILURE; 00205 } 00206 00207 if (g_test.failed > 0) 00208 { 00209 diag("Failed %d tests!", g_test.failed); 00210 return EXIT_FAILURE; 00211 } 00212 00213 return EXIT_SUCCESS; 00214 } 00215
1.4.7

