Introduce SimulatorBuilder
[gromacs.git] / src / gromacs / commandline / tests / cmdlineparser.cpp
blob643379f9ff23c0f50fb067ee1bb2d01a2f67ca20
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2016,2017,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \internal \file
36 * \brief
37 * Tests gmx::CommandLineParser.
39 * These tests exercise a large fraction of the code, so they may
40 * catch errors in other parts than just in command-line parsing.
42 * \author Teemu Murtola <teemu.murtola@gmail.com>
43 * \ingroup module_commandline
45 #include "gmxpre.h"
47 #include "gromacs/commandline/cmdlineparser.h"
49 #include <vector>
51 #include <gtest/gtest.h>
53 #include "gromacs/options/basicoptions.h"
54 #include "gromacs/options/options.h"
56 #include "testutils/cmdlinetest.h"
57 #include "testutils/testasserts.h"
59 namespace
62 using gmx::test::CommandLine;
64 class CommandLineParserTest : public ::testing::Test
66 public:
67 CommandLineParserTest();
69 gmx::Options options_;
70 gmx::CommandLineParser parser_;
71 bool flag_;
72 std::vector<int> ivalues_;
73 std::vector<double> dvalues_;
74 int ivalue1p_;
75 int ivalue12_;
76 std::string stringValue_;
79 CommandLineParserTest::CommandLineParserTest()
80 : parser_(&options_), flag_(false), ivalue1p_(0), ivalue12_(0)
82 using gmx::BooleanOption;
83 using gmx::IntegerOption;
84 using gmx::DoubleOption;
85 using gmx::StringOption;
86 options_.addOption(BooleanOption("flag").store(&flag_));
87 options_.addOption(IntegerOption("mvi").storeVector(&ivalues_).multiValue());
88 options_.addOption(DoubleOption("mvd").storeVector(&dvalues_).allowMultiple());
89 options_.addOption(IntegerOption("1p").store(&ivalue1p_));
90 options_.addOption(IntegerOption("12").store(&ivalue12_));
91 options_.addOption(StringOption("str").store(&stringValue_));
94 TEST_F(CommandLineParserTest, HandlesSingleValues)
96 const char *const cmdline[] = {
97 "test", "-flag", "yes", "-mvi", "2", "-mvd", "2.7"
99 CommandLine args(cmdline);
100 ASSERT_NO_THROW_GMX(parser_.parse(&args.argc(), args.argv()));
101 ASSERT_NO_THROW_GMX(options_.finish());
102 EXPECT_EQ(7, args.argc());
103 EXPECT_STREQ("test", args.arg(0));
105 EXPECT_TRUE(flag_);
106 ASSERT_EQ(1U, ivalues_.size());
107 EXPECT_EQ(2, ivalues_[0]);
108 ASSERT_EQ(1U, dvalues_.size());
109 EXPECT_DOUBLE_EQ(2.7, dvalues_[0]);
112 TEST_F(CommandLineParserTest, HandlesBooleanWithoutArgument)
114 const char *const cmdline[] = {
115 "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));
123 EXPECT_TRUE(flag_);
126 TEST_F(CommandLineParserTest, HandlesBooleanAsNoWithoutArgument)
128 const char *const cmdline[] = {
129 "test", "-noflag"
131 CommandLine args(cmdline);
132 ASSERT_NO_THROW_GMX(parser_.parse(&args.argc(), args.argv()));
133 ASSERT_NO_THROW_GMX(options_.finish());
134 EXPECT_EQ(2, args.argc());
135 EXPECT_STREQ("test", args.arg(0));
137 EXPECT_FALSE(flag_);
140 TEST_F(CommandLineParserTest, ThrowsWithBooleanAsNoWithArgument)
142 const char *const cmdline[] = {
143 "test", "-noflag", "no"
145 CommandLine args(cmdline);
146 EXPECT_THROW_GMX(parser_.parse(&args.argc(), args.argv()), gmx::InvalidInputError);
149 TEST_F(CommandLineParserTest, HandlesNegativeNumbers)
151 const char *const cmdline[] = {
152 "test", "-mvi", "1", "-2", "-mvd", "-2.7"
154 CommandLine args(cmdline);
155 ASSERT_NO_THROW_GMX(parser_.parse(&args.argc(), args.argv()));
156 ASSERT_NO_THROW_GMX(options_.finish());
157 EXPECT_EQ(6, args.argc());
158 EXPECT_STREQ("test", args.arg(0));
160 ASSERT_EQ(2U, ivalues_.size());
161 EXPECT_EQ(1, ivalues_[0]);
162 EXPECT_EQ(-2, ivalues_[1]);
163 ASSERT_EQ(1U, dvalues_.size());
164 EXPECT_DOUBLE_EQ(-2.7, dvalues_[0]);
167 TEST_F(CommandLineParserTest, HandlesString)
169 const char *const cmdline[] = {
170 "test", "-str", "text"
172 CommandLine args(cmdline);
173 ASSERT_NO_THROW_GMX(parser_.parse(&args.argc(), args.argv()));
174 ASSERT_NO_THROW_GMX(options_.finish());
175 EXPECT_EQ(3, args.argc());
176 EXPECT_STREQ("test", args.arg(0));
178 EXPECT_EQ("text", stringValue_);
181 TEST_F(CommandLineParserTest, RejectsStringWithMultipleValues)
183 const char *const cmdline[] = {
184 "test", "-str", "text", "excess text"
186 CommandLine args(cmdline);
187 EXPECT_THROW_GMX(parser_.parse(&args.argc(), args.argv()), gmx::InvalidInputError);
190 TEST_F(CommandLineParserTest, HandlesDoubleDashOptionPrefix)
192 const char *const cmdline[] = {
193 "test", "--mvi", "1", "-2", "--mvd", "-2.7"
195 CommandLine args(cmdline);
196 ASSERT_NO_THROW_GMX(parser_.parse(&args.argc(), args.argv()));
197 ASSERT_NO_THROW_GMX(options_.finish());
198 EXPECT_EQ(6, args.argc());
199 EXPECT_STREQ("test", args.arg(0));
201 ASSERT_EQ(2U, ivalues_.size());
202 EXPECT_EQ(1, ivalues_[0]);
203 EXPECT_EQ(-2, ivalues_[1]);
204 ASSERT_EQ(1U, dvalues_.size());
205 EXPECT_DOUBLE_EQ(-2.7, dvalues_[0]);
208 TEST_F(CommandLineParserTest, HandlesOptionsStartingWithNumbers)
210 const char *const cmdline[] = {
211 "test", "--12", "1", "-1p", "-12"
213 CommandLine args(cmdline);
214 ASSERT_NO_THROW_GMX(parser_.parse(&args.argc(), args.argv()));
215 ASSERT_NO_THROW_GMX(options_.finish());
216 EXPECT_EQ(5, args.argc());
217 EXPECT_STREQ("test", args.arg(0));
219 EXPECT_EQ(1, ivalue12_);
220 EXPECT_EQ(-12, ivalue1p_);
223 TEST_F(CommandLineParserTest, HandlesSkipUnknown)
225 const char *const cmdline[] = {
226 "test", "-unknown1", "-flag", "-unknown2", "value", "-mvi", "2", "-mvd", "2.7", "-unknown3"
228 CommandLine args(cmdline);
229 parser_.skipUnknown(true);
230 ASSERT_NO_THROW_GMX(parser_.parse(&args.argc(), args.argv()));
231 ASSERT_NO_THROW_GMX(options_.finish());
233 ASSERT_EQ(5, args.argc());
234 EXPECT_STREQ("test", args.arg(0));
235 EXPECT_STREQ("-unknown1", args.arg(1));
236 EXPECT_STREQ("-unknown2", args.arg(2));
237 EXPECT_STREQ("value", args.arg(3));
238 EXPECT_STREQ("-unknown3", args.arg(4));
239 EXPECT_TRUE(args.arg(5) == nullptr);
241 EXPECT_TRUE(flag_);
242 ASSERT_EQ(1U, ivalues_.size());
243 EXPECT_EQ(2, ivalues_[0]);
244 ASSERT_EQ(1U, dvalues_.size());
245 EXPECT_DOUBLE_EQ(2.7, dvalues_[0]);
248 TEST_F(CommandLineParserTest, RejectsPositionalArgumentsByDefault)
250 // Ensures that "gmx trjconv f" gets rejected.
251 const char *const cmdline[] = {
252 "test", "module", "positional"
254 CommandLine args(cmdline);
255 EXPECT_THROW_GMX(parser_.parse(&args.argc(), args.argv()), gmx::InvalidInputError);
258 TEST_F(CommandLineParserTest, CanAllowPositionalArguments)
260 // Ensures that "gmx help trjconv" works
261 const char *const cmdline[] = {
262 "test", "module", "positional", "-flag"
264 CommandLine args(cmdline);
265 parser_.allowPositionalArguments(true);
266 ASSERT_NO_THROW_GMX(parser_.parse(&args.argc(), args.argv()));
267 ASSERT_NO_THROW_GMX(options_.finish());
268 ASSERT_EQ(4, args.argc());
269 EXPECT_STREQ("test", args.arg(0));
270 EXPECT_STREQ("module", args.arg(1));
271 EXPECT_STREQ("positional", args.arg(2));
274 TEST_F(CommandLineParserTest, CannotHavePositionalArgumentsAfterOptions)
276 // Even for the options that can't have arbitrary numbers of
277 // values, there's no way to check whether there's been enough
278 // values provided, so we can't have positional arguments after
279 // any options.
280 const char *const cmdline[] = {
281 "test", "module", "-1p", "2", "positional"
283 CommandLine args(cmdline);
284 parser_.allowPositionalArguments(true);
285 EXPECT_THROW_GMX(parser_.parse(&args.argc(), args.argv()), gmx::InvalidInputError);
288 } // namespace