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