2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Implements classes from cmdlinetest.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_testutils
44 #include "cmdlinetest.h"
53 #include <boost/scoped_ptr.hpp>
55 #include "gromacs/commandline/cmdlineoptionsmodule.h"
56 #include "gromacs/commandline/cmdlineprogramcontext.h"
57 #include "gromacs/utility/arrayref.h"
58 #include "gromacs/utility/file.h"
59 #include "gromacs/utility/gmxassert.h"
60 #include "gromacs/utility/stringutil.h"
62 #include "testutils/refdata.h"
63 #include "testutils/testfilemanager.h"
70 /********************************************************************
74 class CommandLine::Impl
77 Impl(const char *const cmdline[], size_t count);
80 std::vector<char *> args_;
81 std::vector<char *> argv_;
85 CommandLine::Impl::Impl(const char *const cmdline[], size_t count)
88 argv_.reserve(count + 1);
89 argc_ = static_cast<int>(count);
90 for (size_t i = 0; i < count; ++i)
92 char *arg = strdup(cmdline[i]);
95 throw std::bad_alloc();
100 argv_.push_back(NULL);
103 CommandLine::Impl::~Impl()
105 for (size_t i = 0; i < args_.size(); ++i)
111 /********************************************************************
115 CommandLine::CommandLine()
116 : impl_(new Impl(NULL, 0))
120 CommandLine::CommandLine(const ConstArrayRef<const char *> &cmdline)
121 : impl_(new Impl(cmdline.data(), cmdline.size()))
125 CommandLine::CommandLine(const CommandLine &other)
126 : impl_(new Impl(other.argv(), other.argc()))
130 CommandLine::~CommandLine()
134 void CommandLine::initFromArray(const ConstArrayRef<const char *> &cmdline)
136 impl_.reset(new Impl(cmdline.data(), cmdline.size()));
139 void CommandLine::append(const char *arg)
141 GMX_RELEASE_ASSERT(impl_->argc_ == static_cast<int>(impl_->args_.size()),
142 "Command-line has been modified externally");
143 size_t newSize = impl_->args_.size() + 1;
144 impl_->args_.reserve(newSize);
145 impl_->argv_.reserve(newSize + 1);
146 char *newArg = strdup(arg);
149 throw std::bad_alloc();
151 impl_->args_.push_back(newArg);
152 impl_->argv_.pop_back(); // Remove the trailing NULL.
153 impl_->argv_.push_back(newArg);
154 impl_->argv_.push_back(NULL);
155 impl_->argc_ = static_cast<int>(newSize);
161 //! Helper function for converting values to strings
162 template <typename T>
163 std::string value2string(T value)
165 std::stringstream ss;
172 void CommandLine::addOption(const char *name, const char *value)
178 void CommandLine::addOption(const char *name, const std::string &value)
180 addOption(name, value.c_str());
183 void CommandLine::addOption(const char *name, int value)
186 append(value2string(value));
189 void CommandLine::addOption(const char *name, double value)
192 append(value2string(value));
195 void CommandLine::merge(const CommandLine &args)
197 // Skip first argument if it is the module name.
198 const int firstArg = (args.arg(0)[0] == '-' ? 0 : 1);
199 for (int i = firstArg; i < args.argc(); ++i)
205 int &CommandLine::argc()
209 char **CommandLine::argv()
211 return &impl_->argv_[0];
213 int CommandLine::argc() const
217 const char *const *CommandLine::argv() const
219 return &impl_->argv_[0];
221 const char *CommandLine::arg(int i) const
223 return impl_->argv_[i];
226 std::string CommandLine::toString() const
228 return CommandLineProgramContext(argc(), argv()).commandLine();
231 /********************************************************************
232 * CommandLineTestHelper::Impl
235 class CommandLineTestHelper::Impl
238 struct OutputFileInfo
240 OutputFileInfo(const char *option, const std::string &path)
241 : option(option), path(path)
249 typedef std::vector<OutputFileInfo> OutputFileList;
251 explicit Impl(TestFileManager *fileManager)
252 : fileManager_(*fileManager)
256 TestFileManager &fileManager_;
257 OutputFileList outputFiles_;
260 /********************************************************************
261 * CommandLineTestHelper
265 int CommandLineTestHelper::runModule(
266 CommandLineModuleInterface *module, CommandLine *commandLine)
268 CommandLineModuleSettings settings;
269 module->init(&settings);
270 return module->run(commandLine->argc(), commandLine->argv());
274 int CommandLineTestHelper::runModule(
275 CommandLineOptionsModuleInterface::FactoryMethod factory,
276 CommandLine *commandLine)
278 // The name and description are not used in the tests, so they can be NULL.
279 boost::scoped_ptr<CommandLineModuleInterface> module(
280 CommandLineOptionsModuleInterface::createModule(NULL, NULL, factory));
281 return runModule(module.get(), commandLine);
284 CommandLineTestHelper::CommandLineTestHelper(TestFileManager *fileManager)
285 : impl_(new Impl(fileManager))
289 CommandLineTestHelper::~CommandLineTestHelper()
293 void CommandLineTestHelper::setInputFileContents(
294 CommandLine *args, const char *option, const char *extension,
295 const std::string &contents)
297 GMX_ASSERT(extension[0] != '.', "Extension should not contain a dot");
298 std::string fullFilename = impl_->fileManager_.getTemporaryFilePath(
299 formatString("%d.%s", args->argc(), extension));
300 File::writeFileFromString(fullFilename, contents);
301 args->addOption(option, fullFilename);
304 void CommandLineTestHelper::setInputFileContents(
305 CommandLine *args, const char *option, const char *extension,
306 const ConstArrayRef<const char *> &contents)
308 GMX_ASSERT(extension[0] != '.', "Extension should not contain a dot");
309 std::string fullFilename = impl_->fileManager_.getTemporaryFilePath(
310 formatString("%d.%s", args->argc(), extension));
311 File file(fullFilename, "w");
312 ConstArrayRef<const char *>::const_iterator i;
313 for (i = contents.begin(); i != contents.end(); ++i)
318 args->addOption(option, fullFilename);
321 void CommandLineTestHelper::setOutputFile(
322 CommandLine *args, const char *option, const char *filename)
324 std::string fullFilename = impl_->fileManager_.getTemporaryFilePath(filename);
325 args->addOption(option, fullFilename);
326 impl_->outputFiles_.push_back(Impl::OutputFileInfo(option, fullFilename));
329 void CommandLineTestHelper::setOutputFileNoTest(
330 CommandLine *args, const char *option, const char *extension)
332 std::string fullFilename = impl_->fileManager_.getTemporaryFilePath(
333 formatString("%d.%s", args->argc(), extension));
334 args->addOption(option, fullFilename);
337 void CommandLineTestHelper::checkOutputFiles(TestReferenceChecker checker) const
339 if (!impl_->outputFiles_.empty())
341 TestReferenceChecker outputChecker(
342 checker.checkCompound("OutputFiles", "Files"));
343 Impl::OutputFileList::const_iterator outfile;
344 for (outfile = impl_->outputFiles_.begin();
345 outfile != impl_->outputFiles_.end();
348 std::string output = File::readToString(outfile->path);
349 outputChecker.checkStringBlock(output, outfile->option.c_str());
354 /********************************************************************
355 * CommandLineTestBase::Impl
358 class CommandLineTestBase::Impl
361 Impl() : helper_(&tempFiles_)
363 cmdline_.append("module");
366 TestReferenceData data_;
367 TestFileManager tempFiles_;
368 CommandLineTestHelper helper_;
369 CommandLine cmdline_;
372 /********************************************************************
373 * CommandLineTestBase
376 CommandLineTestBase::CommandLineTestBase()
381 CommandLineTestBase::~CommandLineTestBase()
385 void CommandLineTestBase::setInputFile(
386 const char *option, const char *filename)
388 impl_->cmdline_.addOption(option, TestFileManager::getInputFilePath(filename));
391 void CommandLineTestBase::setInputFileContents(
392 const char *option, const char *extension, const std::string &contents)
394 impl_->helper_.setInputFileContents(&impl_->cmdline_, option, extension,
398 void CommandLineTestBase::setInputFileContents(
399 const char *option, const char *extension,
400 const ConstArrayRef<const char *> &contents)
402 impl_->helper_.setInputFileContents(&impl_->cmdline_, option, extension,
406 void CommandLineTestBase::setOutputFile(
407 const char *option, const char *filename)
409 impl_->helper_.setOutputFile(&impl_->cmdline_, option, filename);
412 void CommandLineTestBase::setOutputFileNoTest(
413 const char *option, const char *extension)
415 impl_->helper_.setOutputFileNoTest(&impl_->cmdline_, option, extension);
418 CommandLine &CommandLineTestBase::commandLine()
420 return impl_->cmdline_;
423 TestFileManager &CommandLineTestBase::fileManager()
425 return impl_->tempFiles_;
428 TestReferenceChecker CommandLineTestBase::rootChecker()
430 return impl_->data_.rootChecker();
433 void CommandLineTestBase::checkOutputFiles()
435 impl_->helper_.checkOutputFiles(rootChecker());