2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2018, The GROMACS development team.
5 * Copyright (c) 2019, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Tests gmx::CommandLineParser.
40 * These tests exercise a large fraction of the code, so they may
41 * catch errors in other parts than just in command-line parsing.
43 * \author Teemu Murtola <teemu.murtola@gmail.com>
44 * \ingroup module_commandline
48 #include "gromacs/commandline/cmdlineparser.h"
52 #include <gtest/gtest.h>
54 #include "gromacs/options/basicoptions.h"
55 #include "gromacs/options/options.h"
57 #include "testutils/cmdlinetest.h"
58 #include "testutils/testasserts.h"
63 using gmx::test::CommandLine
;
65 class CommandLineParserTest
: public ::testing::Test
68 CommandLineParserTest();
70 gmx::Options options_
;
71 gmx::CommandLineParser parser_
;
73 std::vector
<int> ivalues_
;
74 std::vector
<double> dvalues_
;
77 std::string stringValue_
;
80 CommandLineParserTest::CommandLineParserTest() :
86 using gmx::BooleanOption
;
87 using gmx::DoubleOption
;
88 using gmx::IntegerOption
;
89 using gmx::StringOption
;
90 options_
.addOption(BooleanOption("flag").store(&flag_
));
91 options_
.addOption(IntegerOption("mvi").storeVector(&ivalues_
).multiValue());
92 options_
.addOption(DoubleOption("mvd").storeVector(&dvalues_
).allowMultiple());
93 options_
.addOption(IntegerOption("1p").store(&ivalue1p_
));
94 options_
.addOption(IntegerOption("12").store(&ivalue12_
));
95 options_
.addOption(StringOption("str").store(&stringValue_
));
98 TEST_F(CommandLineParserTest
, HandlesSingleValues
)
100 const char* const cmdline
[] = { "test", "-flag", "yes", "-mvi", "2", "-mvd", "2.7" };
101 CommandLine
args(cmdline
);
102 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
103 ASSERT_NO_THROW_GMX(options_
.finish());
104 EXPECT_EQ(7, args
.argc());
105 EXPECT_STREQ("test", args
.arg(0));
108 ASSERT_EQ(1U, ivalues_
.size());
109 EXPECT_EQ(2, ivalues_
[0]);
110 ASSERT_EQ(1U, dvalues_
.size());
111 EXPECT_DOUBLE_EQ(2.7, dvalues_
[0]);
114 TEST_F(CommandLineParserTest
, HandlesBooleanWithoutArgument
)
116 const char* const cmdline
[] = { "test", "-flag" };
117 CommandLine
args(cmdline
);
118 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
119 ASSERT_NO_THROW_GMX(options_
.finish());
120 EXPECT_EQ(2, args
.argc());
121 EXPECT_STREQ("test", args
.arg(0));
126 TEST_F(CommandLineParserTest
, HandlesBooleanAsNoWithoutArgument
)
128 const char* const cmdline
[] = { "test", "-noflag" };
129 CommandLine
args(cmdline
);
130 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
131 ASSERT_NO_THROW_GMX(options_
.finish());
132 EXPECT_EQ(2, args
.argc());
133 EXPECT_STREQ("test", args
.arg(0));
138 TEST_F(CommandLineParserTest
, ThrowsWithBooleanAsNoWithArgument
)
140 const char* const cmdline
[] = { "test", "-noflag", "no" };
141 CommandLine
args(cmdline
);
142 EXPECT_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()), gmx::InvalidInputError
);
145 TEST_F(CommandLineParserTest
, HandlesNegativeNumbers
)
147 const char* const cmdline
[] = { "test", "-mvi", "1", "-2", "-mvd", "-2.7" };
148 CommandLine
args(cmdline
);
149 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
150 ASSERT_NO_THROW_GMX(options_
.finish());
151 EXPECT_EQ(6, args
.argc());
152 EXPECT_STREQ("test", args
.arg(0));
154 ASSERT_EQ(2U, ivalues_
.size());
155 EXPECT_EQ(1, ivalues_
[0]);
156 EXPECT_EQ(-2, ivalues_
[1]);
157 ASSERT_EQ(1U, dvalues_
.size());
158 EXPECT_DOUBLE_EQ(-2.7, dvalues_
[0]);
161 TEST_F(CommandLineParserTest
, HandlesString
)
163 const char* const cmdline
[] = { "test", "-str", "text" };
164 CommandLine
args(cmdline
);
165 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
166 ASSERT_NO_THROW_GMX(options_
.finish());
167 EXPECT_EQ(3, args
.argc());
168 EXPECT_STREQ("test", args
.arg(0));
170 EXPECT_EQ("text", stringValue_
);
173 TEST_F(CommandLineParserTest
, RejectsStringWithMultipleValues
)
175 const char* const cmdline
[] = { "test", "-str", "text", "excess text" };
176 CommandLine
args(cmdline
);
177 EXPECT_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()), gmx::InvalidInputError
);
180 TEST_F(CommandLineParserTest
, HandlesDoubleDashOptionPrefix
)
182 const char* const cmdline
[] = { "test", "--mvi", "1", "-2", "--mvd", "-2.7" };
183 CommandLine
args(cmdline
);
184 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
185 ASSERT_NO_THROW_GMX(options_
.finish());
186 EXPECT_EQ(6, args
.argc());
187 EXPECT_STREQ("test", args
.arg(0));
189 ASSERT_EQ(2U, ivalues_
.size());
190 EXPECT_EQ(1, ivalues_
[0]);
191 EXPECT_EQ(-2, ivalues_
[1]);
192 ASSERT_EQ(1U, dvalues_
.size());
193 EXPECT_DOUBLE_EQ(-2.7, dvalues_
[0]);
196 TEST_F(CommandLineParserTest
, HandlesOptionsStartingWithNumbers
)
198 const char* const cmdline
[] = { "test", "--12", "1", "-1p", "-12" };
199 CommandLine
args(cmdline
);
200 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
201 ASSERT_NO_THROW_GMX(options_
.finish());
202 EXPECT_EQ(5, args
.argc());
203 EXPECT_STREQ("test", args
.arg(0));
205 EXPECT_EQ(1, ivalue12_
);
206 EXPECT_EQ(-12, ivalue1p_
);
209 TEST_F(CommandLineParserTest
, HandlesSkipUnknown
)
211 const char* const cmdline
[] = { "test", "-unknown1", "-flag", "-unknown2", "value",
212 "-mvi", "2", "-mvd", "2.7", "-unknown3" };
213 CommandLine
args(cmdline
);
214 parser_
.skipUnknown(true);
215 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
216 ASSERT_NO_THROW_GMX(options_
.finish());
218 ASSERT_EQ(5, args
.argc());
219 EXPECT_STREQ("test", args
.arg(0));
220 EXPECT_STREQ("-unknown1", args
.arg(1));
221 EXPECT_STREQ("-unknown2", args
.arg(2));
222 EXPECT_STREQ("value", args
.arg(3));
223 EXPECT_STREQ("-unknown3", args
.arg(4));
224 EXPECT_TRUE(args
.arg(5) == nullptr);
227 ASSERT_EQ(1U, ivalues_
.size());
228 EXPECT_EQ(2, ivalues_
[0]);
229 ASSERT_EQ(1U, dvalues_
.size());
230 EXPECT_DOUBLE_EQ(2.7, dvalues_
[0]);
233 TEST_F(CommandLineParserTest
, RejectsPositionalArgumentsByDefault
)
235 // Ensures that "gmx trjconv f" gets rejected.
236 const char* const cmdline
[] = { "test", "module", "positional" };
237 CommandLine
args(cmdline
);
238 EXPECT_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()), gmx::InvalidInputError
);
241 TEST_F(CommandLineParserTest
, CanAllowPositionalArguments
)
243 // Ensures that "gmx help trjconv" works
244 const char* const cmdline
[] = { "test", "module", "positional", "-flag" };
245 CommandLine
args(cmdline
);
246 parser_
.allowPositionalArguments(true);
247 ASSERT_NO_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()));
248 ASSERT_NO_THROW_GMX(options_
.finish());
249 ASSERT_EQ(4, args
.argc());
250 EXPECT_STREQ("test", args
.arg(0));
251 EXPECT_STREQ("module", args
.arg(1));
252 EXPECT_STREQ("positional", args
.arg(2));
255 TEST_F(CommandLineParserTest
, CannotHavePositionalArgumentsAfterOptions
)
257 // Even for the options that can't have arbitrary numbers of
258 // values, there's no way to check whether there's been enough
259 // values provided, so we can't have positional arguments after
261 const char* const cmdline
[] = { "test", "module", "-1p", "2", "positional" };
262 CommandLine
args(cmdline
);
263 parser_
.allowPositionalArguments(true);
264 EXPECT_THROW_GMX(parser_
.parse(&args
.argc(), args
.argv()), gmx::InvalidInputError
);