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