| 1 |
#define _CRT_SECURE_NO_WARNINGS // work around 'getenv' deprecation on WIN32 |
| 2 |
|
| 3 |
#include <cstdlib> |
| 4 |
#include <fstream> |
| 5 |
#include <iostream> |
| 6 |
#include <string> |
| 7 |
|
| 8 |
#if defined(_MSC_VER) && _MSC_VER < 1310 |
| 9 |
# define GETENV ::getenv |
| 10 |
#else |
| 11 |
# define GETENV std::getenv |
| 12 |
#endif |
| 13 |
|
| 14 |
#define ARRAY_SIZE(a) sizeof(a) / sizeof(*a) |
| 15 |
|
| 16 |
int main(int argc, char** argv) |
| 17 |
{ |
| 18 |
// Note: GoogleTest.cmake doesn't actually depend on Google Test as such; |
| 19 |
// it only requires that we produces output in the expected format when |
| 20 |
// invoked with --gtest_list_tests. Thus, we fake that here. This allows us |
| 21 |
// to test the module without actually needing Google Test. |
| 22 |
bool is_filtered = |
| 23 |
argc > 3 && std::string(argv[3]).find("--gtest_filter=") == 0; |
| 24 |
bool add_basic_tests = |
| 25 |
!is_filtered || (std::string(argv[3]).find("basic*") != std::string::npos); |
| 26 |
bool add_typed_tests = |
| 27 |
!is_filtered || (std::string(argv[3]).find("typed*") != std::string::npos); |
| 28 |
bool add_value_tests = |
| 29 |
!is_filtered || (std::string(argv[3]).find("value*") != std::string::npos); |
| 30 |
bool add_dynamic_tests = !is_filtered || |
| 31 |
(std::string(argv[3]).find("dynamic*") != std::string::npos); |
| 32 |
|
| 33 |
if (argc > 2 && std::string(argv[1]) == "--gtest_list_tests" && |
| 34 |
std::string(argv[2]).find("--gtest_output=json:") != std::string::npos) { |
| 35 |
std::ofstream ostrm; |
| 36 |
if (!GETENV("NO_GTEST_JSON_OUTPUT")) { |
| 37 |
std::string output_param(argv[2]); |
| 38 |
std::string::size_type split = output_param.find(":"); |
| 39 |
std::string filepath = output_param.substr(split + 1); |
| 40 |
// The full file path is passed |
| 41 |
ostrm.open(filepath.c_str(), std::ios::out | std::ios::binary); |
| 42 |
if (!ostrm) { |
| 43 |
std::cerr << "Failed to create file: " << filepath.c_str() |
| 44 |
<< std::endl; |
| 45 |
return 1; |
| 46 |
} |
| 47 |
} else { |
| 48 |
std::cout << "WARNING: An old-style warning." << std::endl; |
| 49 |
std::cout << "[ WARNING ] A new-style warning." << std::endl; |
| 50 |
std::cout << "[ ERROR ] A new-style error." << std::endl; |
| 51 |
} |
| 52 |
|
| 53 |
int tests = 0; |
| 54 |
ostrm << "{\n" |
| 55 |
" \"name\": \"AllTests\",\n" |
| 56 |
" \"testsuites\": ["; |
| 57 |
|
| 58 |
if (add_basic_tests) { |
| 59 |
char const* basic_suite_names[] = { "basic", "ns.basic" }; |
| 60 |
for (size_t i = 0; i < ARRAY_SIZE(basic_suite_names); i++) { |
| 61 |
std::cout << basic_suite_names[i] << '.' << std::endl; |
| 62 |
std::cout << " case_foo" << std::endl; |
| 63 |
std::cout << " case_bar" << std::endl; |
| 64 |
std::cout << " DISABLED_disabled_case" << std::endl; |
| 65 |
std::cout << " DISABLEDnot_really_case" << std::endl; |
| 66 |
|
| 67 |
if (tests) |
| 68 |
ostrm << ","; |
| 69 |
ostrm << "\n" |
| 70 |
" {\n" |
| 71 |
" \"name\": \"" |
| 72 |
<< basic_suite_names[i] |
| 73 |
<< "\",\n" |
| 74 |
" \"tests\": 4,\n" |
| 75 |
" \"testsuite\": [\n" |
| 76 |
" { \"name\": \"case_foo\", \"file\": " |
| 77 |
"\"file1.cpp\", \"line\": 1 },\n" |
| 78 |
" { \"name\": \"case_bar\", \"file\": " |
| 79 |
"\"file with spaces.cpp\", \"line\": 2 },\n" |
| 80 |
" { \"name\": \"DISABLED_disabled_case\", " |
| 81 |
"\"file\": \"file1.cpp\", \"line\": 3 },\n" |
| 82 |
" { \"name\": \"DISABLEDnot_really_case\", " |
| 83 |
"\"file\": \"file1.cpp\", \"line\": 4 }\n" |
| 84 |
" ]\n" |
| 85 |
" }"; |
| 86 |
tests += 4; |
| 87 |
} |
| 88 |
} |
| 89 |
if (!is_filtered) { |
| 90 |
std::cout << "DISABLED_disabled." << std::endl; |
| 91 |
std::cout << " case" << std::endl; |
| 92 |
std::cout << "DISABLEDnotreally." << std::endl; |
| 93 |
std::cout << " case" << std::endl; |
| 94 |
|
| 95 |
if (tests) |
| 96 |
ostrm << ","; |
| 97 |
ostrm << "\n" |
| 98 |
" {\n" |
| 99 |
" \"name\": \"DISABLED_disabled\",\n" |
| 100 |
" \"tests\": 1,\n" |
| 101 |
" \"testsuite\": [\n" |
| 102 |
" { \"name\": \"case\", \"file\": " |
| 103 |
"\"file2.cpp\", \"line\": 1 }\n" |
| 104 |
" ]\n" |
| 105 |
" },\n" |
| 106 |
" {\n" |
| 107 |
" \"name\": \"DISABLEDnotreally\",\n" |
| 108 |
" \"tests\": 1,\n" |
| 109 |
" \"testsuite\": [\n" |
| 110 |
" { \"name\": \"case\", \"file\": " |
| 111 |
"\"file2.cpp\", \"line\": 2 }\n" |
| 112 |
" ]\n" |
| 113 |
" }"; |
| 114 |
tests += 2; |
| 115 |
} |
| 116 |
if (add_typed_tests) { |
| 117 |
char const* typed_suite_names[] = { "typed", "ns.typed", |
| 118 |
"prefix/typed" }; |
| 119 |
for (size_t i = 0; i < ARRAY_SIZE(typed_suite_names); i++) { |
| 120 |
std::cout << typed_suite_names[i] << "/0. # TypeParam = short\n"; |
| 121 |
std::cout << " case" << std::endl; |
| 122 |
std::cout << typed_suite_names[i] << "/1. # TypeParam = float\n"; |
| 123 |
std::cout << " case" << std::endl; |
| 124 |
std::cout << typed_suite_names[i] << "/42. # TypeParam = char\n"; |
| 125 |
std::cout << " case" << std::endl; |
| 126 |
std::cout << typed_suite_names[i] << "/named. # TypeParam = int\n"; |
| 127 |
std::cout << " case" << std::endl; |
| 128 |
|
| 129 |
if (tests) |
| 130 |
ostrm << ","; |
| 131 |
ostrm << "\n" |
| 132 |
" {\n" |
| 133 |
" \"name\": \"" |
| 134 |
<< typed_suite_names[i] |
| 135 |
<< "/0\",\n" |
| 136 |
" \"tests\": 1, \"testsuite\": [ { \"name\": " |
| 137 |
"\"case\", \"type_param\": \"short\", \"file\": " |
| 138 |
"\"file3.cpp\", \"line\": 1 } ]\n" |
| 139 |
" },\n" |
| 140 |
" {\n" |
| 141 |
" \"name\": \"" |
| 142 |
<< typed_suite_names[i] |
| 143 |
<< "/1\",\n" |
| 144 |
" \"tests\": 1, \"testsuite\": [ { \"name\": " |
| 145 |
"\"case\", \"type_param\": \"float\", \"file\": " |
| 146 |
"\"file3.cpp\", \"line\": 2 } ]\n" |
| 147 |
" },\n" |
| 148 |
" {\n" |
| 149 |
" \"name\": \"" |
| 150 |
<< typed_suite_names[i] |
| 151 |
<< "/42\",\n" |
| 152 |
" \"tests\": 1, \"testsuite\": [ { \"name\": " |
| 153 |
"\"case\", \"type_param\": \"char\", \"file\": " |
| 154 |
"\"file3.cpp\", \"line\": 3 } ]\n" |
| 155 |
" },\n" |
| 156 |
" {\n" |
| 157 |
" \"name\": \"" |
| 158 |
<< typed_suite_names[i] |
| 159 |
<< "/named\",\n" |
| 160 |
" \"tests\": 1, \"testsuite\": [ { \"name\": " |
| 161 |
"\"case\", \"type_param\": \"int\", \"file\": \"file3.cpp\", " |
| 162 |
"\"line\": 4 } ]\n" |
| 163 |
" }"; |
| 164 |
tests += 4; |
| 165 |
} |
| 166 |
} |
| 167 |
if (add_value_tests) { |
| 168 |
char const* value_suite_names[] = { "value", "ns.value", |
| 169 |
"prefix/value" }; |
| 170 |
for (size_t i = 0; i < ARRAY_SIZE(value_suite_names); i++) { |
| 171 |
std::cout << value_suite_names[i] << "/test." << std::endl; |
| 172 |
std::cout << " case/0 # GetParam() = 1" << std::endl; |
| 173 |
std::cout << " case/1 # GetParam() = \"foo\"" << std::endl; |
| 174 |
std::cout << " case/named # GetParam() = 'c'" << std::endl; |
| 175 |
|
| 176 |
if (tests) |
| 177 |
ostrm << ","; |
| 178 |
ostrm |
| 179 |
<< "\n" |
| 180 |
" {\n" |
| 181 |
" \"name\": \"" |
| 182 |
<< value_suite_names[i] << "/test" |
| 183 |
<< "\",\n" |
| 184 |
" \"tests\": 3,\n" |
| 185 |
" \"testsuite\": [\n" |
| 186 |
" { \"name\": \"case/0\", \"value_param\": \"1\", " |
| 187 |
"\"file\": \"file4.cpp\", \"line\": 1 },\n" |
| 188 |
" { \"name\": \"case/1\", \"value_param\": " |
| 189 |
"\"\\\"foo\\\"\", \"file\": \"file4.cpp\", \"line\": 2 },\n" |
| 190 |
" { \"name\": \"case/named\", \"value_param\": " |
| 191 |
"\"'c'\", \"file\": \"file4.cpp\", \"line\": 3 }\n" |
| 192 |
" ]\n" |
| 193 |
" }"; |
| 194 |
tests += 3; |
| 195 |
} |
| 196 |
char const* param_suite_names[] = { "param", "ns.param", |
| 197 |
"prefix/param" }; |
| 198 |
for (size_t j = 0; j < ARRAY_SIZE(param_suite_names); j++) { |
| 199 |
std::cout << param_suite_names[j] << "/special." << std::endl; |
| 200 |
std::cout << " case/0 # GetParam() = \"semicolon;\"" << std::endl; |
| 201 |
std::cout << " case/1 # GetParam() = \"backslash\\\"" << std::endl; |
| 202 |
std::cout << " case/2 # GetParam() = \"${var}\"" << std::endl; |
| 203 |
std::cout << " case/3 # GetParam() = '['" << std::endl; |
| 204 |
std::cout << " case/4 # GetParam() = \"]]=]\"" << std::endl; |
| 205 |
std::cout << " case/5 # GetParam() = \"__osbtext\"" << std::endl; |
| 206 |
std::cout << " case/6 # GetParam() = \"__csb___text\"" << std::endl; |
| 207 |
std::cout << " case/7 # GetParam() = \"S o m e \"" << std::endl; |
| 208 |
|
| 209 |
ostrm |
| 210 |
<< ",\n" |
| 211 |
" {\n" |
| 212 |
" \"name\": \"" |
| 213 |
<< param_suite_names[j] << "/special" |
| 214 |
<< "\",\n" |
| 215 |
" \"tests\": 8,\n" |
| 216 |
" \"testsuite\": [\n" |
| 217 |
" { \"name\": \"case/0\", \"value_param\": " |
| 218 |
"\"\\\"semicolon;\\\"\", \"file\": \"file4.cpp\", \"line\": 1 " |
| 219 |
"},\n" |
| 220 |
" { \"name\": \"case/1\", \"value_param\": " |
| 221 |
"\"\\\"backslash\\\\\\\"\", \"file\": \"file4.cpp\", \"line\": 2 " |
| 222 |
"},\n" |
| 223 |
" { \"name\": \"case/2\", \"value_param\": " |
| 224 |
"\"\\\"${var}\\\"\", \"file\": \"file4.cpp\", \"line\": 3 },\n" |
| 225 |
" { \"name\": \"case/3\", \"value_param\": " |
| 226 |
"\"'['\", \"file\": \"file4.cpp\", \"line\": 4 },\n" |
| 227 |
" { \"name\": \"case/4\", \"value_param\": " |
| 228 |
"\"\\\"]]=]\\\"\", \"file\": \"file4.cpp\", \"line\": 5 },\n" |
| 229 |
" { \"name\": \"case/5\", \"value_param\": " |
| 230 |
"\"\\\"__osbtext\\\"\", \"file\": \"file4.cpp\", \"line\": 5 },\n" |
| 231 |
" { \"name\": \"case/6\", \"value_param\": " |
| 232 |
"\"\\\"__csb___text\\\"\", \"file\": \"file4.cpp\", \"line\": 5 " |
| 233 |
"},\n" |
| 234 |
" { \"name\": \"case/7\", \"value_param\": " |
| 235 |
"\"\\\"S o m e \\\"\", \"file\": \"file4.cpp\", \"line\": 6 " |
| 236 |
"}\n" |
| 237 |
" ]\n" |
| 238 |
" }"; |
| 239 |
tests += 8; |
| 240 |
} |
| 241 |
} |
| 242 |
if (add_value_tests || add_typed_tests || add_dynamic_tests) { |
| 243 |
char const* both_suite_names[] = { "both_suite", "both/suite", |
| 244 |
"ns.both/suite", |
| 245 |
"prefix/both/suite" }; |
| 246 |
for (size_t k = 0; k < ARRAY_SIZE(both_suite_names); k++) { |
| 247 |
std::cout << both_suite_names[k] << ". # TypeParam = TYPE" |
| 248 |
<< std::endl; |
| 249 |
std::cout << " test # GetParam() = VALUE" << std::endl; |
| 250 |
std::cout << " case/test # GetParam() = VALUE" << std::endl; |
| 251 |
|
| 252 |
if (tests) |
| 253 |
ostrm << ","; |
| 254 |
ostrm << "\n" |
| 255 |
" {\n" |
| 256 |
" \"name\": \"" |
| 257 |
<< both_suite_names[k] |
| 258 |
<< "\",\n" |
| 259 |
" \"tests\": 1,\n" |
| 260 |
" \"testsuite\": [\n" |
| 261 |
" { \"name\": \"test\", \"type_param\": " |
| 262 |
"\"TYPE\", \"value_param\": \"VALUE\", \"file\": " |
| 263 |
"\"file5.cpp\", \"line\": 1 },\n" |
| 264 |
" { \"name\": \"case/test\", " |
| 265 |
"\"type_param\": \"TYPE\", \"value_param\": \"VALUE\", " |
| 266 |
"\"file\": \"file5.cpp\", \"line\": 2 }\n" |
| 267 |
" ]\n" |
| 268 |
" }"; |
| 269 |
tests += 2; |
| 270 |
} |
| 271 |
} |
| 272 |
ostrm << "\n" |
| 273 |
" ],\n" |
| 274 |
" \"tests\": " |
| 275 |
<< tests << "\n}"; |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
if (argc > 5) { |
| 280 |
// Simple test of EXTRA_ARGS |
| 281 |
if (std::string(argv[3]) == "how" && std::string(argv[4]) == "now" && |
| 282 |
std::string(argv[5]) == "\"brown\" cow") { |
| 283 |
return 0; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// Print arguments for debugging, if we didn't get the expected arguments |
| 288 |
for (int i = 1; i < argc; ++i) { |
| 289 |
std::cerr << "arg[" << i << "]: '" << argv[i] << "'\n"; |
| 290 |
} |
| 291 |
|
| 292 |
return 1; |
| 293 |
} |