Line data Source code
1 : // Copyright (c) 2018-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdev
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : /** \file
21 : * \brief Verify that the trim_string() function works.
22 : *
23 : * This file implements tests for the trim_string() function.
24 : */
25 :
26 : // self
27 : //
28 : #include <snapdev/trim_string.h>
29 :
30 : #include "catch_main.h"
31 :
32 :
33 : // last include
34 : //
35 : #include <snapdev/poison.h>
36 :
37 :
38 : namespace
39 : {
40 :
41 :
42 :
43 : struct strings_t
44 : {
45 : char const * f_original = nullptr;
46 :
47 : char const * f_start = nullptr;
48 : char const * f_end = nullptr;
49 : char const * f_inside = nullptr;
50 : char const * f_start_and_end = nullptr;
51 : char const * f_start_and_inside = nullptr;
52 : char const * f_inside_and_end = nullptr;
53 : char const * f_all = nullptr;
54 :
55 : char const * f_spaces_start = nullptr;
56 : char const * f_spaces_end = nullptr;
57 : char const * f_spaces_inside = nullptr;
58 : char const * f_spaces_start_and_end = nullptr;
59 : char const * f_spaces_start_and_inside = nullptr;
60 : char const * f_spaces_inside_and_end = nullptr;
61 : char const * f_spaces_all = nullptr;
62 : };
63 :
64 : constexpr strings_t const g_strings[] =
65 : {
66 : {
67 : /* orginal */ " do \t not \f trim \r anything \n here ",
68 :
69 : /* start */ "do \t not \f trim \r anything \n here ",
70 : /* end */ " do \t not \f trim \r anything \n here",
71 : /* inside */ " do not trim anything here ",
72 : /* s/e */ "do \t not \f trim \r anything \n here",
73 : /* s/i */ "do not trim anything here ",
74 : /* i/e */ " do not trim anything here",
75 : /* all */ "do not trim anything here",
76 :
77 : /* start */ "do \t not \f trim \r anything \n here ",
78 : /* end */ " do \t not \f trim \r anything \n here",
79 : /* inside */ " do \t not \f trim \r anything \n here ",
80 : /* s/e */ "do \t not \f trim \r anything \n here",
81 : /* s/i */ "do \t not \f trim \r anything \n here ",
82 : /* i/e */ " do \t not \f trim \r anything \n here",
83 : /* all */ "do \t not \f trim \r anything \n here",
84 : },
85 : {
86 : /* orginal */ " do \t not trim \r anything \n here again!\r\n",
87 :
88 : /* start */ "do \t not trim \r anything \n here again!\r\n",
89 : /* end */ " do \t not trim \r anything \n here again!",
90 : /* inside */ " do not trim anything here again! ",
91 : /* s/e */ "do \t not trim \r anything \n here again!",
92 : /* s/i */ "do not trim anything here again! ",
93 : /* i/e */ " do not trim anything here again!",
94 : /* all */ "do not trim anything here again!",
95 :
96 : /* start */ "do \t not trim \r anything \n here again!\r\n",
97 : /* end */ " do \t not trim \r anything \n here again!\r\n",
98 : /* inside */ " do \t not trim \r anything \n here again!\r\n",
99 : /* s/e */ "do \t not trim \r anything \n here again!\r\n",
100 : /* s/i */ "do \t not trim \r anything \n here again!\r\n",
101 : /* i/e */ " do \t not trim \r anything \n here again!\r\n",
102 : /* all */ "do \t not trim \r anything \n here again!\r\n",
103 : },
104 : {
105 : /* orginal */ "nothing-to-remove",
106 :
107 : /* start */ "nothing-to-remove",
108 : /* end */ "nothing-to-remove",
109 : /* inside */ "nothing-to-remove",
110 : /* s/e */ "nothing-to-remove",
111 : /* s/i */ "nothing-to-remove",
112 : /* i/e */ "nothing-to-remove",
113 : /* all */ "nothing-to-remove",
114 :
115 : /* start */ "nothing-to-remove",
116 : /* end */ "nothing-to-remove",
117 : /* inside */ "nothing-to-remove",
118 : /* s/e */ "nothing-to-remove",
119 : /* s/i */ "nothing-to-remove",
120 : /* i/e */ "nothing-to-remove",
121 : /* all */ "nothing-to-remove",
122 : },
123 : };
124 :
125 :
126 : }
127 : // no name namespace
128 :
129 :
130 :
131 3 : CATCH_TEST_CASE("trim_string", "[string]")
132 : {
133 2 : CATCH_START_SECTION("trim_string many cases")
134 : {
135 4 : for(auto const & p : g_strings)
136 : {
137 6 : std::string const original(p.f_original);
138 :
139 : // no trimming
140 : //
141 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, false) == p.f_original);
142 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, false, false, std::string(" ")) == p.f_original);
143 :
144 : // start trimming
145 : //
146 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, false) == p.f_start);
147 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, false, false, std::string(" ")) == p.f_spaces_start);
148 :
149 : // end trimming
150 : //
151 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, true) == p.f_end);
152 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, true, false, std::string(" ")) == p.f_spaces_end);
153 :
154 : // inside trimming
155 : //
156 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, false, true) == p.f_inside);
157 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, false, true, std::string(" ")) == p.f_spaces_inside);
158 :
159 : // start/end trimming
160 : //
161 3 : CATCH_REQUIRE(snapdev::trim_string(original) == p.f_start_and_end);
162 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, true, false, std::string(" ")) == p.f_spaces_start_and_end);
163 :
164 : // start/inside trimming
165 : //
166 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, false, true) == p.f_start_and_inside);
167 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, false, true, std::string(" ")) == p.f_spaces_start_and_inside);
168 :
169 : // inside/end trimming
170 : //
171 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, true, true) == p.f_inside_and_end);
172 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, true, true, std::string(" ")) == p.f_spaces_inside_and_end);
173 :
174 : // all trimming
175 : //
176 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, true, true) == p.f_all);
177 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, true, true, std::string(" ")) == p.f_spaces_all);
178 : }
179 : }
180 : CATCH_END_SECTION()
181 7 : }
182 :
183 :
184 :
185 : // vim: ts=4 sw=4 et
|