Tests for interactive selection input
[alexxy/gromacs.git] / src / testutils / interactivetest.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015, 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  * Implements classes from interactivetest.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_testutils
41  */
42 #include "gmxpre.h"
43
44 #include "interactivetest.h"
45
46 #include <string>
47
48 #include <gmock/gmock.h>
49 #include <gtest/gtest.h>
50
51 #include "gromacs/utility/arrayref.h"
52 #include "gromacs/utility/stringutil.h"
53 #include "gromacs/utility/textstream.h"
54
55 #include "testutils/refdata.h"
56 #include "testutils/stringtest.h"
57
58 namespace gmx
59 {
60 namespace test
61 {
62
63 // These two classes cannot be in an unnamed namespace (easily), since
64 // then their use as members below would trigger warnings.
65 // But if anyone needs these outside this file, they can easily be moved to a
66 // separate header.
67
68 class MockTextInputStream : public TextInputStream
69 {
70     public:
71         MOCK_METHOD1(readLine, bool(std::string *));
72         MOCK_METHOD0(close, void());
73 };
74
75 class MockTextOutputStream : public TextOutputStream
76 {
77     public:
78         MOCK_METHOD1(write, void(const char *));
79         MOCK_METHOD0(close, void());
80 };
81
82 class InteractiveTestHelper::Impl
83 {
84     public:
85         explicit Impl(TestReferenceChecker checker)
86             : checker_(checker), bLastNewline_(true),
87               currentLine_(0), bHasOutput_(false)
88         {
89             using ::testing::_;
90             using ::testing::Invoke;
91             EXPECT_CALL(inputStream_, readLine(_))
92                 .WillRepeatedly(Invoke(this, &Impl::readInputLine));
93             EXPECT_CALL(inputStream_, close()).Times(0);
94             EXPECT_CALL(outputStream_, write(_))
95                 .WillRepeatedly(Invoke(this, &Impl::addOutput));
96             EXPECT_CALL(outputStream_, close()).Times(0);
97         }
98
99         bool readInputLine(std::string *line)
100         {
101             checkOutput();
102             line->clear();
103             const bool bPresent = (currentLine_ < inputLines_.size());
104             if (bPresent)
105             {
106                 line->assign(inputLines_[currentLine_]);
107                 if (bLastNewline_ || currentLine_ + 1 < inputLines_.size())
108                 {
109                     line->append("\n");
110                 }
111             }
112             ++currentLine_;
113             const std::string id = formatString("Input%d", static_cast<int>(currentLine_));
114             StringTestBase::checkText(&checker_, *line, id.c_str());
115             return bPresent;
116         }
117         void addOutput(const char *str)
118         {
119             bHasOutput_ = true;
120             currentOutput_.append(str);
121         }
122
123         void checkOutput()
124         {
125             const std::string id = formatString("Output%d", static_cast<int>(currentLine_));
126             if (checker_.checkPresent(bHasOutput_, id.c_str()))
127             {
128                 StringTestBase::checkText(&checker_, currentOutput_, id.c_str());
129             }
130             bHasOutput_ = false;
131             currentOutput_.clear();
132         }
133         void checkPendingInput()
134         {
135             const std::string id = formatString("Input%d", static_cast<int>(currentLine_+1));
136             checker_.checkPresent(false, id.c_str());
137         }
138
139         TestReferenceChecker             checker_;
140         ConstArrayRef<const char *>      inputLines_;
141         bool                             bLastNewline_;
142         size_t                           currentLine_;
143         bool                             bHasOutput_;
144         std::string                      currentOutput_;
145         MockTextInputStream              inputStream_;
146         MockTextOutputStream             outputStream_;
147 };
148
149 InteractiveTestHelper::InteractiveTestHelper(TestReferenceChecker checker)
150     : impl_(new Impl(checker.checkCompound("InteractiveSession", "Interactive")))
151 {
152 }
153
154 InteractiveTestHelper::~InteractiveTestHelper()
155 {
156 }
157
158 void InteractiveTestHelper::setLastNewline(bool bInclude)
159 {
160     impl_->bLastNewline_ = bInclude;
161 }
162
163 void InteractiveTestHelper::setInputLines(
164         const ConstArrayRef<const char *> &inputLines)
165 {
166     impl_->inputLines_  = inputLines;
167     impl_->currentLine_ = 0;
168 }
169
170 TextInputStream &InteractiveTestHelper::inputStream()
171 {
172     return impl_->inputStream_;
173 }
174
175 TextOutputStream &InteractiveTestHelper::outputStream()
176 {
177     return impl_->outputStream_;
178 }
179
180 void InteractiveTestHelper::checkSession()
181 {
182     impl_->checkOutput();
183     impl_->checkPendingInput();
184 }
185
186 } // namespace test
187 } // namespace gmx