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