53 string StringPrintf(
const char* format, ...) {
59 vsnprintf(buf, N, format, ap);
67 "usage: ninja_tests [options]\n" 70 " --gtest_filter=POSTIVE_PATTERN[-NEGATIVE_PATTERN]\n" 71 " Run tests whose names match the positive but not the negative pattern.\n" 72 " '*' matches any substring. (gtest's ':', '?' are not implemented).\n");
75 bool PatternMatchesString(
const char* pattern,
const char* str) {
78 case '-':
return *str ==
'\0';
79 case '*':
return (*str !=
'\0' && PatternMatchesString(pattern, str + 1)) ||
80 PatternMatchesString(pattern + 1, str);
81 default:
return *pattern == *str &&
82 PatternMatchesString(pattern + 1, str + 1);
86 bool TestMatchesFilter(
const char* test,
const char* filter) {
88 const char*
const dash = strchr(filter,
'-');
89 const char* pos = dash == filter ?
"*" : filter;
90 const char* neg = dash ? dash + 1 :
"";
91 return PatternMatchesString(pos, test) && !PatternMatchesString(neg, test);
94 bool ReadFlags(
int* argc,
char*** argv,
const char** test_filter) {
95 enum { OPT_GTEST_FILTER = 1 };
96 const option kLongOptions[] = {
102 while ((opt =
getopt_long(*argc, *argv,
"h", kLongOptions, NULL)) != -1) {
104 case OPT_GTEST_FILTER:
105 if (strchr(
optarg,
'?') == NULL && strchr(
optarg,
':') == NULL) {
125 StringPrintf(
"*** Failure in %s:%d\n%s\n", file, line, error));
131 int main(
int argc,
char **argv) {
132 int tests_started = 0;
134 const char* test_filter =
"*";
135 if (!ReadFlags(&argc, &argv, &test_filter))
138 int nactivetests = 0;
139 for (
int i = 0; i <
ntests; i++)
140 if ((
tests[i].should_run = TestMatchesFilter(
tests[i].name, test_filter)))
144 for (
int i = 0; i <
ntests; i++) {
145 if (!
tests[i].should_run)
continue;
150 StringPrintf(
"[%d/%d] %s", tests_started, nactivetests,
tests[i].name),
161 return passed ? EXIT_SUCCESS : EXIT_FAILURE;
static RegisteredTest tests[10000]
void Print(std::string to_print, LineType type)
Overprints the current line.
void RegisterTest(testing::Test *(*factory)(), const char *name)
static LinePrinter printer
int main(int argc, char **argv)
int getopt_long(int argc, char **argv, const char *shortopts, const GETOPT_LONG_OPTION_T *longopts, int *longind)
bool Check(bool condition, const char *file, int line, const char *error)
Prints lines of text, possibly overprinting previously printed lines if the terminal supports it...
void PrintOnNewLine(const std::string &to_print)
Prints a string on a new line, not overprinting previous output.
testing::Test * g_current_test
#define required_argument
testing::Test *(* factory)()