Line data Source code
1 : #include "test/common.h"
2 :
3 : #include "artdaq-database/DataFormats/Json/json_common.h"
4 :
5 : namespace bpo = boost::program_options;
6 : using namespace artdaq::database;
7 :
8 7 : int main(int argc, char* argv[]) try {
9 7 : std::ostringstream descstr;
10 7 : descstr << argv[0] << " <-c <config-file>> <other-options>";
11 :
12 7 : bpo::options_description desc = descstr.str();
13 :
14 7 : desc.add_options()("config,c", bpo::value<std::string>(), "Configuration file.")("outputformat,f", bpo::value<std::string>(),
15 7 : "Output file format.")("help,h", "produce help message");
16 :
17 7 : bpo::variables_map vm;
18 :
19 : try {
20 7 : bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
21 7 : bpo::notify(vm);
22 0 : } catch (bpo::error const& e) {
23 0 : std::cerr << "Exception from command line processing in " << argv[0] << ": " << e.what() << "\n";
24 0 : return process_exit_code::INVALID_ARGUMENT;
25 0 : }
26 :
27 14 : if (vm.count("help") != 0u) {
28 0 : std::cout << desc << std::endl;
29 0 : return process_exit_code::HELP;
30 : }
31 :
32 14 : if (vm.count("config") == 0u) {
33 : std::cerr << "Exception from command line processing in " << argv[0] << ": no configuration file given.\n"
34 : << "For usage and an options list, please do '" << argv[0] << " --help"
35 0 : << "'.\n";
36 0 : return process_exit_code::INVALID_ARGUMENT | 1;
37 : }
38 :
39 7 : auto file_name = vm["config"].as<std::string>();
40 :
41 7 : std::ifstream is(file_name);
42 :
43 7 : std::stringstream json_buffer;
44 7 : json_buffer << is.rdbuf();
45 7 : is.close();
46 :
47 7 : auto json = json_buffer.str();
48 :
49 : namespace jsn = artdaq::database::json;
50 :
51 : auto reader = jsn::JsonReader{};
52 :
53 7 : jsn::object_t doc_ast1;
54 :
55 7 : if (!reader.read(json, doc_ast1)) {
56 0 : std::cout << "Failed reading JSON buffer: << " << json_buffer.str() << ">>\n";
57 :
58 0 : return process_exit_code::FAILURE;
59 : }
60 :
61 : auto writer = jsn::JsonWriter{};
62 :
63 7 : auto jsonout = std::string();
64 :
65 7 : if (!writer.write(doc_ast1, jsonout)) {
66 0 : return process_exit_code::FAILURE;
67 : }
68 7 : std::cout << "Initial:" << json << "\n";
69 7 : std::cout << "Converted:" << jsonout << "\n";
70 :
71 7 : jsn::object_t doc_ast2;
72 :
73 7 : if (!reader.read(jsonout, doc_ast2)) {
74 0 : std::cout << "Failed reading JSON buffer: << " << jsonout << ">>\n";
75 :
76 0 : return process_exit_code::FAILURE;
77 : }
78 :
79 7 : auto compare_result = doc_ast1 == doc_ast2;
80 :
81 7 : if (compare_result.first) {
82 7 : std::cout << "Returned:" << jsonout << "\n";
83 7 : return process_exit_code::SUCCESS;
84 : } else {
85 0 : std::cout << "Test failed (expected!=returned); error message: " << compare_result.second << "\n";
86 :
87 0 : std::cout << "Expected:" << json << "\n";
88 0 : std::cout << "Returned:" << jsonout << "\n";
89 :
90 0 : return process_exit_code::FAILURE;
91 : }
92 7 : } catch (...) {
93 0 : std::cerr << "Process exited with error: " << ::debug::current_exception_diagnostic_information();
94 0 : return process_exit_code::UNCAUGHT_EXCEPTION;
95 0 : }
|