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