Line data Source code
1 : // Copyright (c) 2006-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/advgetopt
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : // advgetopt
21 : //
22 : #include <advgetopt/exception.h>
23 :
24 :
25 : // self
26 : //
27 : #include "catch_main.h"
28 :
29 :
30 : // snapdev
31 : //
32 : #include <snapdev/safe_setenv.h>
33 :
34 :
35 : // C++
36 : //
37 : #include <fstream>
38 :
39 :
40 : // last include
41 : //
42 : #include <snapdev/poison.h>
43 :
44 :
45 :
46 6 : CATCH_TEST_CASE("program_name", "[program_name][valid][getopt]")
47 : {
48 6 : CATCH_START_SECTION("program_name: Verify a nullptr program name in argv[]s")
49 : {
50 1 : advgetopt::options_environment environment_options;
51 1 : environment_options.f_project_name = "unittest";
52 1 : environment_options.f_help_header = "Usage: verify program name handling";
53 :
54 1 : char const * cargv[] =
55 : {
56 : nullptr,
57 : "--verbose",
58 : nullptr
59 : };
60 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
61 1 : char ** argv = const_cast<char **>(cargv);
62 :
63 1 : advgetopt::getopt opt(environment_options);
64 :
65 1 : opt.parse_program_name(argv);
66 :
67 1 : CATCH_REQUIRE(opt.get_program_name().empty());
68 1 : CATCH_REQUIRE(opt.get_program_fullname().empty());
69 1 : }
70 6 : CATCH_END_SECTION()
71 :
72 6 : CATCH_START_SECTION("program_name: verify a program name with no path")
73 : {
74 1 : advgetopt::options_environment environment_options;
75 1 : environment_options.f_project_name = "unittest";
76 1 : environment_options.f_options = nullptr;
77 1 : environment_options.f_help_header = "Usage: verify program name handling";
78 :
79 1 : char const * cargv[] =
80 : {
81 : "basename-only.exe",
82 : "--verbose",
83 : nullptr
84 : };
85 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
86 1 : char ** argv = const_cast<char **>(cargv);
87 :
88 1 : advgetopt::getopt opt(environment_options);
89 :
90 1 : opt.parse_program_name(argv);
91 :
92 1 : CATCH_REQUIRE(opt.get_program_name() == "basename-only.exe");
93 1 : CATCH_REQUIRE(opt.get_program_fullname() == "basename-only.exe");
94 1 : }
95 6 : CATCH_END_SECTION()
96 :
97 6 : CATCH_START_SECTION("program_name: verify a program name with a relative path")
98 : {
99 1 : advgetopt::options_environment environment_options;
100 1 : environment_options.f_project_name = "unittest";
101 1 : environment_options.f_options = nullptr;
102 1 : environment_options.f_help_header = "Usage: verify program name handling";
103 :
104 1 : char const * cargv[] =
105 : {
106 : "project/bin/and-basename.tool",
107 : "--verbose",
108 : nullptr
109 : };
110 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
111 1 : char ** argv = const_cast<char **>(cargv);
112 :
113 1 : advgetopt::getopt opt(environment_options);
114 :
115 1 : opt.parse_program_name(argv);
116 :
117 1 : CATCH_REQUIRE(opt.get_program_name() == "and-basename.tool");
118 1 : CATCH_REQUIRE(opt.get_program_fullname() == "project/bin/and-basename.tool");
119 1 : }
120 6 : CATCH_END_SECTION()
121 :
122 6 : CATCH_START_SECTION("program_name: verify a program name with a relative path and backslashes")
123 : {
124 1 : advgetopt::options_environment environment_options;
125 1 : environment_options.f_project_name = "unittest";
126 1 : environment_options.f_options = nullptr;
127 1 : environment_options.f_help_header = "Usage: verify program name handling";
128 :
129 1 : char const * cargv[] =
130 : {
131 : "project\\bin\\and-basename.tool",
132 : "--verbose",
133 : nullptr
134 : };
135 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
136 1 : char ** argv = const_cast<char **>(cargv);
137 :
138 1 : advgetopt::getopt opt(environment_options);
139 :
140 1 : opt.parse_program_name(argv);
141 :
142 1 : CATCH_REQUIRE(opt.get_program_name() == "and-basename.tool");
143 1 : CATCH_REQUIRE(opt.get_program_fullname() == "project\\bin\\and-basename.tool");
144 1 : }
145 6 : CATCH_END_SECTION()
146 :
147 6 : CATCH_START_SECTION("program_name: verify a program name with a full path")
148 : {
149 1 : advgetopt::options_environment environment_options;
150 1 : environment_options.f_project_name = "unittest";
151 1 : environment_options.f_options = nullptr;
152 1 : environment_options.f_help_header = "Usage: verify program name handling";
153 :
154 1 : char const * cargv[] =
155 : {
156 : "/usr/bin/basename",
157 : "--verbose",
158 : nullptr
159 : };
160 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
161 1 : char ** argv = const_cast<char **>(cargv);
162 :
163 1 : advgetopt::getopt opt(environment_options);
164 :
165 1 : opt.parse_program_name(argv);
166 :
167 1 : CATCH_REQUIRE(opt.get_program_name() == "basename");
168 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/basename");
169 1 : }
170 6 : CATCH_END_SECTION()
171 :
172 6 : CATCH_START_SECTION("program_name: verify a program name with a full path and backslashes")
173 : {
174 1 : advgetopt::options_environment environment_options;
175 1 : environment_options.f_project_name = "unittest";
176 1 : environment_options.f_options = nullptr;
177 1 : environment_options.f_help_header = "Usage: verify program name handling";
178 :
179 1 : char const * cargv[] =
180 : {
181 : "\\usr\\bin\\basename",
182 : "--verbose",
183 : nullptr
184 : };
185 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
186 1 : char ** argv = const_cast<char **>(cargv);
187 :
188 1 : advgetopt::getopt opt(environment_options);
189 :
190 1 : opt.parse_program_name(argv);
191 :
192 1 : CATCH_REQUIRE(opt.get_program_name() == "basename");
193 1 : CATCH_REQUIRE(opt.get_program_fullname() == "\\usr\\bin\\basename");
194 1 : }
195 6 : CATCH_END_SECTION()
196 6 : }
197 :
198 :
199 2 : CATCH_TEST_CASE("project_name", "[project_name][valid][getopt]")
200 : {
201 2 : CATCH_START_SECTION("project_name: verify a nullptr project name")
202 : {
203 1 : advgetopt::options_environment environment_options;
204 1 : environment_options.f_help_header = "Usage: verify project name handling";
205 :
206 1 : advgetopt::getopt opt(environment_options);
207 :
208 1 : CATCH_REQUIRE(opt.get_project_name().empty());
209 1 : }
210 2 : CATCH_END_SECTION()
211 :
212 2 : CATCH_START_SECTION("project_name: verify an actual project name")
213 : {
214 1 : advgetopt::options_environment environment_options;
215 1 : environment_options.f_project_name = "unit-test";
216 1 : environment_options.f_help_header = "Usage: verify program name handling";
217 :
218 1 : advgetopt::getopt opt(environment_options);
219 :
220 1 : CATCH_REQUIRE(opt.get_project_name() == "unit-test");
221 1 : }
222 2 : CATCH_END_SECTION()
223 2 : }
224 :
225 :
226 1 : CATCH_TEST_CASE("invalid_program_name", "[program_name][invalid][getopt]")
227 : {
228 1 : CATCH_START_SECTION("invalid_program_name: parsing a nullptr program name throws")
229 : {
230 1 : advgetopt::options_environment environment_options;
231 1 : environment_options.f_project_name = "unittest";
232 1 : environment_options.f_help_header = "Usage: verify program name handling";
233 :
234 1 : advgetopt::getopt opt(environment_options);
235 :
236 3 : CATCH_REQUIRE_THROWS_MATCHES(
237 : opt.parse_program_name(nullptr)
238 : , advgetopt::getopt_logic_error
239 : , Catch::Matchers::ExceptionMessage(
240 : "getopt_logic_error: argv pointer cannot be nullptr"));
241 1 : }
242 1 : CATCH_END_SECTION()
243 1 : }
244 :
245 :
246 :
247 : // vim: ts=4 sw=4 et
|