Line data Source code
1 : // Copyright (c) 2018-2025 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 the string_replace_many() function.
21 : *
22 : * Make sure that it does replace as expected.
23 : */
24 :
25 : // self
26 : //
27 : #include <snapdev/string_replace_many.h>
28 :
29 : #include "catch_main.h"
30 :
31 :
32 : // C++
33 : //
34 : #include <deque>
35 : #include <list>
36 :
37 :
38 : // last include
39 : //
40 : #include <snapdev/poison.h>
41 :
42 :
43 :
44 4 : CATCH_TEST_CASE("string_replace_many", "[string_replace_many][string]")
45 : {
46 4 : CATCH_START_SECTION("string_replace_many: empty input string")
47 : {
48 1 : std::string const empty;
49 4 : std::string result(snapdev::string_replace_many(empty, {{"this", "that"}}));
50 1 : CATCH_REQUIRE(empty == result);
51 :
52 : // is the following a bug?
53 : // (i.e. I am thinking it should return "once")
54 : //
55 4 : result = snapdev::string_replace_many(empty, {{"", "once"}});
56 1 : CATCH_REQUIRE(empty == result);
57 1 : }
58 4 : CATCH_END_SECTION()
59 :
60 4 : CATCH_START_SECTION("string_replace_many: replace one string")
61 : {
62 6 : std::string result(snapdev::string_replace_many(std::string("this is replaced"), {{"this", "that"}}));
63 1 : CATCH_REQUIRE("that is replaced" == result);
64 :
65 6 : result = snapdev::string_replace_many(std::string("place this in the middle"), {{"this", "that"}});
66 1 : CATCH_REQUIRE("place that in the middle" == result);
67 :
68 6 : result = snapdev::string_replace_many(std::string("place at the end"), {{"end", "finish"}});
69 1 : CATCH_REQUIRE("place at the finish" == result);
70 1 : }
71 4 : CATCH_END_SECTION()
72 :
73 4 : CATCH_START_SECTION("string_replace_many: replace many strings")
74 : {
75 6 : std::string result(snapdev::string_replace_many(std::string("this is replaced isn't it?"), {{"is", "XXX"}}));
76 1 : CATCH_REQUIRE("thXXX XXX replaced XXXn't it?" == result);
77 :
78 6 : result = snapdev::string_replace_many(std::string("XXstartXXmiddle endXX"), {{"XX", "*"}});
79 1 : CATCH_REQUIRE("*start*middle end*" == result);
80 :
81 6 : result = snapdev::string_replace_many(std::string("glueglueglueglue"), {{"glue", " WORD "}});
82 1 : CATCH_REQUIRE(" WORD WORD WORD WORD " == result);
83 :
84 9 : result = snapdev::string_replace_many(std::string("this is something that is eaten"), {{"is", "at"}, {"th", "z"}, {"eat", ""}, {"en", "fr"}});
85 1 : CATCH_REQUIRE("zat at somezing zat at fr" == result);
86 1 : }
87 4 : CATCH_END_SECTION()
88 :
89 4 : CATCH_START_SECTION("string_replace_many: dashing string")
90 : {
91 6 : std::string result(snapdev::string_replace_many(std::string("dash this string here"), {{"", "-"}}));
92 1 : CATCH_REQUIRE("-d-a-s-h- -t-h-i-s- -s-t-r-i-n-g- -h-e-r-e" == result);
93 1 : }
94 4 : CATCH_END_SECTION()
95 14 : }
96 :
97 :
98 :
99 : // vim: ts=4 sw=4 et
|