2 * This file is part of the GROMACS molecular simulation package.
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.
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 ¶ms = 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 =
117 " \t expected text \t",
121 "expected text # not expected ",
127 /*! \brief A case of expected output data that is produced from two
128 * different configurations.
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 =
140 "expected text \t\n",
141 " \t expected text\n",
142 " \t expected text \t\n",
144 "expected text\t #\n",
146 "expected text # not expected \n",
149 " # not expected \n",
151 INSTANTIATE_TEST_CASE_P(ParsesLinesDifferently, TextReaderTest,
152 ::testing::Values(TextReaderTestParams {
160 TextReaderTestParams {
164 r.setTrimLeadingWhiteSpace(true);
170 "expected text \t\n",
172 "expected text \t\n",
174 "expected text\t #\n",
176 "expected text # not expected \n",
179 "# not expected \n", }
181 TextReaderTestParams {
185 r.setTrimTrailingWhiteSpace(true);
197 "expected text # not expected",
202 TextReaderTestParams {
206 r.setTrimTrailingWhiteSpace(true);
207 r.setTrimLeadingWhiteSpace(true);
219 "expected text # not expected",
224 TextReaderTestParams {
228 r.setTrimTrailingComment(true, '#');
234 "expected text \t\n",
235 " \t expected text\n",
236 " \t expected text \t\n",
245 TextReaderTestParams {
249 r.setTrimTrailingComment(true, '#');
250 r.setTrimTrailingComment(false, 0);
254 TextReaderTestParams {
258 r.setTrimTrailingComment(true, '#');
259 r.setTrimTrailingWhiteSpace(true);