LCOV - code coverage report
Current view: top level - /opt/artdaq/srcs/artdaq-database/test/DataFormats/Xml - XMLConvert_t.cc (source / functions) Coverage Total Hit
Test: artdaq.info.cleaned Lines: 52.0 % 98 51
Test Date: 2026-10-29 00:35:58 Functions: 66.7 % 6 4

            Line data    Source code
       1              : 
       2              : #include "test/common.h"
       3              : 
       4              : #include "artdaq-database/BasicTypes/basictypes.h"
       5              : #include "artdaq-database/DataFormats/Json/json_common.h"
       6              : #include "artdaq-database/DataFormats/Xml/convertxml2json.h"
       7              : #include "artdaq-database/DataFormats/Xml/xml_common.h"
       8              : #include "artdaq-database/DataFormats/Xml/xmljsondb.h"
       9              : 
      10              : #include <fstream>
      11              : #include <regex>
      12              : 
      13              : namespace bpo = boost::program_options;
      14              : using namespace artdaq::database;
      15              : 
      16              : using test_case = bool (*)(const std::string&, const std::string&, const std::string&);
      17              : 
      18              : bool test_convertxml2json(std::string const& /*input*/, std::string const& /*compare*/, std::string const& /*source_filename*/);
      19              : bool test_convertjson2xml(std::string const& /*input*/, std::string const& /*compare*/, std::string const& /*source_filename*/);
      20              : 
      21              : // Helper function to extract test number from filename
      22              : // e.g., "test001.src.xml" -> "001"
      23            0 : std::string extract_test_number(const std::string& filename) {
      24            0 :   std::regex pattern(R"(test(\d{3}))");
      25            0 :   std::smatch matches;
      26              : 
      27            0 :   if (std::regex_search(filename, matches, pattern)) {
      28            0 :     return matches[1].str();
      29              :   }
      30              : 
      31            0 :   return "unknown";
      32            0 : }
      33              : 
      34              : // Helper function to write content to a file
      35            0 : bool write_to_file(const std::string& filename, const std::string& content) {
      36            0 :   std::ofstream outfile(filename);
      37              : 
      38            0 :   if (!outfile.is_open()) {
      39            0 :     std::cerr << "ERROR: Failed to open file for writing: " << filename << "\n";
      40            0 :     return false;
      41              :   }
      42              : 
      43            0 :   outfile << content;
      44            0 :   outfile.close();
      45              : 
      46            0 :   std::cout << "Debug file written: " << filename << "\n";
      47            0 :   return true;
      48            0 : }
      49              : 
      50            2 : int main(int argc, char* argv[]) {
      51            2 :   artdaq::database::xml::debug::XmlReader();
      52            2 :   artdaq::database::xml::debug::XmlWriter();
      53            2 :   artdaq::database::xmljson::debug::Xml2Json();
      54            2 :   artdaq::database::xmljson::debug::XmlJson();
      55              : 
      56            2 :   debug::registerUngracefullExitHandlers();
      57            2 :   artdaq::database::useFakeTime(true);
      58              : 
      59            2 :   std::ostringstream descstr;
      60            2 :   descstr << argv[0] << " <-s <source-file>> <-c <compare-with-file>> <-t <test-name>> (available test names: xml2json,json2xml)";
      61              : 
      62            2 :   bpo::options_description desc = descstr.str();
      63              : 
      64            2 :   desc.add_options()("source,s", bpo::value<std::string>(), "Input source file.")(
      65            2 :       "compare,c", bpo::value<std::string>(), "Expected result of convertion.")("testname,t", bpo::value<std::string>(), "Test name.")(
      66              :       "help,h", "produce help message");
      67              : 
      68            2 :   bpo::variables_map vm;
      69              : 
      70              :   try {
      71            2 :     bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
      72            2 :     bpo::notify(vm);
      73            0 :   } catch (bpo::error const& e) {
      74            0 :     std::cerr << "Exception from command line processing in " << argv[0] << ": " << e.what() << "\n";
      75            0 :     return process_exit_code::INVALID_ARGUMENT;
      76            0 :   }
      77              : 
      78            4 :   if (vm.count("help") != 0u) {
      79            0 :     std::cout << desc << std::endl;
      80            0 :     return process_exit_code::HELP;
      81              :   }
      82              : 
      83            4 :   if (vm.count("source") == 0u) {
      84              :     std::cerr << "Exception from command line processing in " << argv[0] << ": no source file given.\n"
      85              :               << "For usage and an options list, please do '" << argv[0] << " --help"
      86            0 :               << "'.\n";
      87            0 :     return process_exit_code::INVALID_ARGUMENT | 1;
      88              :   }
      89              : 
      90            4 :   if (vm.count("compare") == 0u) {
      91              :     std::cerr << "Exception from command line processing in " << argv[0] << ": no compare file given.\n"
      92              :               << "For usage and an options list, please do '" << argv[0] << " --help"
      93            0 :               << "'.\n";
      94            0 :     return process_exit_code::INVALID_ARGUMENT | 2;
      95              :   }
      96              : 
      97            4 :   if (vm.count("testname") == 0u) {
      98              :     std::cerr << "Exception from command line processing in " << argv[0] << ": no test name given.\n"
      99              :               << "For usage and an options list, please do '" << argv[0] << " --help"
     100            0 :               << "'.\n";
     101            0 :     return process_exit_code::INVALID_ARGUMENT | 3;
     102              :   }
     103              : 
     104            4 :   auto input_name = vm["source"].as<std::string>();
     105            4 :   auto compare_name = vm["compare"].as<std::string>();
     106            2 :   auto test_name = vm["testname"].as<std::string>();
     107              : 
     108            2 :   std::ifstream is1(input_name);
     109              : 
     110            2 :   std::string input((std::istreambuf_iterator<char>(is1)), std::istreambuf_iterator<char>());
     111              : 
     112            2 :   std::ifstream is2(compare_name);
     113              : 
     114            2 :   std::string compare((std::istreambuf_iterator<char>(is2)), std::istreambuf_iterator<char>());
     115              : 
     116            2 :   auto runTest = [](std::string const& name) {
     117            8 :     auto tests = std::map<std::string, test_case>{{"xml2json", test_convertxml2json}, {"json2xml", test_convertjson2xml}};
     118              : 
     119            2 :     std::cout << "Running test:<" << name << ">\n";
     120              : 
     121            4 :     return tests.at(name);
     122            4 :   };
     123              : 
     124            2 :   auto testResult = runTest(test_name)(input, compare, input_name);
     125              : 
     126            2 :   return static_cast<int>(!testResult);
     127            2 : }
     128              : 
     129            1 : bool test_convertxml2json(std::string const& input, std::string const& compare, std::string const& source_filename) {
     130            1 :   confirm(!input.empty());
     131            1 :   confirm(!compare.empty());
     132              : 
     133            1 :   auto output = std::string();
     134              : 
     135            1 :   if (!artdaq::database::xmljson::xml_to_json(input, output)) {
     136            0 :     return false;
     137              :   }
     138              : 
     139            1 :   auto compare_result = artdaq::database::json::compare_json_objects(output, compare);
     140            1 :   if (compare_result.first) {
     141            1 :     return true;
     142              :   } else {
     143            0 :     std::cout << "Convertion failed; error: " << compare_result.second << "\n";
     144            0 :     std::cerr << "output:\n" << output << "\n";
     145            0 :     std::cerr << "expected:\n" << compare << "\n";
     146              : 
     147              :     // Write debug files
     148            0 :     std::string test_number = extract_test_number(source_filename);
     149            0 :     write_to_file("test" + test_number + ".output.json", output);
     150            0 :     write_to_file("test" + test_number + ".expected.json", compare);
     151            0 :   }
     152              : 
     153            0 :   return false;
     154            1 : }
     155              : 
     156            1 : bool test_convertjson2xml(std::string const& input, std::string const& compare, std::string const& source_filename) {
     157            1 :   confirm(!input.empty());
     158            1 :   confirm(!compare.empty());
     159              : 
     160            1 :   auto output = std::string();
     161              : 
     162            1 :   if (!artdaq::database::xmljson::json_to_xml(input, output)) {
     163            0 :     return false;
     164              :   }
     165              : 
     166            1 :   if (output == compare) {
     167            1 :     return true;
     168              :   } else {
     169            0 :     std::cout << "Convertion failed. \n";
     170            0 :     std::cerr << "output:\n" << output << "\n";
     171            0 :     std::cerr << "expected:\n" << compare << "\n";
     172              : 
     173              :     // Write debug files
     174            0 :     std::string test_number = extract_test_number(source_filename);
     175            0 :     write_to_file("test" + test_number + ".output.json", output);
     176            0 :     write_to_file("test" + test_number + ".expected.json", compare);
     177            0 :   }
     178              : 
     179            0 :   return false;
     180            1 : }
        

Generated by: LCOV version 2.0-1