2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2018,2019, 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 * Tests for general functionality in gmx::TrajectoryAnalysisCommandLineRunner.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_trajectoryanalysis
44 #include "gromacs/trajectoryanalysis/cmdlinerunner.h"
46 #include <gmock/gmock.h>
47 #include <gtest/gtest.h>
49 #include "gromacs/commandline/cmdlinemodule.h"
50 #include "gromacs/commandline/cmdlineoptionsmodule.h"
51 #include "gromacs/options/basicoptions.h"
52 #include "gromacs/options/ioptionscontainer.h"
53 #include "gromacs/trajectory/trajectoryframe.h"
54 #include "gromacs/trajectoryanalysis/analysismodule.h"
55 #include "gromacs/trajectoryanalysis/analysissettings.h"
56 #include "gromacs/trajectoryanalysis/topologyinformation.h"
57 #include "gromacs/utility/exceptions.h"
59 #include "testutils/cmdlinetest.h"
60 #include "testutils/testasserts.h"
65 class MockModule : public gmx::TrajectoryAnalysisModule
68 MOCK_METHOD2(initOptions,
69 void(gmx::IOptionsContainer* options, gmx::TrajectoryAnalysisSettings* settings));
70 MOCK_METHOD2(initAnalysis,
71 void(const gmx::TrajectoryAnalysisSettings& settings, const gmx::TopologyInformation& top));
73 MOCK_METHOD4(analyzeFrame,
74 void(int frnr, const t_trxframe& fr, t_pbc* pbc, gmx::TrajectoryAnalysisModuleData* pdata));
75 MOCK_METHOD1(finishAnalysis, void(int nframes));
76 MOCK_METHOD0(writeOutput, void());
79 using gmx::test::CommandLine;
81 class TrajectoryAnalysisCommandLineRunnerTest : public gmx::test::CommandLineTestBase
84 TrajectoryAnalysisCommandLineRunnerTest() : mockModule_(new MockModule()) {}
86 gmx::ICommandLineOptionsModulePointer createRunner()
88 return gmx::TrajectoryAnalysisCommandLineRunner::createModule(std::move(mockModule_));
91 void runTest(const CommandLine& args)
93 CommandLine& cmdline = commandLine();
95 ASSERT_EQ(0, gmx::test::CommandLineTestHelper::runModuleDirect(createRunner(), &cmdline));
98 std::unique_ptr<MockModule> mockModule_;
101 //! Initializes options for help testing.
102 void initOptions(gmx::IOptionsContainer* options, gmx::TrajectoryAnalysisSettings* settings)
104 const char* const desc[] = { "Sample description", "for testing [THISMODULE]." };
105 settings->setHelpText(desc);
107 options->addOption(gmx::BooleanOption("test").description("Test option"));
110 TEST_F(TrajectoryAnalysisCommandLineRunnerTest, WritesHelp)
113 using ::testing::Invoke;
114 EXPECT_CALL(*mockModule_, initOptions(_, _)).WillOnce(Invoke(&initOptions));
116 const std::unique_ptr<gmx::ICommandLineModule> module(
117 gmx::ICommandLineOptionsModule::createModule("mod", "Description", createRunner()));
118 testWriteHelp(module.get());
121 TEST_F(TrajectoryAnalysisCommandLineRunnerTest, RunsWithSubsetTrajectory)
123 const char* const cmdline[] = { "-fgroup", "atomnr 4 5 6 10 to 14" };
126 EXPECT_CALL(*mockModule_, initOptions(_, _));
127 EXPECT_CALL(*mockModule_, initAnalysis(_, _));
128 EXPECT_CALL(*mockModule_, analyzeFrame(0, _, _, _));
129 EXPECT_CALL(*mockModule_, analyzeFrame(1, _, _, _));
130 EXPECT_CALL(*mockModule_, finishAnalysis(2));
131 EXPECT_CALL(*mockModule_, writeOutput());
133 setInputFile("-s", "simple.gro");
134 setInputFile("-f", "simple-subset.gro");
135 EXPECT_NO_THROW_GMX(runTest(CommandLine(cmdline)));
138 TEST_F(TrajectoryAnalysisCommandLineRunnerTest, DetectsIncorrectTrajectorySubset)
140 const char* const cmdline[] = { "-fgroup", "atomnr 3 to 6 10 to 14" };
143 EXPECT_CALL(*mockModule_, initOptions(_, _));
144 EXPECT_CALL(*mockModule_, initAnalysis(_, _));
146 setInputFile("-s", "simple.gro");
147 setInputFile("-f", "simple-subset.gro");
148 EXPECT_THROW_GMX(runTest(CommandLine(cmdline)), gmx::InconsistentInputError);
151 TEST_F(TrajectoryAnalysisCommandLineRunnerTest, FailsWithTrajectorySubsetWithoutTrajectory)
153 const char* const cmdline[] = { "-fgroup", "atomnr 3 to 6 10 to 14" };
156 EXPECT_CALL(*mockModule_, initOptions(_, _));
158 setInputFile("-s", "simple.gro");
159 EXPECT_THROW_GMX(runTest(CommandLine(cmdline)), gmx::InconsistentInputError);