Line data Source code
1 : /**
2 : * @file CommandLineInterpreter_test.cxx CommandLineInterpreter class Unit Tests
3 : *
4 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include "CommandLineInterpreter.hpp"
10 :
11 : #define BOOST_TEST_MODULE CommandLineInterpreter_test // NOLINT
12 :
13 : #include "boost/test/unit_test.hpp"
14 :
15 : #include <string>
16 : #include <vector>
17 :
18 : using namespace dunedaq::appfwk;
19 :
20 : // n.b. Because CommandLineInterpreter requires a non-const char**,
21 : // some coding guidelines need to be bent for the sake of simplicity
22 :
23 : // Turning off clang-format to preserve formatting of commandline array declarations
24 : // clang-format off
25 :
26 : BOOST_AUTO_TEST_SUITE(CommandLineInterpreter_test)
27 :
28 2 : BOOST_AUTO_TEST_CASE(ParseNoOptions)
29 : {
30 1 : char** arg_list = new char*[1]{ (char*)("CommandLineInterpreter_test") }; // NOLINT
31 3 : BOOST_REQUIRE_EXCEPTION(CommandLineInterpreter::parse(1, arg_list), bpo::error, [&](bpo::error) { return true; });
32 :
33 1 : delete[] arg_list; // NOLINT
34 1 : }
35 :
36 2 : BOOST_AUTO_TEST_CASE(AskForHelp)
37 : {
38 :
39 1 : char** arg_list = new char*[2]{ (char*)("CommandLineInterpreter_test"), (char*)("-h") }; // NOLINT
40 1 : auto parsed = CommandLineInterpreter::parse(2, arg_list);
41 :
42 1 : BOOST_REQUIRE_EQUAL(parsed.help_requested, true);
43 1 : BOOST_REQUIRE_EQUAL(parsed.app_name, "");
44 1 : BOOST_REQUIRE_EQUAL(parsed.session_name, "");
45 1 : BOOST_REQUIRE_EQUAL(parsed.command_facility_plugin_name, "");
46 1 : BOOST_REQUIRE_EQUAL(parsed.conf_service_plugin_name, "");
47 1 : BOOST_REQUIRE_EQUAL(parsed.other_options.size(), 0);
48 :
49 1 : delete[] arg_list; // NOLINT
50 1 : }
51 :
52 2 : BOOST_AUTO_TEST_CASE(ParseCommandFacility)
53 : {
54 1 : char** arg_list =
55 1 : new char*[3]{ (char*)("CommandLineInterpreter_test"), (char*)("-c"), (char*)("stdin://") }; // NOLINT
56 3 : BOOST_REQUIRE_EXCEPTION(CommandLineInterpreter::parse(3, arg_list), bpo::error, [&](bpo::error) { return true; });
57 :
58 1 : delete[] arg_list; // NOLINT
59 1 : }
60 :
61 2 : BOOST_AUTO_TEST_CASE(ParseName)
62 : {
63 :
64 1 : char** arg_list = new char*[5]{
65 : (char*)("CommandLineInterpreter_test"), // NOLINT
66 : (char*)("-n"), (char*)("cli_test"), // NOLINT
67 : (char*)("-d"), (char*)("file://"), // NOLINT
68 1 : };
69 3 : BOOST_REQUIRE_EXCEPTION(CommandLineInterpreter::parse(5, arg_list), bpo::error, [&](bpo::error) { return true; });
70 :
71 1 : delete[] arg_list; // NOLINT
72 1 : }
73 :
74 2 : BOOST_AUTO_TEST_CASE(ParseNameAndCommandFacility)
75 : {
76 1 : char** arg_list = new char* [11] {
77 : (char*)("CommandLineInterpreter_test"), // NOLINT
78 : (char*)("-c"), (char*)("stdin://"), // NOLINT
79 : (char*)("-d"), (char*)("file://"), // NOLINT
80 : (char*)("-s"), (char*)("my_test_session"), // NOLINT
81 : (char*)("-k"), (char*)("test_session"), // NOLINT
82 : (char*)("-n"), (char*)("cli_test") // NOLINT
83 1 : };
84 1 : auto parsed = CommandLineInterpreter::parse(11, arg_list);
85 :
86 1 : BOOST_REQUIRE_EQUAL(parsed.help_requested, false);
87 1 : BOOST_REQUIRE_EQUAL(parsed.app_name, "cli_test");
88 1 : BOOST_REQUIRE_EQUAL(parsed.session_name, "my_test_session");
89 1 : BOOST_REQUIRE_EQUAL(parsed.configuration_id, "test_session");
90 1 : BOOST_REQUIRE_EQUAL(parsed.command_facility_plugin_name, "stdin://");
91 1 : BOOST_REQUIRE_EQUAL(parsed.conf_service_plugin_name, "file://");
92 1 : BOOST_REQUIRE_EQUAL(parsed.other_options.size(), 0);
93 :
94 1 : delete[] arg_list; // NOLINT
95 1 : }
96 :
97 2 : BOOST_AUTO_TEST_CASE(ParseSession)
98 : {
99 1 : char** arg_list = new char* [11] {
100 : (char*)("CommandLineInterpreter_test"), // NOLINT
101 : (char*)("-c"), (char*)("stdin://"), // NOLINT
102 : (char*)("-d"), (char*)("file://"), // NOLINT
103 : (char*)("-s"), (char*)("my_test_session"), // NOLINT
104 : (char*)("-k"), (char*)("test_session"), // NOLINT
105 : (char*)("-n"), (char*)("cli_test") // NOLINT
106 1 : };
107 1 : auto parsed = CommandLineInterpreter::parse(11, arg_list);
108 :
109 1 : BOOST_REQUIRE_EQUAL(parsed.help_requested, false);
110 1 : BOOST_REQUIRE_EQUAL(parsed.app_name, "cli_test");
111 1 : BOOST_REQUIRE_EQUAL(parsed.session_name, "my_test_session");
112 1 : BOOST_REQUIRE_EQUAL(parsed.configuration_id, "test_session");
113 1 : BOOST_REQUIRE_EQUAL(parsed.command_facility_plugin_name, "stdin://");
114 1 : BOOST_REQUIRE_EQUAL(parsed.conf_service_plugin_name, "file://");
115 1 : BOOST_REQUIRE_EQUAL(parsed.other_options.size(), 0);
116 :
117 1 : delete[] arg_list; // NOLINT
118 1 : }
119 2 : BOOST_AUTO_TEST_CASE(ParseOtherOption)
120 : {
121 1 : char** arg_list = new char* [12] {
122 : (char*)("CommandLineInterpreter_test"), // NOLINT
123 : (char*)("-c"), (char*)("stdin://"), // NOLINT
124 : (char*)("-d"), (char*)("file://"), // NOLINT
125 : (char*)("-s"), (char*)("my_test_session"), // NOLINT
126 : (char*)("-k"), (char*)("test_session"), // NOLINT
127 : (char*)("-n"), (char*)("cli_test"), // NOLINT
128 : (char*)("--some-other-option") // NOLINT
129 1 : };
130 1 : auto parsed = CommandLineInterpreter::parse(12, arg_list);
131 :
132 1 : BOOST_REQUIRE_EQUAL(parsed.help_requested, false);
133 1 : BOOST_REQUIRE_EQUAL(parsed.app_name, "cli_test");
134 1 : BOOST_REQUIRE_EQUAL(parsed.session_name, "my_test_session");
135 1 : BOOST_REQUIRE_EQUAL(parsed.configuration_id, "test_session");
136 1 : BOOST_REQUIRE_EQUAL(parsed.command_facility_plugin_name, "stdin://");
137 1 : BOOST_REQUIRE_EQUAL(parsed.conf_service_plugin_name, "file://");
138 1 : BOOST_REQUIRE_EQUAL(parsed.other_options.size(), 1);
139 1 : BOOST_REQUIRE_EQUAL(parsed.other_options[0], "--some-other-option");
140 :
141 1 : delete[] arg_list; // NOLINT
142 1 : }
143 2 : BOOST_AUTO_TEST_CASE(ParseMultipleOtherOptions)
144 : {
145 1 : char** arg_list = new char* [15] {
146 : (char*)("CommandLineInterpreter_test"), // NOLINT
147 : (char*)("-c"), (char*)("stdin://"), // NOLINT
148 : (char*)("-d"), (char*)("file://"), // NOLINT
149 : (char*)("-n"), (char*)("cli_test"), // NOLINT
150 : (char*)("-s"), (char*)("my_test_session"), // NOLINT
151 : (char*)("-k"), (char*)("test_session"), // NOLINT
152 : (char*)("--some-other-option"), // NOLINT
153 : (char*)("--yet-another-option=4"), // NOLINT
154 : (char*)("-u"), (char*)("me") // NOLINT
155 1 : };
156 1 : auto parsed = CommandLineInterpreter::parse(15, arg_list);
157 :
158 1 : BOOST_REQUIRE_EQUAL(parsed.help_requested, false);
159 1 : BOOST_REQUIRE_EQUAL(parsed.app_name, "cli_test");
160 1 : BOOST_REQUIRE_EQUAL(parsed.session_name, "my_test_session");
161 1 : BOOST_REQUIRE_EQUAL(parsed.configuration_id, "test_session");
162 1 : BOOST_REQUIRE_EQUAL(parsed.command_facility_plugin_name, "stdin://");
163 1 : BOOST_REQUIRE_EQUAL(parsed.conf_service_plugin_name, "file://");
164 1 : BOOST_REQUIRE_EQUAL(parsed.other_options.size(), 4);
165 1 : BOOST_REQUIRE_EQUAL(parsed.other_options[0], "--some-other-option");
166 1 : BOOST_REQUIRE_EQUAL(parsed.other_options[1], "--yet-another-option=4");
167 1 : BOOST_REQUIRE_EQUAL(parsed.other_options[2], "-u");
168 1 : BOOST_REQUIRE_EQUAL(parsed.other_options[3], "me");
169 1 : delete[] arg_list; // NOLINT
170 1 : }
171 :
172 : BOOST_AUTO_TEST_SUITE_END()
173 :
174 : // clang-format on
|