Update instructions in containers.rst
[gromacs.git] / src / gromacs / options / tests / filenameoption.cpp
blob4196277d4c5103b9957cb032e35ed15e99ba756a
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2019,2020, 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.
36 /*! \internal \file
37 * \brief
38 * Tests basic file name option implementation.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_options
43 #include "gmxpre.h"
45 #include "gromacs/options/filenameoption.h"
47 #include <gtest/gtest.h>
49 #include "gromacs/fileio/filetypes.h"
50 #include "gromacs/options/options.h"
51 #include "gromacs/options/optionsassigner.h"
52 #include "gromacs/utility/exceptions.h"
54 #include "testutils/testasserts.h"
56 namespace
59 using gmx::FileNameOption;
61 TEST(FileNameOptionTest, HandlesRequiredDefaultValueWithoutExtension)
63 gmx::Options options;
64 std::string value;
65 ASSERT_NO_THROW_GMX(options.addOption(
66 FileNameOption("f").store(&value).required().filetype(gmx::eftGenericData).outputFile().defaultBasename("testfile")));
67 EXPECT_EQ("testfile.dat", value);
69 gmx::OptionsAssigner assigner(&options);
70 EXPECT_NO_THROW_GMX(assigner.start());
71 EXPECT_NO_THROW_GMX(assigner.finish());
72 EXPECT_NO_THROW_GMX(options.finish());
74 EXPECT_EQ("testfile.dat", value);
77 TEST(FileNameOptionTest, HandlesRequiredOptionWithoutValue)
79 gmx::Options options;
80 std::string value;
81 ASSERT_NO_THROW_GMX(options.addOption(
82 FileNameOption("f").store(&value).required().filetype(gmx::eftGenericData).outputFile().defaultBasename("testfile")));
83 EXPECT_EQ("testfile.dat", value);
85 gmx::OptionsAssigner assigner(&options);
86 EXPECT_NO_THROW_GMX(assigner.start());
87 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
88 EXPECT_NO_THROW_GMX(assigner.finishOption());
89 EXPECT_NO_THROW_GMX(assigner.finish());
90 EXPECT_NO_THROW_GMX(options.finish());
92 EXPECT_EQ("testfile.dat", value);
95 TEST(FileNameOptionTest, HandlesOptionalUnsetOption)
97 gmx::Options options;
98 std::string value;
99 ASSERT_NO_THROW_GMX(options.addOption(
100 FileNameOption("f").store(&value).filetype(gmx::eftTrajectory).outputFile().defaultBasename("testfile")));
101 EXPECT_TRUE(value.empty());
103 gmx::OptionsAssigner assigner(&options);
104 EXPECT_NO_THROW_GMX(assigner.start());
105 EXPECT_NO_THROW_GMX(assigner.finish());
106 EXPECT_NO_THROW_GMX(options.finish());
108 EXPECT_TRUE(value.empty());
111 TEST(FileNameOptionTest, HandlesOptionalDefaultValueWithoutExtension)
113 gmx::Options options;
114 std::string value;
115 ASSERT_NO_THROW_GMX(options.addOption(
116 FileNameOption("f").store(&value).filetype(gmx::eftIndex).outputFile().defaultBasename("testfile")));
117 EXPECT_TRUE(value.empty());
119 gmx::OptionsAssigner assigner(&options);
120 EXPECT_NO_THROW_GMX(assigner.start());
121 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
122 EXPECT_NO_THROW_GMX(assigner.finishOption());
123 EXPECT_NO_THROW_GMX(assigner.finish());
124 EXPECT_NO_THROW_GMX(options.finish());
126 EXPECT_EQ("testfile.ndx", value);
129 TEST(FileNameOptionTest, HandlesRequiredCustomDefaultExtension)
131 gmx::Options options;
132 std::string value;
133 ASSERT_NO_THROW_GMX(options.addOption(FileNameOption("f")
134 .store(&value)
135 .required()
136 .filetype(gmx::eftTrajectory)
137 .outputFile()
138 .defaultBasename("testfile")
139 .defaultType(efPDB)));
140 EXPECT_EQ("testfile.pdb", value);
142 gmx::OptionsAssigner assigner(&options);
143 EXPECT_NO_THROW_GMX(assigner.start());
144 EXPECT_NO_THROW_GMX(assigner.finish());
145 EXPECT_NO_THROW_GMX(options.finish());
147 EXPECT_EQ("testfile.pdb", value);
150 TEST(FileNameOptionTest, HandlesOptionalCustomDefaultExtension)
152 gmx::Options options;
153 std::string value;
154 ASSERT_NO_THROW_GMX(options.addOption(FileNameOption("f")
155 .store(&value)
156 .filetype(gmx::eftTrajectory)
157 .outputFile()
158 .defaultBasename("testfile")
159 .defaultType(efPDB)));
160 EXPECT_TRUE(value.empty());
162 gmx::OptionsAssigner assigner(&options);
163 EXPECT_NO_THROW_GMX(assigner.start());
164 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
165 EXPECT_NO_THROW_GMX(assigner.finishOption());
166 EXPECT_NO_THROW_GMX(assigner.finish());
167 EXPECT_NO_THROW_GMX(options.finish());
169 EXPECT_EQ("testfile.pdb", value);
172 TEST(FileNameOptionTest, GivesErrorOnUnknownFileSuffix)
174 gmx::Options options;
175 std::string value;
176 ASSERT_NO_THROW_GMX(
177 options.addOption(FileNameOption("f").store(&value).filetype(gmx::eftIndex).outputFile()));
178 EXPECT_TRUE(value.empty());
180 gmx::OptionsAssigner assigner(&options);
181 EXPECT_NO_THROW_GMX(assigner.start());
182 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
183 EXPECT_THROW_GMX(assigner.appendValue("testfile.foo"), gmx::InvalidInputError);
184 EXPECT_NO_THROW_GMX(assigner.finishOption());
185 EXPECT_NO_THROW_GMX(assigner.finish());
186 EXPECT_NO_THROW_GMX(options.finish());
188 EXPECT_TRUE(value.empty());
191 TEST(FileNameOptionTest, GivesErrorOnInvalidFileSuffix)
193 gmx::Options options;
194 std::string value;
195 ASSERT_NO_THROW_GMX(options.addOption(
196 FileNameOption("f").store(&value).filetype(gmx::eftTrajectory).outputFile()));
197 EXPECT_TRUE(value.empty());
199 gmx::OptionsAssigner assigner(&options);
200 EXPECT_NO_THROW_GMX(assigner.start());
201 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
202 EXPECT_THROW_GMX(assigner.appendValue("testfile.dat"), gmx::InvalidInputError);
203 EXPECT_NO_THROW_GMX(assigner.finishOption());
204 EXPECT_NO_THROW_GMX(assigner.finish());
205 EXPECT_NO_THROW_GMX(options.finish());
207 EXPECT_TRUE(value.empty());
210 TEST(FileNameOptionTest, HandlesRequiredCsvValueWithoutExtension)
212 gmx::Options options;
213 std::string value;
214 ASSERT_NO_THROW_GMX(options.addOption(
215 FileNameOption("f").store(&value).required().filetype(gmx::eftCsv).outputFile().defaultBasename("testfile")));
216 EXPECT_EQ("testfile.csv", value);
218 gmx::OptionsAssigner assigner(&options);
219 EXPECT_NO_THROW_GMX(assigner.start());
220 EXPECT_NO_THROW_GMX(assigner.finish());
221 EXPECT_NO_THROW_GMX(options.finish());
223 EXPECT_EQ("testfile.csv", value);
226 TEST(FileNameOptionTest, HandlesRequiredCsvOptionWithoutValue)
228 gmx::Options options;
229 std::string value;
230 ASSERT_NO_THROW_GMX(options.addOption(
231 FileNameOption("f").store(&value).required().filetype(gmx::eftCsv).outputFile().defaultBasename("testfile")));
232 EXPECT_EQ("testfile.csv", value);
234 gmx::OptionsAssigner assigner(&options);
235 EXPECT_NO_THROW_GMX(assigner.start());
236 EXPECT_NO_THROW_GMX(assigner.startOption("f"));
237 EXPECT_NO_THROW_GMX(assigner.finishOption());
238 EXPECT_NO_THROW_GMX(assigner.finish());
239 EXPECT_NO_THROW_GMX(options.finish());
241 EXPECT_EQ("testfile.csv", value);
244 } // namespace