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