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