Line data Source code
1 : // Copyright (c) 2006-2022 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 : // self
21 : //
22 : #include "catch_main.h"
23 :
24 :
25 : // advgetopt lib
26 : //
27 : #include <advgetopt/exception.h>
28 :
29 :
30 : // snapdev lib
31 : //
32 : #include <snapdev/safe_setenv.h>
33 :
34 :
35 : // C++ lib
36 : //
37 : #include <fstream>
38 :
39 :
40 : // last include
41 : //
42 : #include <snapdev/poison.h>
43 :
44 :
45 :
46 :
47 :
48 :
49 8 : CATCH_TEST_CASE("program_name", "[program_name][valid][getopt]")
50 : {
51 12 : CATCH_START_SECTION("Verify a nullptr program name in argv[]s")
52 1 : advgetopt::options_environment environment_options;
53 1 : environment_options.f_project_name = "unittest";
54 1 : environment_options.f_help_header = "Usage: verify program name handling";
55 :
56 1 : char const * cargv[] =
57 : {
58 : nullptr,
59 : "--verbose",
60 : nullptr
61 : };
62 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
63 1 : char ** argv = const_cast<char **>(cargv);
64 :
65 2 : advgetopt::getopt opt(environment_options);
66 :
67 1 : opt.parse_program_name(argv);
68 :
69 1 : CATCH_REQUIRE(opt.get_program_name().empty());
70 1 : CATCH_REQUIRE(opt.get_program_fullname().empty());
71 : CATCH_END_SECTION()
72 :
73 12 : CATCH_START_SECTION("Verify a program name with no path")
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 2 : 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 : CATCH_END_SECTION()
95 :
96 12 : CATCH_START_SECTION("Verify a program name with a relative path")
97 1 : advgetopt::options_environment environment_options;
98 1 : environment_options.f_project_name = "unittest";
99 1 : environment_options.f_options = nullptr;
100 1 : environment_options.f_help_header = "Usage: verify program name handling";
101 :
102 1 : char const * cargv[] =
103 : {
104 : "project/bin/and-basename.tool",
105 : "--verbose",
106 : nullptr
107 : };
108 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
109 1 : char ** argv = const_cast<char **>(cargv);
110 :
111 2 : advgetopt::getopt opt(environment_options);
112 :
113 1 : opt.parse_program_name(argv);
114 :
115 1 : CATCH_REQUIRE(opt.get_program_name() == "and-basename.tool");
116 1 : CATCH_REQUIRE(opt.get_program_fullname() == "project/bin/and-basename.tool");
117 : CATCH_END_SECTION()
118 :
119 12 : CATCH_START_SECTION("Verify a program name with a relative path and backslashes")
120 1 : advgetopt::options_environment environment_options;
121 1 : environment_options.f_project_name = "unittest";
122 1 : environment_options.f_options = nullptr;
123 1 : environment_options.f_help_header = "Usage: verify program name handling";
124 :
125 1 : char const * cargv[] =
126 : {
127 : "project\\bin\\and-basename.tool",
128 : "--verbose",
129 : nullptr
130 : };
131 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
132 1 : char ** argv = const_cast<char **>(cargv);
133 :
134 2 : advgetopt::getopt opt(environment_options);
135 :
136 1 : opt.parse_program_name(argv);
137 :
138 1 : CATCH_REQUIRE(opt.get_program_name() == "and-basename.tool");
139 1 : CATCH_REQUIRE(opt.get_program_fullname() == "project\\bin\\and-basename.tool");
140 : CATCH_END_SECTION()
141 :
142 12 : CATCH_START_SECTION("Verify a program name with a full path")
143 1 : advgetopt::options_environment environment_options;
144 1 : environment_options.f_project_name = "unittest";
145 1 : environment_options.f_options = nullptr;
146 1 : environment_options.f_help_header = "Usage: verify program name handling";
147 :
148 1 : char const * cargv[] =
149 : {
150 : "/usr/bin/basename",
151 : "--verbose",
152 : nullptr
153 : };
154 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
155 1 : char ** argv = const_cast<char **>(cargv);
156 :
157 2 : advgetopt::getopt opt(environment_options);
158 :
159 1 : opt.parse_program_name(argv);
160 :
161 1 : CATCH_REQUIRE(opt.get_program_name() == "basename");
162 1 : CATCH_REQUIRE(opt.get_program_fullname() == "/usr/bin/basename");
163 : CATCH_END_SECTION()
164 :
165 12 : CATCH_START_SECTION("Verify a program name with a full path and backslashes")
166 1 : advgetopt::options_environment environment_options;
167 1 : environment_options.f_project_name = "unittest";
168 1 : environment_options.f_options = nullptr;
169 1 : environment_options.f_help_header = "Usage: verify program name handling";
170 :
171 1 : char const * cargv[] =
172 : {
173 : "\\usr\\bin\\basename",
174 : "--verbose",
175 : nullptr
176 : };
177 : //int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
178 1 : char ** argv = const_cast<char **>(cargv);
179 :
180 2 : advgetopt::getopt opt(environment_options);
181 :
182 1 : opt.parse_program_name(argv);
183 :
184 1 : CATCH_REQUIRE(opt.get_program_name() == "basename");
185 1 : CATCH_REQUIRE(opt.get_program_fullname() == "\\usr\\bin\\basename");
186 : CATCH_END_SECTION()
187 6 : }
188 :
189 :
190 :
191 4 : CATCH_TEST_CASE("project_name", "[project_name][valid][getopt]")
192 : {
193 4 : CATCH_START_SECTION("Verify a nullptr project name")
194 1 : advgetopt::options_environment environment_options;
195 1 : environment_options.f_help_header = "Usage: verify project name handling";
196 :
197 2 : advgetopt::getopt opt(environment_options);
198 :
199 1 : CATCH_REQUIRE(opt.get_project_name().empty());
200 : CATCH_END_SECTION()
201 :
202 4 : CATCH_START_SECTION("Verify an actual project name")
203 1 : advgetopt::options_environment environment_options;
204 1 : environment_options.f_project_name = "unit-test";
205 1 : environment_options.f_help_header = "Usage: verify program name handling";
206 :
207 2 : advgetopt::getopt opt(environment_options);
208 :
209 1 : CATCH_REQUIRE(opt.get_project_name() == "unit-test");
210 : CATCH_END_SECTION()
211 2 : }
212 :
213 :
214 3 : CATCH_TEST_CASE("invalid_program_name", "[program_name][invalid][getopt]")
215 : {
216 2 : CATCH_START_SECTION("Parsing a nullptr program name throws")
217 1 : advgetopt::options_environment environment_options;
218 1 : environment_options.f_project_name = "unittest";
219 1 : environment_options.f_help_header = "Usage: verify program name handling";
220 :
221 2 : advgetopt::getopt opt(environment_options);
222 :
223 1 : CATCH_REQUIRE_THROWS_MATCHES(
224 : opt.parse_program_name(nullptr)
225 : , advgetopt::getopt_logic_error
226 : , Catch::Matchers::ExceptionMessage(
227 : "getopt_logic_error: argv pointer cannot be nullptr"));
228 : CATCH_END_SECTION()
229 7 : }
230 :
231 :
232 :
233 :
234 : // vim: ts=4 sw=4 et
|