Merge branch 'release-4-6' into master
[alexxy/gromacs.git] / src / gromacs / options / tests / filenameoption.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * directory and at http://www.gromacs.org.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests file name option implementation.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #include <vector>
43
44 #include <gtest/gtest.h>
45
46 #include "gromacs/options/filenameoption.h"
47 #include "gromacs/options/options.h"
48 #include "gromacs/options/optionsassigner.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/file.h"
51
52 #include "testutils/testasserts.h"
53 #include "testutils/testfilemanager.h"
54
55 namespace
56 {
57
58 using gmx::FileNameOption;
59 using gmx::test::TestFileManager;
60
61 TEST(FileNameOptionTest, AddsMissingExtension)
62 {
63     gmx::Options           options(NULL, NULL);
64     std::string            value;
65     ASSERT_NO_THROW_GMX(options.addOption(
66                                 FileNameOption("f").store(&value)
67                                     .filetype(gmx::eftTrajectory).outputFile()));
68
69     gmx::OptionsAssigner assigner(&options);
70     EXPECT_NO_THROW_GMX(assigner.start());
71     EXPECT_NO_THROW_GMX(assigner.startOption("f"));
72     EXPECT_NO_THROW_GMX(assigner.appendValue("testfile"));
73     EXPECT_NO_THROW_GMX(assigner.finishOption());
74     EXPECT_NO_THROW_GMX(assigner.finish());
75     EXPECT_NO_THROW_GMX(options.finish());
76
77     EXPECT_EQ("testfile.xtc", value);
78 }
79
80 TEST(FileNameOptionTest, HandlesRequiredDefaultValueWithoutExtension)
81 {
82     gmx::Options           options(NULL, NULL);
83     std::string            value;
84     ASSERT_NO_THROW_GMX(options.addOption(
85                                 FileNameOption("f").store(&value).required()
86                                     .filetype(gmx::eftGenericData).outputFile()
87                                     .defaultBasename("testfile")));
88     EXPECT_EQ("testfile.dat", value);
89
90     gmx::OptionsAssigner assigner(&options);
91     EXPECT_NO_THROW_GMX(assigner.start());
92     EXPECT_NO_THROW_GMX(assigner.finish());
93     EXPECT_NO_THROW_GMX(options.finish());
94
95     EXPECT_EQ("testfile.dat", value);
96 }
97
98 TEST(FileNameOptionTest, HandlesOptionalDefaultValueWithoutExtension)
99 {
100     gmx::Options           options(NULL, NULL);
101     std::string            value;
102     ASSERT_NO_THROW_GMX(options.addOption(
103                                 FileNameOption("f").store(&value)
104                                     .filetype(gmx::eftIndex).outputFile()
105                                     .defaultBasename("testfile")));
106     EXPECT_TRUE(value.empty());
107
108     gmx::OptionsAssigner assigner(&options);
109     EXPECT_NO_THROW_GMX(assigner.start());
110     EXPECT_NO_THROW_GMX(assigner.startOption("f"));
111     EXPECT_NO_THROW_GMX(assigner.finishOption());
112     EXPECT_NO_THROW_GMX(assigner.finish());
113     EXPECT_NO_THROW_GMX(options.finish());
114
115     EXPECT_EQ("testfile.ndx", value);
116 }
117
118 TEST(FileNameOptionTest, AddsMissingExtensionBasedOnExistingFile)
119 {
120     gmx::Options           options(NULL, NULL);
121     std::string            value;
122     ASSERT_NO_THROW_GMX(options.addOption(
123                                 FileNameOption("f").store(&value)
124                                     .filetype(gmx::eftTrajectory).inputFile()));
125     TestFileManager      tempFiles;
126     std::string          filename(tempFiles.getTemporaryFilePath(".trr"));
127     gmx::File::writeFileFromString(filename, "Dummy trajectory file");
128     std::string          inputValue(filename.substr(0, filename.length() - 4));
129
130     gmx::OptionsAssigner assigner(&options);
131     EXPECT_NO_THROW_GMX(assigner.start());
132     EXPECT_NO_THROW_GMX(assigner.startOption("f"));
133     EXPECT_NO_THROW_GMX(assigner.appendValue(inputValue));
134     EXPECT_NO_THROW_GMX(assigner.finishOption());
135     EXPECT_NO_THROW_GMX(assigner.finish());
136     EXPECT_NO_THROW_GMX(options.finish());
137
138     EXPECT_EQ(filename, value);
139 }
140
141 } // namespace