Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / onlinehelp / tests / helpmanager.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Tests for help topic management and help topic formatting.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_onlinehelp
37  */
38 #include "gromacs/onlinehelp/helpmanager.h"
39
40 #include <string>
41
42 #include <gmock/gmock.h>
43 #include <gtest/gtest.h>
44
45 #include "gromacs/onlinehelp/helptopic.h"
46 #include "gromacs/onlinehelp/helpwritercontext.h"
47 #include "gromacs/utility/exceptions.h"
48 #include "gromacs/utility/file.h"
49
50 #include "testutils/mock_helptopic.h"
51 #include "testutils/stringtest.h"
52 #include "testutils/testfilemanager.h"
53
54 namespace
55 {
56
57 using gmx::test::MockHelpTopic;
58
59 class HelpTestBase : public gmx::test::StringTestBase
60 {
61     public:
62         HelpTestBase();
63
64         gmx::test::TestFileManager tempFiles_;
65         MockHelpTopic              rootTopic_;
66         std::string                filename_;
67         gmx::File                  helpFile_;
68         gmx::HelpWriterContext     context_;
69         gmx::HelpManager           manager_;
70 };
71
72 HelpTestBase::HelpTestBase()
73     : rootTopic_("", NULL, "Root topic text"),
74       filename_(tempFiles_.getTemporaryFilePath("helptext.txt")),
75       helpFile_(filename_, "w"),
76       context_(&helpFile_, gmx::eHelpOutputFormat_Console),
77       manager_(rootTopic_, context_)
78 {
79 }
80
81 /********************************************************************
82  * Tests for HelpManager
83  */
84
85 //! Test fixture for gmx::HelpManager.
86 typedef HelpTestBase HelpManagerTest;
87
88 TEST_F(HelpManagerTest, HandlesRootTopic)
89 {
90     using ::testing::_;
91     EXPECT_CALL(rootTopic_, writeHelp(_));
92     manager_.writeCurrentTopic();
93 }
94
95 TEST_F(HelpManagerTest, HandlesSubTopics)
96 {
97     MockHelpTopic &first =
98         rootTopic_.addSubTopic("first", "First topic", "First topic text");
99     MockHelpTopic &firstSub =
100         first.addSubTopic("firstsub", "First subtopic", "First subtopic text");
101     rootTopic_.addSubTopic("second", "Second topic", "Second topic text");
102
103     using ::testing::_;
104     EXPECT_CALL(firstSub, writeHelp(_));
105     ASSERT_NO_THROW(manager_.enterTopic("first"));
106     ASSERT_NO_THROW(manager_.enterTopic("firstsub"));
107     manager_.writeCurrentTopic();
108 }
109
110 TEST_F(HelpManagerTest, HandlesInvalidTopics)
111 {
112     MockHelpTopic &first =
113         rootTopic_.addSubTopic("first", "First topic", "First topic text");
114     first.addSubTopic("firstsub", "First subtopic", "First subtopic text");
115     rootTopic_.addSubTopic("second", "Second topic", "Second topic text");
116
117     ASSERT_THROW(manager_.enterTopic("unknown"), gmx::InvalidInputError);
118     ASSERT_NO_THROW(manager_.enterTopic("first"));
119     ASSERT_THROW(manager_.enterTopic("unknown"), gmx::InvalidInputError);
120     ASSERT_THROW(manager_.enterTopic("second"), gmx::InvalidInputError);
121     ASSERT_NO_THROW(manager_.enterTopic("firstsub"));
122 }
123
124 /********************************************************************
125  * Tests for help topic formatting
126  */
127
128 struct TestHelpText
129 {
130     static const char        name[];
131     static const char        title[];
132     static const char *const text[];
133 };
134
135 const char        TestHelpText::name[]  = "testtopic";
136 const char        TestHelpText::title[] = "Topic title";
137 const char *const TestHelpText::text[]  = {
138     "Test topic text.[PAR]",
139     "Another paragraph of text."
140 };
141
142 class HelpTopicFormattingTest : public HelpTestBase
143 {
144     public:
145         void checkHelpFormatting();
146 };
147
148 void HelpTopicFormattingTest::checkHelpFormatting()
149 {
150     ASSERT_NO_THROW(manager_.enterTopic("testtopic"));
151     ASSERT_NO_THROW(manager_.writeCurrentTopic());
152     helpFile_.close();
153
154     checkFileContents(filename_, "HelpText");
155 }
156
157 TEST_F(HelpTopicFormattingTest, FormatsSimpleTopic)
158 {
159     rootTopic_.addSubTopic(gmx::HelpTopicPointer(
160                                    new gmx::SimpleHelpTopic<TestHelpText>));
161     checkHelpFormatting();
162 }
163
164 TEST_F(HelpTopicFormattingTest, FormatsCompositeTopicWithSubTopics)
165 {
166     gmx::CompositeHelpTopicPointer topic(new gmx::CompositeHelpTopic<TestHelpText>);
167     MockHelpTopic::addSubTopic(topic.get(), "subtopic", "First subtopic", "Text");
168     MockHelpTopic::addSubTopic(topic.get(), "other", "Second subtopic", "Text");
169     rootTopic_.addSubTopic(move(topic));
170     checkHelpFormatting();
171 }
172
173 } // namespace