Merge branch release-4-6 into release-5-0
[alexxy/gromacs.git] / src / gromacs / onlinehelp / tests / helpformat.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, 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 help string formatting functions and classes.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_onlinehelp
41  */
42 #include <gtest/gtest.h>
43
44 #include "gromacs/onlinehelp/helpformat.h"
45
46 #include "testutils/stringtest.h"
47
48 namespace
49 {
50
51 //! Simple test string for wrapping.
52 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
53 //! Test string for wrapping with embedded line breaks.
54 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
55
56 /********************************************************************
57  * Tests for TextTableFormatter
58  */
59
60 class TextTableFormatterTest : public gmx::test::StringTestBase
61 {
62     public:
63         void setupStandardColumns();
64
65         gmx::TextTableFormatter formatter_;
66 };
67
68 void TextTableFormatterTest::setupStandardColumns()
69 {
70     formatter_.addColumn("Col1", 4, false);
71     formatter_.addColumn("Col2", 4, false);
72     formatter_.addColumn("Col3Wrap", 14, true);
73     formatter_.addColumn("Col4Wrap", 14, true);
74 }
75
76 TEST_F(TextTableFormatterTest, HandlesBasicCase)
77 {
78     setupStandardColumns();
79     formatter_.clear();
80     formatter_.addColumnLine(0, "foo");
81     formatter_.addColumnLine(1, "bar");
82     formatter_.addColumnLine(2, g_wrapText);
83     formatter_.addColumnLine(3, g_wrapText2);
84     checkText(formatter_.formatRow(), "FormattedTable");
85 }
86
87 TEST_F(TextTableFormatterTest, HandlesEmptyColumnTitles)
88 {
89     formatter_.addColumn(NULL, 4, false);
90     formatter_.addColumn("", 4, false);
91     formatter_.addColumn(NULL, 14, true);
92     formatter_.addColumn("", 14, true);
93
94     formatter_.clear();
95     formatter_.addColumnLine(0, "foo");
96     formatter_.addColumnLine(1, "bar");
97     formatter_.addColumnLine(2, g_wrapText);
98     formatter_.addColumnLine(3, g_wrapText2);
99     checkText(formatter_.formatRow(), "FormattedTable");
100 }
101
102 TEST_F(TextTableFormatterTest, HandlesIndentation)
103 {
104     setupStandardColumns();
105     formatter_.setFirstColumnIndent(4);
106     formatter_.clear();
107     formatter_.addColumnLine(0, "foo");
108     formatter_.addColumnLine(1, "bar");
109     formatter_.addColumnLine(2, g_wrapText);
110     formatter_.addColumnLine(3, g_wrapText2);
111     checkText(formatter_.formatRow(), "FormattedTable");
112 }
113
114 TEST_F(TextTableFormatterTest, HandlesOverflowingLines)
115 {
116     setupStandardColumns();
117     formatter_.clear();
118     formatter_.addColumnLine(0, "foobar");
119     formatter_.addColumnLine(1, "barfoo");
120     formatter_.addColumnLine(2, g_wrapText);
121     formatter_.addColumnLine(3, g_wrapText2);
122     checkText(formatter_.formatRow(), "FormattedTable");
123     formatter_.clear();
124     formatter_.addColumnLine(0, "foobar");
125     formatter_.addColumnLine(1, "barfoo");
126     formatter_.setColumnFirstLineOffset(1, 1);
127     formatter_.addColumnLine(2, g_wrapText);
128     formatter_.addColumnLine(3, g_wrapText2);
129     checkText(formatter_.formatRow(), "FormattedRow2");
130     formatter_.clear();
131     formatter_.addColumnLine(0, "foobar");
132     formatter_.addColumnLine(1, "barfoo");
133     formatter_.setColumnFirstLineOffset(1, 1);
134     formatter_.addColumnLine(2, g_wrapText);
135     formatter_.setColumnFirstLineOffset(2, 2);
136     formatter_.addColumnLine(3, g_wrapText2);
137     checkText(formatter_.formatRow(), "FormattedRow3");
138     // Test a case where a column value overflows even the next column.
139     formatter_.addColumnLine(0, "foobarfoobar");
140     formatter_.addColumnLine(1, "barfoobarfoo");
141     formatter_.addColumnLine(2, g_wrapText);
142     formatter_.addColumnLine(3, g_wrapText2);
143     checkText(formatter_.formatRow(), "FormattedRow4");
144 }
145
146 TEST_F(TextTableFormatterTest, HandlesLastColumnFolding)
147 {
148     setupStandardColumns();
149     formatter_.setFoldLastColumnToNextLine(4);
150     formatter_.clear();
151     formatter_.addColumnLine(0, "foo");
152     formatter_.addColumnLine(1, "bar");
153     formatter_.addColumnLine(2, "text");
154     formatter_.addColumnLine(3, g_wrapText);
155     checkText(formatter_.formatRow(), "FormattedTable");
156     formatter_.clear();
157     formatter_.addColumnLine(0, "foo");
158     formatter_.addColumnLine(1, "bar");
159     formatter_.addColumnLine(2, "text");
160     formatter_.addColumnLine(3, g_wrapText2);
161     checkText(formatter_.formatRow(), "FormattedRow2");
162     formatter_.clear();
163     formatter_.addColumnLine(0, "foo");
164     formatter_.addColumnLine(1, "bar");
165     formatter_.addColumnLine(2, "text");
166     formatter_.addColumnLine(3, "A quick brown fox jumps");
167     checkText(formatter_.formatRow(), "FormattedRow3");
168 }
169
170 TEST_F(TextTableFormatterTest, HandlesEmptyColumns)
171 {
172     setupStandardColumns();
173     formatter_.clear();
174     formatter_.addColumnLine(0, "foo");
175     formatter_.addColumnLine(1, "bar");
176     formatter_.addColumnLine(3, "Text");
177     checkText(formatter_.formatRow(), "FormattedTable");
178     formatter_.clear();
179     formatter_.addColumnLine(0, "foo");
180     formatter_.addColumnLine(1, "bar");
181     formatter_.setColumnFirstLineOffset(2, 1);
182     formatter_.addColumnLine(3, "Text");
183     checkText(formatter_.formatRow(), "FormattedRow2");
184     formatter_.clear();
185     formatter_.addColumnLine(0, "foo");
186     formatter_.addColumnLine(1, "bar");
187     formatter_.addColumnLine(2, "");
188     formatter_.setColumnFirstLineOffset(2, 1);
189     formatter_.addColumnLine(3, "Text");
190     checkText(formatter_.formatRow(), "FormattedRow3");
191 }
192
193 } // namespace