Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / tests / cmdlinerunner.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 for general functionality in gmx::TrajectoryAnalysisCommandLineRunner.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_trajectoryanalysis
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/trajectoryanalysis/cmdlinerunner.h"
45
46 #include <gmock/gmock.h>
47 #include <gtest/gtest.h>
48
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"
58
59 #include "testutils/cmdlinetest.h"
60 #include "testutils/testasserts.h"
61
62 namespace
63 {
64
65 class MockModule : public gmx::TrajectoryAnalysisModule
66 {
67 public:
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));
72
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());
77 };
78
79 using gmx::test::CommandLine;
80
81 class TrajectoryAnalysisCommandLineRunnerTest : public gmx::test::CommandLineTestBase
82 {
83 public:
84     TrajectoryAnalysisCommandLineRunnerTest() : mockModule_(new MockModule()) {}
85
86     gmx::ICommandLineOptionsModulePointer createRunner()
87     {
88         return gmx::TrajectoryAnalysisCommandLineRunner::createModule(std::move(mockModule_));
89     }
90
91     void runTest(const CommandLine& args)
92     {
93         CommandLine& cmdline = commandLine();
94         cmdline.merge(args);
95         ASSERT_EQ(0, gmx::test::CommandLineTestHelper::runModuleDirect(createRunner(), &cmdline));
96     }
97
98     std::unique_ptr<MockModule> mockModule_;
99 };
100
101 //! Initializes options for help testing.
102 void initOptions(gmx::IOptionsContainer* options, gmx::TrajectoryAnalysisSettings* settings)
103 {
104     const char* const desc[] = { "Sample description", "for testing [THISMODULE]." };
105     settings->setHelpText(desc);
106
107     options->addOption(gmx::BooleanOption("test").description("Test option"));
108 }
109
110 TEST_F(TrajectoryAnalysisCommandLineRunnerTest, WritesHelp)
111 {
112     using ::testing::_;
113     using ::testing::Invoke;
114     EXPECT_CALL(*mockModule_, initOptions(_, _)).WillOnce(Invoke(&initOptions));
115
116     const std::unique_ptr<gmx::ICommandLineModule> module(
117             gmx::ICommandLineOptionsModule::createModule("mod", "Description", createRunner()));
118     testWriteHelp(module.get());
119 }
120
121 TEST_F(TrajectoryAnalysisCommandLineRunnerTest, RunsWithSubsetTrajectory)
122 {
123     const char* const cmdline[] = { "-fgroup", "atomnr 4 5 6 10 to 14" };
124
125     using ::testing::_;
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());
132
133     setInputFile("-s", "simple.gro");
134     setInputFile("-f", "simple-subset.gro");
135     EXPECT_NO_THROW_GMX(runTest(CommandLine(cmdline)));
136 }
137
138 TEST_F(TrajectoryAnalysisCommandLineRunnerTest, DetectsIncorrectTrajectorySubset)
139 {
140     const char* const cmdline[] = { "-fgroup", "atomnr 3 to 6 10 to 14" };
141
142     using ::testing::_;
143     EXPECT_CALL(*mockModule_, initOptions(_, _));
144     EXPECT_CALL(*mockModule_, initAnalysis(_, _));
145
146     setInputFile("-s", "simple.gro");
147     setInputFile("-f", "simple-subset.gro");
148     EXPECT_THROW_GMX(runTest(CommandLine(cmdline)), gmx::InconsistentInputError);
149 }
150
151 TEST_F(TrajectoryAnalysisCommandLineRunnerTest, FailsWithTrajectorySubsetWithoutTrajectory)
152 {
153     const char* const cmdline[] = { "-fgroup", "atomnr 3 to 6 10 to 14" };
154
155     using ::testing::_;
156     EXPECT_CALL(*mockModule_, initOptions(_, _));
157
158     setInputFile("-s", "simple.gro");
159     EXPECT_THROW_GMX(runTest(CommandLine(cmdline)), gmx::InconsistentInputError);
160 }
161
162 } // namespace