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