Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / commandline / tests / cmdlinehelpwriter.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, 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.
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 gmx::CommandLineHelpWriter.
38  *
39  * These tests fail for any change in the output, and it should be reviewed
40  * whether the change was intentional.
41  * For development, the tests can be run with a '-stdout' command-line option
42  * to print out the help to stdout instead of using the XML reference
43  * framework.
44  *
45  * \author Teemu Murtola <teemu.murtola@gmail.com>
46  * \ingroup module_commandline
47  */
48 #include <gtest/gtest.h>
49
50 #include "gromacs/commandline/cmdlinehelpcontext.h"
51 #include "gromacs/commandline/cmdlinehelpwriter.h"
52 #include "gromacs/math/vectypes.h"
53 #include "gromacs/options/basicoptions.h"
54 #include "gromacs/options/filenameoption.h"
55 #include "gromacs/options/options.h"
56 #include "gromacs/utility/file.h"
57
58 #include "testutils/stringtest.h"
59 #include "testutils/testfilemanager.h"
60
61 namespace
62 {
63
64 class CommandLineHelpWriterTest : public ::gmx::test::StringTestBase
65 {
66     public:
67         CommandLineHelpWriterTest() : bHidden_(false) {}
68
69         void checkHelp(gmx::CommandLineHelpWriter *writer);
70
71         gmx::test::TestFileManager tempFiles_;
72         bool                       bHidden_;
73 };
74
75 void CommandLineHelpWriterTest::checkHelp(gmx::CommandLineHelpWriter *writer)
76 {
77     std::string                 filename = tempFiles_.getTemporaryFilePath("helptext.txt");
78     gmx::File                   file(filename, "w");
79     gmx::CommandLineHelpContext context(&file, gmx::eHelpOutputFormat_Console, NULL);
80     context.setShowHidden(bHidden_);
81     writer->writeHelp(context);
82     file.close();
83
84     checkFileContents(filename, "HelpText");
85 }
86
87
88 /********************************************************************
89  * Tests start here
90  */
91
92 /*
93  * Tests help printing for each option type, but doesn't contain much
94  * variablity in the options.
95  */
96 TEST_F(CommandLineHelpWriterTest, HandlesOptionTypes)
97 {
98     using namespace gmx;
99
100     Options options("test", "Short Description");
101     options.addOption(BooleanOption("bool").description("Boolean option")
102                           .defaultValue(true));
103     options.addOption(BooleanOption("hidden").description("Hidden option")
104                           .hidden().defaultValue(true));
105     options.addOption(IntegerOption("int").description("Integer option")
106                           .defaultValue(2));
107     ivec intvec = {1, 2, 3};
108     options.addOption(IntegerOption("ivec").description("Integer vector option")
109                           .vector().store(intvec));
110     options.addOption(DoubleOption("double").description("Double option")
111                           .defaultValue(2.5));
112     dvec dblvec = {1.1, 2.3, 3.2};
113     options.addOption(DoubleOption("dvec").description("Double vector option")
114                           .vector().store(dblvec));
115     options.addOption(DoubleOption("time").description("Time option (%t)")
116                           .timeValue().defaultValue(10.0));
117     options.addOption(StringOption("string").description("String option")
118                           .defaultValue("test"));
119     const char * const enumValues[] = { "no", "opt1", "opt2" };
120     options.addOption(StringOption("enum").description("Enum option")
121                           .enumValue(enumValues).defaultEnumIndex(0));
122
123     std::string filename;
124     options.addOption(FileNameOption("f")
125                           .description("Input file description")
126                           .filetype(eftTrajectory).inputFile().required()
127                           .defaultBasename("traj"));
128     options.addOption(FileNameOption("mult")
129                           .description("Multiple file description")
130                           .filetype(eftTrajectory).inputFile().multiValue()
131                           .defaultBasename("traj"));
132     options.addOption(FileNameOption("lib")
133                           .description("Library file description")
134                           .filetype(eftGenericData).inputFile().libraryFile()
135                           .defaultBasename("libdata"));
136     options.addOption(FileNameOption("io")
137                           .store(&filename)
138                           .description("Input/Output file description")
139                           .filetype(eftGenericData).inputOutputFile()
140                           .defaultBasename("inout"));
141     options.addOption(FileNameOption("o")
142                           .description("Output file description")
143                           .filetype(eftPlot).outputFile());
144
145     CommandLineHelpWriter writer(options);
146     bHidden_ = true;
147     checkHelp(&writer);
148 }
149
150 /*
151  * Tests help printing with file name options with various values that don't
152  * fit into the allocated columns.
153  */
154 TEST_F(CommandLineHelpWriterTest, HandlesLongFileOptions)
155 {
156     using gmx::FileNameOption;
157     using gmx::eftGenericData;
158     using gmx::eftTrajectory;
159
160     gmx::Options options(NULL, NULL);
161     options.addOption(FileNameOption("f")
162                           .description("File name option with a long value")
163                           .filetype(eftTrajectory).inputFile().required()
164                           .defaultBasename("path/to/long/trajectory/name"));
165     options.addOption(FileNameOption("f2")
166                           .description("File name option with a long value")
167                           .filetype(eftTrajectory).inputFile().required()
168                           .defaultBasename("path/to/long/trajectory"));
169     options.addOption(FileNameOption("lib")
170                           .description("File name option with a long value and type")
171                           .filetype(eftTrajectory).inputFile().libraryFile()
172                           .defaultBasename("path/to/long/trajectory/name"));
173     options.addOption(FileNameOption("longfileopt")
174                           .description("File name option with a long name")
175                           .filetype(eftGenericData).inputFile()
176                           .defaultBasename("deffile"));
177     options.addOption(FileNameOption("longfileopt2")
178                           .description("File name option with multiple long fields")
179                           .filetype(eftGenericData).inputFile().libraryFile()
180                           .defaultBasename("path/to/long/file/name"));
181
182     gmx::CommandLineHelpWriter writer(options);
183     checkHelp(&writer);
184 }
185
186 /*
187  * Tests help printing with general options with various values that don't
188  * fit into the allocated columns.
189  */
190 TEST_F(CommandLineHelpWriterTest, HandlesLongOptions)
191 {
192     using gmx::BooleanOption;
193     using gmx::DoubleOption;
194     using gmx::StringOption;
195
196     gmx::Options options(NULL, NULL);
197     options.addOption(BooleanOption("longboolean")
198                           .description("Boolean option with a long name")
199                           .defaultValue(true));
200     dvec dblvec = {1.135, 2.32, 3.2132};
201     options.addOption(DoubleOption("dvec").description("Double vector option")
202                           .vector().store(dblvec));
203     std::vector<std::string> values;
204     values.push_back("A very long string value that overflows even the description column");
205     values.push_back("Another very long string value that overflows even the description column");
206     options.addOption(StringOption("string")
207                           .description("String option with very long values (may "
208                                        "be less relevant with selections having "
209                                        "their own option type)")
210                           .storeVector(&values));
211
212     gmx::CommandLineHelpWriter writer(options);
213     checkHelp(&writer);
214 }
215
216 /* TODO: Add corresponding tests to either the selection module, or as part of
217  * trajectoryanalysis tests.
218  * Tests help printing with selection options with values.
219  */
220 #if 0
221 TEST_F(CommandLineHelpWriterTest, HandlesSelectionOptions)
222 {
223     using gmx::SelectionFileOption;
224     using gmx::SelectionOption;
225
226     gmx::Options                options(NULL, NULL);
227     gmx::SelectionCollection    selections;
228     gmx::SelectionOptionManager manager(&selections);
229     options.addManager(&manager);
230     options.addOption(SelectionFileOption("sf"));
231     options.addOption(SelectionOption("refsel").required()
232                           .description("Reference selection option"));
233     options.addOption(SelectionOption("sel").required().valueCount(2)
234                           .description("Selection option"));
235     options.finish();
236     manager.parseRequestedFromString(
237             "resname SOL;"
238             "surface = within 0.5 of resname SOL;"
239             "group \"Protein\" and surface;"
240             "group \"Protein\" and not surface;");
241
242     gmx::CommandLineHelpWriter writer(options);
243     checkHelp(&writer);
244 }
245 #endif
246
247 /*
248  * Tests help printing for multiple sections.
249  */
250 TEST_F(CommandLineHelpWriterTest, HandlesMultipleSections)
251 {
252     using namespace gmx;
253
254     Options options("test", "Main Title");
255     Options subSect1("subsect1", "Subsection 1 Title");
256     Options subSect2("subsect2", "Subsection 2 Title");
257     Options subSect3("subsect3", NULL);
258     options.addSubSection(&subSect1);
259     options.addSubSection(&subSect2);
260     options.addSubSection(&subSect3);
261     options.setDescription("Description for main section.");
262     subSect1.setDescription("Description for subsection 1.");
263     subSect2.setDescription("Description for subsection 2.");
264     subSect3.setDescription("Description for subsection 3.");
265     options.addOption(IntegerOption("main")
266                           .description("Option in main section"));
267     subSect1.addOption(IntegerOption("sub1")
268                            .description("Option in subsection 1"));
269     subSect2.addOption(IntegerOption("sub2")
270                            .description("Option in subsection 2"));
271
272     CommandLineHelpWriter writer(options);
273     writer.setShowDescriptions(true);
274     checkHelp(&writer);
275 }
276
277 } // namespace