xml_output.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. int main(int argc, char** argv)
  5. {
  6. // Note: GoogleTestXML.cmake doesn't actually depend on Google Test as such;
  7. // it only mimics the output file creation using the path passed to this
  8. // test without any content
  9. for (int i = 0; i < argc; i++) {
  10. std::string param(argv[i]);
  11. if (param.find("--gtest_list_tests") != std::string::npos) {
  12. // This actually defines the name of the file passed in the 2nd run
  13. std::cout << "GoogleTestXML." << std::endl;
  14. std::cout << " Foo" << std::endl;
  15. // When changing these names, make sure to adapt the folder creation
  16. // in GoogleTestXML.cmake
  17. std::cout << "GoogleTestXMLSpecial/cases." << std::endl;
  18. std::cout << " case/0 # GetParam() = 42" << std::endl;
  19. std::cout << " case/1 # GetParam() = \"string\"" << std::endl;
  20. std::cout << " case/2 # GetParam() = \"path/like\"" << std::endl;
  21. } else if (param.find("--gtest_output=json:") != std::string::npos) {
  22. std::string::size_type split = param.find(":");
  23. std::string filepath = param.substr(split + 1);
  24. // The full file path is passed
  25. std::ofstream ostrm(filepath.c_str(), std::ios::binary);
  26. if (!ostrm) {
  27. std::cerr << "Failed to create file: " << filepath.c_str()
  28. << std::endl;
  29. return 1;
  30. }
  31. ostrm
  32. << "{\n"
  33. " \"tests\": 4,\n"
  34. " \"name\": \"AllTests\",\n"
  35. " \"testsuites\": [\n"
  36. " {\n"
  37. " \"name\": \"GoogleTestXML\",\n"
  38. " \"tests\": 1,\n"
  39. " \"testsuite\": [\n"
  40. " { \"name\": \"Foo\", \"file\": \"file.cpp\", "
  41. "\"line\": 42 }\n"
  42. " ]\n"
  43. " },\n"
  44. " {\n"
  45. " \"name\": \"GoogleTestXMLSpecial\\/cases\",\n"
  46. " \"tests\": 3,\n"
  47. " \"testsuite\": [\n"
  48. " { \"name\": \"case\\/0\", \"value_param\": "
  49. "\"42\", \"file\": \"file2.cpp\", \"line\": 47 },\n"
  50. " { \"name\": \"case\\/1\", \"value_param\": "
  51. "\"\\\"string\\\"\", \"file\": \"file2.cpp\", \"line\": 47 },\n"
  52. " { \"name\": \"case\\/2\", \"value_param\": "
  53. "\"\\\"path/like\\\"\", \"file\": \"file2.cpp\", \"line\": 47 }\n"
  54. " ]\n"
  55. " }\n"
  56. " ]\n"
  57. "}";
  58. } else if (param.find("--gtest_output=xml:") != std::string::npos) {
  59. std::string::size_type split = param.find(":");
  60. std::string filepath = param.substr(split + 1);
  61. // The full file path is passed
  62. std::ofstream ostrm(filepath.c_str(), std::ios::binary);
  63. if (!ostrm) {
  64. std::cerr << "Failed to create file: " << filepath.c_str()
  65. << std::endl;
  66. return 1;
  67. }
  68. ostrm << "--gtest_output=xml: mockup file\n";
  69. }
  70. }
  71. return 0;
  72. }