3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
33 * Tests file name option implementation.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_options
40 #include <gtest/gtest.h>
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"
48 #include "testutils/testfilemanager.h"
53 using gmx::FileNameOption;
54 using gmx::test::TestFileManager;
56 TEST(FileNameOptionTest, AddsMissingExtension)
58 gmx::Options options(NULL, NULL);
60 ASSERT_NO_THROW(options.addOption(
61 FileNameOption("f").store(&value)
62 .filetype(gmx::eftTrajectory).outputFile()));
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());
72 EXPECT_EQ("testfile.xtc", value);
75 TEST(FileNameOptionTest, HandlesRequiredDefaultValueWithoutExtension)
77 gmx::Options options(NULL, NULL);
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);
85 gmx::OptionsAssigner assigner(&options);
86 EXPECT_NO_THROW(assigner.start());
87 EXPECT_NO_THROW(assigner.finish());
88 EXPECT_NO_THROW(options.finish());
90 EXPECT_EQ("testfile.dat", value);
93 TEST(FileNameOptionTest, HandlesOptionalDefaultValueWithoutExtension)
95 gmx::Options options(NULL, NULL);
97 ASSERT_NO_THROW(options.addOption(
98 FileNameOption("f").store(&value)
99 .filetype(gmx::eftIndex).outputFile()
100 .defaultBasename("testfile")));
101 EXPECT_TRUE(value.empty());
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());
110 EXPECT_EQ("testfile.ndx", value);
113 TEST(FileNameOptionTest, AddsMissingExtensionBasedOnExistingFile)
115 gmx::Options options(NULL, NULL);
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));
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());
133 EXPECT_EQ(filename, value);