1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,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 gmx::CommandLineHelpModule through gmx::CommandLineModuleManager.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_commandline
41  */
42 #include "gmxpre.h"
43 
44 #include <cstdio>
45 
46 #include <gmock/gmock.h>
47 
48 #include "gromacs/commandline/cmdlinemodulemanager.h"
49 #include "gromacs/options/basicoptions.h"
50 #include "gromacs/options/options.h"
51 #include "gromacs/utility/textwriter.h"
52 
53 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
54 #include "testutils/cmdlinetest.h"
55 #include "testutils/testasserts.h"
56 
57 #include "cmdlinemodulemanagertest.h"
58 
59 namespace
60 {
61 
62 using gmx::test::CommandLine;
63 using gmx::test::MockHelpTopic;
64 using gmx::test::MockOptionsModule;
65 
66 //! Test fixture for the tests.
67 typedef gmx::test::CommandLineModuleManagerTestBase CommandLineHelpModuleTest;
68 
TEST_F(CommandLineHelpModuleTest,PrintsGeneralHelp)69 TEST_F(CommandLineHelpModuleTest, PrintsGeneralHelp)
70 {
71     const char* const cmdline[] = { "test" };
72     CommandLine       args(cmdline);
73     initManager(args, "test");
74     addModule("module", "First module");
75     addModule("other", "Second module");
76     addHelpTopic("topic", "Test topic");
77     int rc = 0;
78     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
79     ASSERT_EQ(0, rc);
80     checkRedirectedOutput();
81 }
82 
TEST_F(CommandLineHelpModuleTest,PrintsHelpOnTopic)83 TEST_F(CommandLineHelpModuleTest, PrintsHelpOnTopic)
84 {
85     const char* const cmdline[] = { "test", "help", "topic" };
86     CommandLine       args(cmdline);
87     initManager(args, "test");
88     addModule("module", "First module");
89     MockHelpTopic& topic = addHelpTopic("topic", "Test topic");
90     topic.addSubTopic("sub1", "Subtopic 1", "");
91     topic.addSubTopic("sub2", "Subtopic 2", "");
92     using ::testing::_;
93     EXPECT_CALL(topic, writeHelp(_));
94     int rc = 0;
95     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
96     ASSERT_EQ(0, rc);
97     checkRedirectedOutput();
98 }
99 
100 /*! \brief
101  * Initializes Options for help export tests.
102  *
103  * \ingroup module_commandline
104  */
initOptionsBasic(gmx::IOptionsContainer * options,gmx::ICommandLineOptionsModuleSettings * settings)105 void initOptionsBasic(gmx::IOptionsContainer* options, gmx::ICommandLineOptionsModuleSettings* settings)
106 {
107     const char* const desc[] = { "Sample description", "for testing [THISMODULE]." };
108     settings->setHelpText(desc);
109     const char* const bug[] = { "Known issue for [THISMODULE].",
110                                 "With another bug for [THISMODULE]." };
111     settings->setBugText(bug);
112     options->addOption(gmx::IntegerOption("int").description("Integer option"));
113 }
114 
TEST_F(CommandLineHelpModuleTest,ExportsHelp)115 TEST_F(CommandLineHelpModuleTest, ExportsHelp)
116 {
117     const char* const cmdline[] = { "test", "help", "-export", "rst" };
118     // TODO: Find a more elegant solution, or get rid of the links.dat altogether.
119     gmx::TextWriter::writeFileFromString("links.dat", "");
120     CommandLine args(cmdline);
121     initManager(args, "test");
122     MockOptionsModule& mod1 = addOptionsModule("module", "First module");
123     MockOptionsModule& mod2 = addOptionsModule("other", "Second module");
124     {
125         gmx::CommandLineModuleGroup group = manager().addModuleGroup("Group 1");
126         group.addModule("module");
127     }
128     {
129         gmx::CommandLineModuleGroup group = manager().addModuleGroup("Group 2");
130         group.addModule("other");
131     }
132     MockHelpTopic& topic1 = addHelpTopic("topic1", "Test topic");
133     MockHelpTopic& sub1   = topic1.addSubTopic("sub1", "Subtopic 1", "Sub text");
134     MockHelpTopic& sub2   = topic1.addSubTopic("sub2", "Subtopic 2", "Sub text");
135     MockHelpTopic& sub3   = topic1.addSubTopic("other", "Out-of-order subtopic", "Sub text");
136     MockHelpTopic& topic2 = addHelpTopic("topic2", "Another topic");
137     using ::testing::_;
138     using ::testing::Invoke;
139     EXPECT_CALL(mod1, initOptions(_, _)).WillOnce(Invoke(&initOptionsBasic));
140     EXPECT_CALL(mod2, initOptions(_, _));
141     EXPECT_CALL(topic1, writeHelp(_));
142     EXPECT_CALL(sub1, writeHelp(_));
143     EXPECT_CALL(sub2, writeHelp(_));
144     EXPECT_CALL(sub3, writeHelp(_));
145     EXPECT_CALL(topic2, writeHelp(_));
146     int rc = 0;
147     ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
148     ASSERT_EQ(0, rc);
149     checkRedirectedOutput();
150     std::remove("links.dat");
151 }
152 
153 } // namespace
154