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