Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / options / tests / filenameoption.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Tests file name option implementation.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_options
37  */
38 #include <vector>
39
40 #include <gtest/gtest.h>
41
42 #include "gromacs/options/filenameoption.h"
43 #include "gromacs/options/options.h"
44 #include "gromacs/options/optionsassigner.h"
45 #include "gromacs/utility/exceptions.h"
46 #include "gromacs/utility/file.h"
47
48 #include "testutils/testfilemanager.h"
49
50 namespace
51 {
52
53 using gmx::FileNameOption;
54 using gmx::test::TestFileManager;
55
56 TEST(FileNameOptionTest, AddsMissingExtension)
57 {
58     gmx::Options           options(NULL, NULL);
59     std::string            value;
60     ASSERT_NO_THROW(options.addOption(
61                         FileNameOption("f").store(&value)
62                             .filetype(gmx::eftTrajectory).outputFile()));
63
64     gmx::OptionsAssigner assigner(&options);
65     EXPECT_NO_THROW(assigner.start());
66     EXPECT_NO_THROW(assigner.startOption("f"));
67     EXPECT_NO_THROW(assigner.appendValue("testfile"));
68     EXPECT_NO_THROW(assigner.finishOption());
69     EXPECT_NO_THROW(assigner.finish());
70     EXPECT_NO_THROW(options.finish());
71
72     EXPECT_EQ("testfile.xtc", value);
73 }
74
75 TEST(FileNameOptionTest, HandlesRequiredDefaultValueWithoutExtension)
76 {
77     gmx::Options           options(NULL, NULL);
78     std::string            value;
79     ASSERT_NO_THROW(options.addOption(
80                         FileNameOption("f").store(&value).required()
81                             .filetype(gmx::eftGenericData).outputFile()
82                             .defaultBasename("testfile")));
83     EXPECT_EQ("testfile.dat", value);
84
85     gmx::OptionsAssigner assigner(&options);
86     EXPECT_NO_THROW(assigner.start());
87     EXPECT_NO_THROW(assigner.finish());
88     EXPECT_NO_THROW(options.finish());
89
90     EXPECT_EQ("testfile.dat", value);
91 }
92
93 TEST(FileNameOptionTest, HandlesOptionalDefaultValueWithoutExtension)
94 {
95     gmx::Options           options(NULL, NULL);
96     std::string            value;
97     ASSERT_NO_THROW(options.addOption(
98                         FileNameOption("f").store(&value)
99                             .filetype(gmx::eftIndex).outputFile()
100                             .defaultBasename("testfile")));
101     EXPECT_TRUE(value.empty());
102
103     gmx::OptionsAssigner assigner(&options);
104     EXPECT_NO_THROW(assigner.start());
105     EXPECT_NO_THROW(assigner.startOption("f"));
106     EXPECT_NO_THROW(assigner.finishOption());
107     EXPECT_NO_THROW(assigner.finish());
108     EXPECT_NO_THROW(options.finish());
109
110     EXPECT_EQ("testfile.ndx", value);
111 }
112
113 TEST(FileNameOptionTest, AddsMissingExtensionBasedOnExistingFile)
114 {
115     gmx::Options           options(NULL, NULL);
116     std::string            value;
117     ASSERT_NO_THROW(options.addOption(
118                         FileNameOption("f").store(&value)
119                             .filetype(gmx::eftTrajectory).inputFile()));
120     TestFileManager tempFiles;
121     std::string filename(tempFiles.getTemporaryFilePath(".trr"));
122     gmx::File::writeFileFromString(filename, "Dummy trajectory file");
123     std::string inputValue(filename.substr(0, filename.length() - 4));
124
125     gmx::OptionsAssigner assigner(&options);
126     EXPECT_NO_THROW(assigner.start());
127     EXPECT_NO_THROW(assigner.startOption("f"));
128     EXPECT_NO_THROW(assigner.appendValue(inputValue));
129     EXPECT_NO_THROW(assigner.finishOption());
130     EXPECT_NO_THROW(assigner.finish());
131     EXPECT_NO_THROW(options.finish());
132
133     EXPECT_EQ(filename, value);
134 }
135
136 } // namespace