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/flags.h>
28 : #include <advgetopt/exception.h>
29 :
30 :
31 : // snapdev lib
32 : //
33 : #include <snapdev/safe_setenv.h>
34 :
35 :
36 : // C++ lib
37 : //
38 : #include <fstream>
39 :
40 :
41 : // last include
42 : //
43 : #include <snapdev/poison.h>
44 :
45 :
46 :
47 :
48 :
49 :
50 :
51 7 : CATCH_TEST_CASE("option_string", "[getopt][string]")
52 : {
53 10 : CATCH_START_SECTION("empty string returns an empty empty")
54 : {
55 1 : CATCH_REQUIRE(advgetopt::getopt::escape_shell_argument(std::string()) == std::string("\"\""));
56 : }
57 : CATCH_END_SECTION()
58 :
59 10 : CATCH_START_SECTION("empty string returns empty")
60 : {
61 2 : std::string const g_simple_characters("-+0123456789ABCEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz_");
62 64 : for(std::size_t i(0); i < g_simple_characters.length(); ++i)
63 : {
64 126 : std::string t;
65 63 : t += g_simple_characters[i];
66 63 : CATCH_REQUIRE(advgetopt::getopt::escape_shell_argument(t) == t);
67 : }
68 : }
69 : CATCH_END_SECTION()
70 :
71 10 : CATCH_START_SECTION("string in single quotes")
72 : {
73 1 : CATCH_REQUIRE(advgetopt::getopt::escape_shell_argument("'between quotes'") == std::string("''\\''between quotes'\\'''"));
74 : }
75 : CATCH_END_SECTION()
76 :
77 10 : CATCH_START_SECTION("string with apostrophe")
78 : {
79 1 : CATCH_REQUIRE(advgetopt::getopt::escape_shell_argument("c'est un test") == std::string("'c'\\''est un test'"));
80 : }
81 : CATCH_END_SECTION()
82 :
83 10 : CATCH_START_SECTION("string with special characters")
84 : {
85 1 : CATCH_REQUIRE(advgetopt::getopt::escape_shell_argument("space colon: and semi-colon;") == std::string("'space colon: and semi-colon;'"));
86 : }
87 : CATCH_END_SECTION()
88 5 : }
89 :
90 :
91 3 : CATCH_TEST_CASE("options_to_string", "[arguments][valid][getopt]")
92 : {
93 2 : CATCH_START_SECTION("Transform command line options back to a shell compatible command.")
94 : {
95 : // create a getopt object with options
96 : //
97 1 : advgetopt::option const options[] =
98 : {
99 : advgetopt::define_option(
100 : advgetopt::Name("verbose")
101 : , advgetopt::ShortName('v')
102 : , advgetopt::Flags(advgetopt::standalone_command_flags())
103 : , advgetopt::EnvironmentVariableName("VERBOSE")
104 : , advgetopt::Help("print info as we work.")
105 : ),
106 : advgetopt::define_option(
107 : advgetopt::Name("weight")
108 : , advgetopt::ShortName('w')
109 : , advgetopt::Flags(advgetopt::command_flags<advgetopt::GETOPT_FLAG_REQUIRED>())
110 : , advgetopt::DefaultValue("455")
111 : , advgetopt::EnvironmentVariableName("WEIGHT")
112 : , advgetopt::Help("define the weight.")
113 : ),
114 : advgetopt::define_option(
115 : advgetopt::Name("--")
116 : , advgetopt::Flags(advgetopt::all_flags<advgetopt::GETOPT_FLAG_MULTIPLE>())
117 : , advgetopt::EnvironmentVariableName("FILES")
118 : , advgetopt::Help("list of filenames.")
119 : ),
120 : advgetopt::end_options()
121 : };
122 :
123 1 : advgetopt::options_environment environment_options;
124 1 : environment_options.f_project_name = "unittest";
125 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS;
126 1 : environment_options.f_help_header = "Usage: testing system arguments.";
127 1 : environment_options.f_options = options;
128 1 : environment_options.f_version = "3.1.5";
129 1 : environment_options.f_license = "MIT";
130 1 : environment_options.f_copyright = "Copyright (c) 2022 Made to Order Software Corp. -- All Rights Reserved";
131 1 : environment_options.f_build_date = "Jun 4 2019";
132 1 : environment_options.f_build_time = "23:02:36";
133 :
134 2 : CATCH_WHEN("Mix environment variable and command line options")
135 : {
136 2 : snapdev::safe_setenv env_size("WEIGHT", "303.183");
137 :
138 1 : char const * cargv[] =
139 : {
140 : "tests/system-arguments",
141 : "--verbose",
142 : "file1",
143 : "more2",
144 : "info3",
145 : nullptr
146 : };
147 1 : int const argc = sizeof(cargv) / sizeof(cargv[0]) - 1;
148 1 : char ** argv = const_cast<char **>(cargv);
149 :
150 : // the command line has priority, but the MULTIPLE creates a
151 : // problem here...
152 : //
153 2 : advgetopt::getopt::pointer_t opt(std::make_shared<advgetopt::getopt>(environment_options, argc, argv));
154 1 : CATCH_REQUIRE(opt != nullptr);
155 :
156 1 : CATCH_REQUIRE(opt->is_defined("verbose"));
157 :
158 1 : CATCH_REQUIRE(opt->is_defined("weight"));
159 1 : CATCH_REQUIRE(opt->get_string("weight") == "303.183");
160 :
161 1 : CATCH_REQUIRE(opt->is_defined("--"));
162 1 : CATCH_REQUIRE(opt->get_string("--", 0) == "file1");
163 1 : CATCH_REQUIRE(opt->get_string("--", 1) == "more2");
164 1 : CATCH_REQUIRE(opt->get_string("--", 2) == "info3");
165 : }
166 : }
167 : CATCH_END_SECTION()
168 7 : }
169 :
170 :
171 :
172 :
173 : // vim: ts=4 sw=4 et
|