clang-tidy: google tests applicable
[alexxy/gromacs.git] / src / gromacs / utility / tests / textreader.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,2018, 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 gmx::TextReader.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \ingroup module_utility
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/utility/textreader.h"
45
46 #include <functional>
47 #include <string>
48 #include <vector>
49
50 #include <gmock/gmock.h>
51 #include <gtest/gtest.h>
52
53 #include "gromacs/utility/basedefinitions.h"
54 #include "gromacs/utility/stringstream.h"
55 #include "gromacs/utility/stringutil.h"
56
57 #include "testutils/refdata.h"
58 #include "testutils/stringtest.h"
59
60 namespace gmx
61 {
62 namespace test
63 {
64 namespace
65 {
66
67 //! Convenience name.
68 using Container = std::vector<std::string>;
69 //! Convenience type for callbacks.
70 using TestCallbackFunc = void(*)(TextReader &);
71
72 //! Helper struct.
73 struct TextReaderTestParams
74 {
75     //! Input data.
76     const Container  input;
77     //! Callback to configure the reader with the behaviour being tested.
78     TestCallbackFunc callback;
79     //! Output to expect from the configured reader acting on the \c input.
80     const Container  expectedOutput;
81 };
82
83 //! Test fixture.
84 class TextReaderTest : public ::testing::TestWithParam<TextReaderTestParams>
85 {
86 };
87
88 TEST_P(TextReaderTest, UsingDifferentConfigurations)
89 {
90     const auto &params = GetParam();
91
92     // Prepare the reader with the input lines.
93     StringInputStream stream(params.input);
94     TextReader        reader(&stream);
95     // Configure the intended reading behaviour.
96     params.callback(reader);
97     // Read the input and store what is read.
98     Container   readLines;
99     std::string line;
100     while (reader.readLine(&line))
101     {
102         readLines.push_back(line);
103     }
104     // Check the results
105     EXPECT_THAT(readLines, ::testing::ElementsAreArray(params.expectedOutput));
106 }
107
108 //! Test input data. Some configurations will remove comments delimited by '#'.
109 const Container g_inputs =
110 {
111     "",
112     " \t ",
113     "expected text",
114     " expected text ",
115     "expected text \t",
116     " \t expected text",
117     " \t expected text \t",
118     "expected text#",
119     "expected text\t #",
120     "expected text# ",
121     "expected text   # not expected ",
122     "#",
123     "\t #",
124     "   # not expected ",
125 };
126
127 /*! \brief A case of expected output data that is produced from two
128  * different configurations.
129  *
130  * Note that the implementation of StringInputStream joins the input
131  * container with "\n", so the inputs are always changed before being
132  * read. The name of this variable reflects that TextReader does not
133  * change them during reading. */
134 const Container g_unchangedOutputs =
135 {
136     "\n",
137     " \t \n",
138     "expected text\n",
139     " expected text \n",
140     "expected text \t\n",
141     " \t expected text\n",
142     " \t expected text \t\n",
143     "expected text#\n",
144     "expected text\t #\n",
145     "expected text# \n",
146     "expected text   # not expected \n",
147     "#\n",
148     "\t #\n",
149     "   # not expected \n",
150 };
151 INSTANTIATE_TEST_CASE_P(ParsesLinesDifferently, TextReaderTest,
152                             ::testing::Values(TextReaderTestParams {
153                                                   g_inputs,
154                                                   [](TextReader &r)
155                                                   {
156                                                       GMX_UNUSED_VALUE(r);
157                                                   },
158                                                   g_unchangedOutputs
159                                               },
160                                               TextReaderTestParams {
161                                                   g_inputs,
162                                                   [](TextReader &r)
163                                                   {
164                                                       r.setTrimLeadingWhiteSpace(true);
165                                                   },
166                                                   { "",
167                                                     "",
168                                                     "expected text\n",
169                                                     "expected text \n",
170                                                     "expected text \t\n",
171                                                     "expected text\n",
172                                                     "expected text \t\n",
173                                                     "expected text#\n",
174                                                     "expected text\t #\n",
175                                                     "expected text# \n",
176                                                     "expected text   # not expected \n",
177                                                     "#\n",
178                                                     "#\n",
179                                                     "# not expected \n", }
180                                               },
181                                               TextReaderTestParams {
182                                                   g_inputs,
183                                                   [](TextReader &r)
184                                                   {
185                                                       r.setTrimTrailingWhiteSpace(true);
186                                                   },
187                                                   { "",
188                                                     "",
189                                                     "expected text",
190                                                     " expected text",
191                                                     "expected text",
192                                                     " \t expected text",
193                                                     " \t expected text",
194                                                     "expected text#",
195                                                     "expected text\t #",
196                                                     "expected text#",
197                                                     "expected text   # not expected",
198                                                     "#",
199                                                     "\t #",
200                                                     "   # not expected", }
201                                               },
202                                               TextReaderTestParams {
203                                                   g_inputs,
204                                                   [](TextReader &r)
205                                                   {
206                                                       r.setTrimTrailingWhiteSpace(true);
207                                                       r.setTrimLeadingWhiteSpace(true);
208                                                   },
209                                                   { "",
210                                                     "",
211                                                     "expected text",
212                                                     "expected text",
213                                                     "expected text",
214                                                     "expected text",
215                                                     "expected text",
216                                                     "expected text#",
217                                                     "expected text\t #",
218                                                     "expected text#",
219                                                     "expected text   # not expected",
220                                                     "#",
221                                                     "#",
222                                                     "# not expected", }
223                                               },
224                                               TextReaderTestParams {
225                                                   g_inputs,
226                                                   [](TextReader &r)
227                                                   {
228                                                       r.setTrimTrailingComment(true, '#');
229                                                   },
230                                                   { "\n",
231                                                     " \t \n",
232                                                     "expected text\n",
233                                                     " expected text \n",
234                                                     "expected text \t\n",
235                                                     " \t expected text\n",
236                                                     " \t expected text \t\n",
237                                                     "expected text",
238                                                     "expected text\t ",
239                                                     "expected text",
240                                                     "expected text   ",
241                                                     "",
242                                                     "\t ",
243                                                     "   ", }
244                                               },
245                                               TextReaderTestParams {
246                                                   g_inputs,
247                                                   [](TextReader &r)
248                                                   {
249                                                       r.setTrimTrailingComment(true, '#');
250                                                       r.setTrimTrailingComment(false, 0);
251                                                   },
252                                                   g_unchangedOutputs
253                                               },
254                                               TextReaderTestParams {
255                                                   g_inputs,
256                                                   [](TextReader &r)
257                                                   {
258                                                       r.setTrimTrailingComment(true, '#');
259                                                       r.setTrimTrailingWhiteSpace(true);
260                                                   },
261                                                   { "",
262                                                     "",
263                                                     "expected text",
264                                                     " expected text",
265                                                     "expected text",
266                                                     " \t expected text",
267                                                     " \t expected text",
268                                                     "expected text",
269                                                     "expected text",
270                                                     "expected text",
271                                                     "expected text",
272                                                     "",
273                                                     "",
274                                                     "",   }
275                                               }
276                                               ));
277
278 } // namespace
279 } // namespace test
280 } // namespace gmx