Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / onlinehelp / tests / helpmanager.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.
5  * Copyright (c) 2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Tests for help topic management and help topic formatting.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_onlinehelp
42  */
43 #include "gmxpre.h"
44
45 #include "gromacs/onlinehelp/helpmanager.h"
46
47 #include <string>
48
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
51
52 #include "gromacs/onlinehelp/helptopic.h"
53 #include "gromacs/onlinehelp/helpwritercontext.h"
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/stringstream.h"
56 #include "gromacs/utility/textwriter.h"
57
58 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
59 #include "testutils/stringtest.h"
60 #include "testutils/testasserts.h"
61
62 namespace
63 {
64
65 using gmx::test::MockHelpTopic;
66
67 class HelpTestBase : public gmx::test::StringTestBase
68 {
69 public:
70     HelpTestBase();
71
72     MockHelpTopic           rootTopic_;
73     gmx::StringOutputStream helpFile_;
74     gmx::TextWriter         writer_;
75     gmx::HelpWriterContext  context_;
76     gmx::HelpManager        manager_;
77 };
78
79 HelpTestBase::HelpTestBase() :
80     rootTopic_("", nullptr, "Root topic text"),
81     writer_(&helpFile_),
82     context_(&writer_, gmx::eHelpOutputFormat_Console),
83     manager_(rootTopic_, context_)
84 {
85 }
86
87 /********************************************************************
88  * Tests for HelpManager
89  */
90
91 //! Test fixture for gmx::HelpManager.
92 typedef HelpTestBase HelpManagerTest;
93
94 TEST_F(HelpManagerTest, HandlesRootTopic)
95 {
96     using ::testing::_;
97     EXPECT_CALL(rootTopic_, writeHelp(_));
98     manager_.writeCurrentTopic();
99 }
100
101 TEST_F(HelpManagerTest, HandlesSubTopics)
102 {
103     MockHelpTopic& first    = rootTopic_.addSubTopic("first", "First topic", nullptr);
104     MockHelpTopic& firstSub = first.addSubTopic("firstsub", "First subtopic", nullptr);
105     rootTopic_.addSubTopic("second", "Second topic", nullptr);
106
107     using ::testing::_;
108     EXPECT_CALL(firstSub, writeHelp(_));
109     ASSERT_NO_THROW_GMX(manager_.enterTopic("first"));
110     ASSERT_NO_THROW_GMX(manager_.enterTopic("firstsub"));
111     manager_.writeCurrentTopic();
112 }
113
114 TEST_F(HelpManagerTest, HandlesInvalidTopics)
115 {
116     MockHelpTopic& first = rootTopic_.addSubTopic("first", "First topic", nullptr);
117     first.addSubTopic("firstsub", "First subtopic", nullptr);
118     rootTopic_.addSubTopic("second", "Second topic", nullptr);
119
120     ASSERT_THROW_GMX(manager_.enterTopic("unknown"), gmx::InvalidInputError);
121     ASSERT_NO_THROW_GMX(manager_.enterTopic("first"));
122     ASSERT_THROW_GMX(manager_.enterTopic("unknown"), gmx::InvalidInputError);
123     ASSERT_THROW_GMX(manager_.enterTopic("second"), gmx::InvalidInputError);
124     ASSERT_NO_THROW_GMX(manager_.enterTopic("firstsub"));
125 }
126
127 /********************************************************************
128  * Tests for help topic formatting
129  */
130
131 struct TestHelpText
132 {
133     static const char        name[];
134     static const char        title[];
135     static const char* const text[];
136 };
137
138 const char        TestHelpText::name[]  = "testtopic";
139 const char        TestHelpText::title[] = "Topic title";
140 const char* const TestHelpText::text[]  = { "Test topic text.[PAR]", "Another paragraph of text." };
141
142 class HelpTopicFormattingTest : public HelpTestBase
143 {
144 public:
145     void checkHelpFormatting();
146 };
147
148 void HelpTopicFormattingTest::checkHelpFormatting()
149 {
150     ASSERT_NO_THROW_GMX(manager_.enterTopic("testtopic"));
151     ASSERT_NO_THROW_GMX(manager_.writeCurrentTopic());
152     helpFile_.close();
153
154     checkText(helpFile_.toString(), "HelpText");
155 }
156
157 TEST_F(HelpTopicFormattingTest, FormatsSimpleTopic)
158 {
159     rootTopic_.addSubTopic(gmx::HelpTopicPointer(new gmx::SimpleHelpTopic<TestHelpText>));
160     checkHelpFormatting();
161 }
162
163 TEST_F(HelpTopicFormattingTest, FormatsCompositeTopicWithSubTopics)
164 {
165     gmx::CompositeHelpTopicPointer topic(new gmx::CompositeHelpTopic<TestHelpText>);
166     MockHelpTopic::addSubTopic(topic.get(), "subtopic", "First subtopic", "Text");
167     MockHelpTopic::addSubTopic(topic.get(), "other", "Second subtopic", "Text");
168     rootTopic_.addSubTopic(std::move(topic));
169     checkHelpFormatting();
170 }
171
172 } // namespace