Version bumps after new release
[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, 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 help topic management and help topic formatting.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_onlinehelp
41  */
42 #include "gromacs/onlinehelp/helpmanager.h"
43
44 #include <string>
45
46 #include <gmock/gmock.h>
47 #include <gtest/gtest.h>
48
49 #include "gromacs/onlinehelp/helptopic.h"
50 #include "gromacs/onlinehelp/helpwritercontext.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/file.h"
53
54 #include "gromacs/onlinehelp/tests/mock_helptopic.h"
55 #include "testutils/stringtest.h"
56 #include "testutils/testasserts.h"
57 #include "testutils/testfilemanager.h"
58
59 namespace
60 {
61
62 using gmx::test::MockHelpTopic;
63
64 class HelpTestBase : public gmx::test::StringTestBase
65 {
66     public:
67         HelpTestBase();
68
69         gmx::test::TestFileManager tempFiles_;
70         MockHelpTopic              rootTopic_;
71         std::string                filename_;
72         gmx::File                  helpFile_;
73         gmx::HelpWriterContext     context_;
74         gmx::HelpManager           manager_;
75 };
76
77 HelpTestBase::HelpTestBase()
78     : rootTopic_("", NULL, "Root topic text"),
79       filename_(tempFiles_.getTemporaryFilePath("helptext.txt")),
80       helpFile_(filename_, "w"),
81       context_(&helpFile_, gmx::eHelpOutputFormat_Console),
82       manager_(rootTopic_, context_)
83 {
84 }
85
86 /********************************************************************
87  * Tests for HelpManager
88  */
89
90 //! Test fixture for gmx::HelpManager.
91 typedef HelpTestBase HelpManagerTest;
92
93 TEST_F(HelpManagerTest, HandlesRootTopic)
94 {
95     using ::testing::_;
96     EXPECT_CALL(rootTopic_, writeHelp(_));
97     manager_.writeCurrentTopic();
98 }
99
100 TEST_F(HelpManagerTest, HandlesSubTopics)
101 {
102     MockHelpTopic &first =
103         rootTopic_.addSubTopic("first", "First topic", "First topic text");
104     MockHelpTopic &firstSub =
105         first.addSubTopic("firstsub", "First subtopic", "First subtopic text");
106     rootTopic_.addSubTopic("second", "Second topic", "Second topic text");
107
108     using ::testing::_;
109     EXPECT_CALL(firstSub, writeHelp(_));
110     ASSERT_NO_THROW_GMX(manager_.enterTopic("first"));
111     ASSERT_NO_THROW_GMX(manager_.enterTopic("firstsub"));
112     manager_.writeCurrentTopic();
113 }
114
115 TEST_F(HelpManagerTest, HandlesInvalidTopics)
116 {
117     MockHelpTopic &first =
118         rootTopic_.addSubTopic("first", "First topic", "First topic text");
119     first.addSubTopic("firstsub", "First subtopic", "First subtopic text");
120     rootTopic_.addSubTopic("second", "Second topic", "Second topic text");
121
122     ASSERT_THROW_GMX(manager_.enterTopic("unknown"), gmx::InvalidInputError);
123     ASSERT_NO_THROW_GMX(manager_.enterTopic("first"));
124     ASSERT_THROW_GMX(manager_.enterTopic("unknown"), gmx::InvalidInputError);
125     ASSERT_THROW_GMX(manager_.enterTopic("second"), gmx::InvalidInputError);
126     ASSERT_NO_THROW_GMX(manager_.enterTopic("firstsub"));
127 }
128
129 /********************************************************************
130  * Tests for help topic formatting
131  */
132
133 struct TestHelpText
134 {
135     static const char        name[];
136     static const char        title[];
137     static const char *const text[];
138 };
139
140 const char        TestHelpText::name[]  = "testtopic";
141 const char        TestHelpText::title[] = "Topic title";
142 const char *const TestHelpText::text[]  = {
143     "Test topic text.[PAR]",
144     "Another paragraph of text."
145 };
146
147 class HelpTopicFormattingTest : public HelpTestBase
148 {
149     public:
150         void checkHelpFormatting();
151 };
152
153 void HelpTopicFormattingTest::checkHelpFormatting()
154 {
155     ASSERT_NO_THROW_GMX(manager_.enterTopic("testtopic"));
156     ASSERT_NO_THROW_GMX(manager_.writeCurrentTopic());
157     helpFile_.close();
158
159     checkFileContents(filename_, "HelpText");
160 }
161
162 TEST_F(HelpTopicFormattingTest, FormatsSimpleTopic)
163 {
164     rootTopic_.addSubTopic(gmx::HelpTopicPointer(
165                                    new gmx::SimpleHelpTopic<TestHelpText>));
166     checkHelpFormatting();
167 }
168
169 TEST_F(HelpTopicFormattingTest, FormatsCompositeTopicWithSubTopics)
170 {
171     gmx::CompositeHelpTopicPointer topic(new gmx::CompositeHelpTopic<TestHelpText>);
172     MockHelpTopic::addSubTopic(topic.get(), "subtopic", "First subtopic", "Text");
173     MockHelpTopic::addSubTopic(topic.get(), "other", "Second subtopic", "Text");
174     rootTopic_.addSubTopic(move(topic));
175     checkHelpFormatting();
176 }
177
178 } // namespace