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