2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
38 * Tests for help topic management and help topic formatting.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_onlinehelp
45 #include "gromacs/onlinehelp/helpmanager.h"
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
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"
58 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
59 #include "testutils/stringtest.h"
60 #include "testutils/testasserts.h"
65 using gmx::test::MockHelpTopic;
67 class HelpTestBase : public gmx::test::StringTestBase
72 MockHelpTopic rootTopic_;
73 gmx::StringOutputStream helpFile_;
74 gmx::TextWriter writer_;
75 gmx::HelpWriterContext context_;
76 gmx::HelpManager manager_;
79 HelpTestBase::HelpTestBase() :
80 rootTopic_("", nullptr, "Root topic text"),
82 context_(&writer_, gmx::eHelpOutputFormat_Console),
83 manager_(rootTopic_, context_)
87 /********************************************************************
88 * Tests for HelpManager
91 //! Test fixture for gmx::HelpManager.
92 typedef HelpTestBase HelpManagerTest;
94 TEST_F(HelpManagerTest, HandlesRootTopic)
97 EXPECT_CALL(rootTopic_, writeHelp(_));
98 manager_.writeCurrentTopic();
101 TEST_F(HelpManagerTest, HandlesSubTopics)
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);
108 EXPECT_CALL(firstSub, writeHelp(_));
109 ASSERT_NO_THROW_GMX(manager_.enterTopic("first"));
110 ASSERT_NO_THROW_GMX(manager_.enterTopic("firstsub"));
111 manager_.writeCurrentTopic();
114 TEST_F(HelpManagerTest, HandlesInvalidTopics)
116 MockHelpTopic& first = rootTopic_.addSubTopic("first", "First topic", nullptr);
117 first.addSubTopic("firstsub", "First subtopic", nullptr);
118 rootTopic_.addSubTopic("second", "Second topic", nullptr);
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"));
127 /********************************************************************
128 * Tests for help topic formatting
133 static const char name[];
134 static const char title[];
135 static const char* const text[];
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." };
142 class HelpTopicFormattingTest : public HelpTestBase
145 void checkHelpFormatting();
148 void HelpTopicFormattingTest::checkHelpFormatting()
150 ASSERT_NO_THROW_GMX(manager_.enterTopic("testtopic"));
151 ASSERT_NO_THROW_GMX(manager_.writeCurrentTopic());
154 checkText(helpFile_.toString(), "HelpText");
157 TEST_F(HelpTopicFormattingTest, FormatsSimpleTopic)
159 rootTopic_.addSubTopic(gmx::HelpTopicPointer(new gmx::SimpleHelpTopic<TestHelpText>));
160 checkHelpFormatting();
163 TEST_F(HelpTopicFormattingTest, FormatsCompositeTopicWithSubTopics)
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();