2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017,2018,2019,2021, 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.
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.
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.
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.
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.
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.
37 * Tests for gmx::TextReader.
39 * \author Mark Abraham <mark.j.abraham@gmail.com>
40 * \ingroup module_utility
44 #include "gromacs/utility/textreader.h"
50 #include <gmock/gmock.h>
51 #include <gtest/gtest.h>
53 #include "gromacs/utility/basedefinitions.h"
54 #include "gromacs/utility/stringstream.h"
55 #include "gromacs/utility/stringutil.h"
57 #include "testutils/refdata.h"
58 #include "testutils/stringtest.h"
68 using Container = std::vector<std::string>;
69 //! Convenience type for callbacks.
70 using TestCallbackFunc = void (*)(TextReader&);
73 struct TextReaderTestParams
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;
84 class TextReaderTest : public ::testing::TestWithParam<TextReaderTestParams>
88 TEST_P(TextReaderTest, UsingDifferentConfigurations)
90 const auto& params = GetParam();
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.
100 while (reader.readLine(&line))
102 readLines.push_back(line);
105 EXPECT_THAT(readLines, ::testing::ElementsAreArray(params.expectedOutput));
108 //! Test input data. Some configurations will remove comments delimited by '#'.
109 const Container g_inputs = {
116 " \t expected text \t",
120 "expected text # not expected ",
126 /*! \brief A case of expected output data that is produced from two
127 * different configurations.
129 * Note that the implementation of StringInputStream joins the input
130 * container with "\n", so the inputs are always changed before being
131 * read. The name of this variable reflects that TextReader does not
132 * change them during reading. */
133 const Container g_unchangedOutputs = {
138 "expected text \t\n",
139 " \t expected text\n",
140 " \t expected text \t\n",
142 "expected text\t #\n",
144 "expected text # not expected \n",
147 " # not expected \n",
149 INSTANTIATE_TEST_SUITE_P(
150 ParsesLinesDifferently,
153 TextReaderTestParams{ g_inputs, [](TextReader& r) { GMX_UNUSED_VALUE(r); }, g_unchangedOutputs },
154 TextReaderTestParams{ g_inputs,
155 [](TextReader& r) { r.setTrimLeadingWhiteSpace(true); },
161 "expected text \t\n",
163 "expected text \t\n",
165 "expected text\t #\n",
167 "expected text # not expected \n",
172 TextReaderTestParams{ g_inputs,
173 [](TextReader& r) { r.setTrimTrailingWhiteSpace(true); },
185 "expected text # not expected",
190 TextReaderTestParams{ g_inputs,
192 r.setTrimTrailingWhiteSpace(true);
193 r.setTrimLeadingWhiteSpace(true);
206 "expected text # not expected",
211 TextReaderTestParams{ g_inputs,
212 [](TextReader& r) { r.setTrimTrailingComment(true, '#'); },
218 "expected text \t\n",
219 " \t expected text\n",
220 " \t expected text \t\n",
229 TextReaderTestParams{ g_inputs,
231 r.setTrimTrailingComment(true, '#');
232 r.setTrimTrailingComment(false, 0);
234 g_unchangedOutputs },
235 TextReaderTestParams{ g_inputs,
237 r.setTrimTrailingComment(true, '#');
238 r.setTrimTrailingWhiteSpace(true);