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