Line data Source code
1 : // Copyright (c) 2018-2024 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 3 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
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : /** \file
20 : * \brief Verify that the trim_string() function works.
21 : *
22 : * This file implements tests for the trim_string() function.
23 : */
24 :
25 : // self
26 : //
27 : #include <snapdev/trim_string.h>
28 :
29 : #include "catch_main.h"
30 :
31 :
32 : // last include
33 : //
34 : #include <snapdev/poison.h>
35 :
36 :
37 : namespace
38 : {
39 :
40 :
41 :
42 : struct strings_t
43 : {
44 : char const * f_original = nullptr;
45 :
46 : char const * f_start = nullptr;
47 : char const * f_end = nullptr;
48 : char const * f_inside = nullptr;
49 : char const * f_start_and_end = nullptr;
50 : char const * f_start_and_inside = nullptr;
51 : char const * f_inside_and_end = nullptr;
52 : char const * f_all = nullptr;
53 :
54 : char const * f_spaces_start = nullptr;
55 : char const * f_spaces_end = nullptr;
56 : char const * f_spaces_inside = nullptr;
57 : char const * f_spaces_start_and_end = nullptr;
58 : char const * f_spaces_start_and_inside = nullptr;
59 : char const * f_spaces_inside_and_end = nullptr;
60 : char const * f_spaces_all = nullptr;
61 : };
62 :
63 : constexpr strings_t const g_strings[] =
64 : {
65 : {
66 : /* orginal */ " do \t not \f trim \r anything \n here ",
67 :
68 : /* start */ "do \t not \f trim \r anything \n here ",
69 : /* end */ " do \t not \f trim \r anything \n here",
70 : /* inside */ " do not trim anything here ",
71 : /* s/e */ "do \t not \f trim \r anything \n here",
72 : /* s/i */ "do not trim anything here ",
73 : /* i/e */ " do not trim anything here",
74 : /* all */ "do not trim anything here",
75 :
76 : /* start */ "do \t not \f trim \r anything \n here ",
77 : /* end */ " do \t not \f trim \r anything \n here",
78 : /* inside */ " do \t not \f trim \r anything \n here ",
79 : /* s/e */ "do \t not \f trim \r anything \n here",
80 : /* s/i */ "do \t not \f trim \r anything \n here ",
81 : /* i/e */ " do \t not \f trim \r anything \n here",
82 : /* all */ "do \t not \f trim \r anything \n here",
83 : },
84 : {
85 : /* orginal */ " do \t not trim \r anything \n here again!\r\n",
86 :
87 : /* start */ "do \t not trim \r anything \n here again!\r\n",
88 : /* end */ " do \t not trim \r anything \n here again!",
89 : /* inside */ " do not trim anything here again! ",
90 : /* s/e */ "do \t not trim \r anything \n here again!",
91 : /* s/i */ "do not trim anything here again! ",
92 : /* i/e */ " do not trim anything here again!",
93 : /* all */ "do not trim anything here again!",
94 :
95 : /* start */ "do \t not trim \r anything \n here again!\r\n",
96 : /* end */ " do \t not trim \r anything \n here again!\r\n",
97 : /* inside */ " do \t not trim \r anything \n here again!\r\n",
98 : /* s/e */ "do \t not trim \r anything \n here again!\r\n",
99 : /* s/i */ "do \t not trim \r anything \n here again!\r\n",
100 : /* i/e */ " do \t not trim \r anything \n here again!\r\n",
101 : /* all */ "do \t not trim \r anything \n here again!\r\n",
102 : },
103 : {
104 : /* orginal */ "nothing-to-remove",
105 :
106 : /* start */ "nothing-to-remove",
107 : /* end */ "nothing-to-remove",
108 : /* inside */ "nothing-to-remove",
109 : /* s/e */ "nothing-to-remove",
110 : /* s/i */ "nothing-to-remove",
111 : /* i/e */ "nothing-to-remove",
112 : /* all */ "nothing-to-remove",
113 :
114 : /* start */ "nothing-to-remove",
115 : /* end */ "nothing-to-remove",
116 : /* inside */ "nothing-to-remove",
117 : /* s/e */ "nothing-to-remove",
118 : /* s/i */ "nothing-to-remove",
119 : /* i/e */ "nothing-to-remove",
120 : /* all */ "nothing-to-remove",
121 : },
122 : };
123 :
124 :
125 : }
126 : // no name namespace
127 :
128 :
129 :
130 1 : CATCH_TEST_CASE("trim_string", "[string]")
131 : {
132 1 : CATCH_START_SECTION("trim_string many cases")
133 : {
134 4 : for(auto const & p : g_strings)
135 : {
136 6 : std::string const original(p.f_original);
137 :
138 : // no trimming
139 : //
140 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, false) == p.f_original);
141 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, false, false, std::string(" ")) == p.f_original);
142 :
143 : // start trimming
144 : //
145 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, false) == p.f_start);
146 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, false, false, std::string(" ")) == p.f_spaces_start);
147 :
148 : // end trimming
149 : //
150 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, true) == p.f_end);
151 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, true, false, std::string(" ")) == p.f_spaces_end);
152 :
153 : // inside trimming
154 : //
155 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, false, true) == p.f_inside);
156 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, false, true, std::string(" ")) == p.f_spaces_inside);
157 :
158 : // start/end trimming
159 : //
160 3 : CATCH_REQUIRE(snapdev::trim_string(original) == p.f_start_and_end);
161 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, true, false, std::string(" ")) == p.f_spaces_start_and_end);
162 :
163 : // start/inside trimming
164 : //
165 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, false, true) == p.f_start_and_inside);
166 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, false, true, std::string(" ")) == p.f_spaces_start_and_inside);
167 :
168 : // inside/end trimming
169 : //
170 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, true, true) == p.f_inside_and_end);
171 3 : CATCH_REQUIRE(snapdev::trim_string(original, false, true, true, std::string(" ")) == p.f_spaces_inside_and_end);
172 :
173 : // all trimming
174 : //
175 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, true, true) == p.f_all);
176 3 : CATCH_REQUIRE(snapdev::trim_string(original, true, true, true, std::string(" ")) == p.f_spaces_all);
177 3 : }
178 : }
179 1 : CATCH_END_SECTION()
180 1 : }
181 :
182 :
183 :
184 : // vim: ts=4 sw=4 et
|