3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
33 * Tests gmx::CommandLineHelpWriter.
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
41 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
42 * \ingroup module_commandline
44 #include <gtest/gtest.h>
46 #include "gromacs/legacyheaders/types/simple.h"
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"
59 #include "testutils/datapath.h"
60 #include "testutils/stringtest.h"
65 class CommandLineHelpWriterTest : public ::gmx::test::StringTestBase
68 void checkHelp(gmx::CommandLineHelpWriter *writer);
70 gmx::test::TestFileManager tempFiles_;
73 void CommandLineHelpWriterTest::checkHelp(gmx::CommandLineHelpWriter *writer)
75 std::string filename = tempFiles_.getTemporaryFilePath("helptext.txt");
76 gmx::File file(filename, "w");
77 writer->writeHelp(&file);
80 checkFileContents(filename, "HelpText");
84 /********************************************************************
89 * Tests help printing for each option type, but doesn't contain much
90 * variablity in the options.
92 TEST_F(CommandLineHelpWriterTest, HandlesOptionTypes)
96 Options options("test", "Short Description");
97 options.addOption(BooleanOption("bool").description("Boolean option")
99 options.addOption(BooleanOption("hidden").description("Hidden option")
100 .hidden().defaultValue(true));
101 options.addOption(IntegerOption("int").description("Integer option")
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")
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));
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());
134 options.addOption(SelectionFileOption("sf"));
135 options.addOption(SelectionOption("sel").description("Selection option"));
137 CommandLineHelpWriter writer(options);
138 writer.setShowHidden(true);
143 * Tests help printing with file name options with various values that don't
144 * fit into the allocated columns.
146 TEST_F(CommandLineHelpWriterTest, HandlesLongFileOptions)
148 using gmx::FileNameOption;
149 using gmx::eftGenericData;
150 using gmx::eftTrajectory;
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"));
174 gmx::CommandLineHelpWriter writer(options);
179 * Tests help printing with general options with various values that don't
180 * fit into the allocated columns.
182 TEST_F(CommandLineHelpWriterTest, HandlesLongOptions)
184 using gmx::BooleanOption;
185 using gmx::DoubleOption;
186 using gmx::StringOption;
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));
204 gmx::CommandLineHelpWriter writer(options);
209 * Tests help printing with selection options with values.
211 TEST_F(CommandLineHelpWriterTest, HandlesSelectionOptions)
213 using gmx::SelectionFileOption;
214 using gmx::SelectionOption;
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);
226 manager.parseRequestedFromString(
228 "surface = within 0.5 of resname SOL;"
229 "group \"Protein\" and surface;"
230 "group \"Protein\" and not surface;");
232 gmx::CommandLineHelpWriter writer(options);
237 * Tests help printing for multiple sections.
239 TEST_F(CommandLineHelpWriterTest, HandlesMultipleSections)
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"));
261 CommandLineHelpWriter writer(options);
262 writer.setShowDescriptions(true);