Tidy: modernize-use-nullptr
[alexxy/gromacs.git] / src / gromacs / commandline / tests / cmdlinemodulemanager.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2017, 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::CommandLineModuleManager.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_commandline
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/commandline/cmdlinemodulemanager.h"
45
46 #include <gmock/gmock.h>
47
48 #include "testutils/cmdlinetest.h"
49 #include "testutils/testasserts.h"
50
51 #include "cmdlinemodulemanagertest.h"
52
53 namespace
54 {
55
56 using gmx::test::CommandLine;
57 using gmx::test::MockModule;
58
59 //! Test fixture for the tests.
60 typedef gmx::test::CommandLineModuleManagerTestBase CommandLineModuleManagerTest;
61
62 TEST_F(CommandLineModuleManagerTest, RunsModule)
63 {
64     const char *const cmdline[] = {
65         "test", "module", "-flag", "yes"
66     };
67     CommandLine       args(cmdline);
68     initManager(args, "test");
69     MockModule       &mod1 = addModule("module", "First module");
70     addModule("other", "Second module");
71     using ::testing::_;
72     using ::testing::Args;
73     using ::testing::ElementsAreArray;
74     EXPECT_CALL(mod1, init(_));
75     EXPECT_CALL(mod1, run(_, _))
76         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
77     int rc = 0;
78     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
79     ASSERT_EQ(0, rc);
80 }
81
82 TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
83 {
84     const char *const cmdline[] = {
85         "test", "help", "module"
86     };
87     CommandLine       args(cmdline);
88     initManager(args, "test");
89     MockModule       &mod1 = addModule("module", "First module");
90     addModule("other", "Second module");
91     using ::testing::_;
92     EXPECT_CALL(mod1, writeHelp(_));
93     mod1.setExpectedDisplayName("test module");
94     int rc = 0;
95     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
96     ASSERT_EQ(0, rc);
97 }
98
99 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
100 {
101     const char *const cmdline[] = {
102         "test", "module", "-h"
103     };
104     CommandLine       args(cmdline);
105     initManager(args, "test");
106     MockModule       &mod1 = addModule("module", "First module");
107     addModule("other", "Second module");
108     using ::testing::_;
109     EXPECT_CALL(mod1, writeHelp(_));
110     mod1.setExpectedDisplayName("test module");
111     int rc = 0;
112     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
113     ASSERT_EQ(0, rc);
114 }
115
116 TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashHWithSingleModule)
117 {
118     const char *const cmdline[] = {
119         "g_module", "-h"
120     };
121     CommandLine       args(cmdline);
122     initManager(args, "g_module");
123     MockModule        mod(nullptr, nullptr);
124     manager().setSingleModule(&mod);
125     using ::testing::_;
126     EXPECT_CALL(mod, writeHelp(_));
127     mod.setExpectedDisplayName("g_module");
128     int rc = 0;
129     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
130     ASSERT_EQ(0, rc);
131 }
132
133 TEST_F(CommandLineModuleManagerTest, HandlesConflictingBinaryAndModuleNames)
134 {
135     const char *const cmdline[] = {
136         "test", "test", "-flag", "yes"
137     };
138     CommandLine       args(cmdline);
139     initManager(args, "test");
140     MockModule       &mod1 = addModule("test", "Test module");
141     addModule("other", "Second module");
142     using ::testing::_;
143     using ::testing::Args;
144     using ::testing::ElementsAreArray;
145     EXPECT_CALL(mod1, init(_));
146     EXPECT_CALL(mod1, run(_, _))
147         .With(Args<1, 0>(ElementsAreArray(args.argv() + 1, args.argc() - 1)));
148     int rc = 0;
149     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
150     ASSERT_EQ(0, rc);
151 }
152
153 } // namespace