summaryrefslogtreecommitdiffstats
path: root/Test/ConditionalReplaceButKeepSigns.cpp
blob: 99c3bcb7df305f8bf7e299a0a1c296bd9af1899e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <Util.h>
#include "Catch2.h"

// Test that putting in an empty string returns an empty string
TEST_CASE(__FILE__"/EmptyInputString", "[]")
{
    // Setup
    const std::string in = "";
    const std::string expected = "";

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(in, "findme", "putme");

    // Verify
    REQUIRE(result == expected);
}

// Tests that simple replacing works
TEST_CASE(__FILE__"/SimpleReplacing", "[]")
{
    // Setup
    const std::string in = "Hello $user, you must be $user, so i am calling you $user.";
    const std::string expected = "Hello bob, you must be bob, so i am calling you bob.";

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(in, "$user", "bob");

    // Verify
    REQUIRE(result == expected);
}

// Find-string is empty
TEST_CASE(__FILE__"/FindEmpty", "[]")
{
    // Setup
    const std::string in = "Hello $user, you must be $user, so i am calling you $user.";
    const std::string expected = in;

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(in, "", "bob");

    // Verify
    REQUIRE(result == expected);
}

// Sub-string is empty
TEST_CASE(__FILE__"/SubEmpty", "[]")
{
    // Setup
    const std::string in = "Hello $user, you must be $user, so i am calling you $user.";
    const std::string expected = "Hello , you must be , so i am calling you .";

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(in, "$user", "");

    // Verify
    REQUIRE(result == expected);
}

// Usual use-case
TEST_CASE(__FILE__"/KeepSigns", "[]")
{
    // Setup
    const std::string in = "Hello Alice, lowercase alice, WTF ALICE?!";
    const std::string expected = "Hello Bob, lowercase bob, WTF BOB?!";

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(in, "alice", "bob");

    // Verify
    REQUIRE(result == expected);
}

// Sub-string is longer than find-string
TEST_CASE(__FILE__"/ExtrapolateSigns", "[]")
{
    // Setup
    const std::string in = "Hi Alice. HI Alice. hi Alice. hI Alice. HIalice. hiALICE.";
    const std::string expected = "Hello Alice. HELLO Alice. hello Alice. hELLO Alice. HElloalice. heLLOALICE.";

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(in, "hi", "hello");

    // Verify
    REQUIRE(result == expected);
}

// Tests that not replacing anything (by callback) returns the original string
TEST_CASE(__FILE__"/RejectingAllReplacementsChangesNothing", "[]")
{
    // Setup
    const std::string in = "Hello, i am a generic string with a lot of e's. It would be a shame of any of these e's were lost, wouldn't it?";
    const std::string expected = in;

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(
            in,
            "e",
            "hello",
            [](auto, auto, auto){ return false; } // Reject all replacements
    );

    // Verify
    REQUIRE(result == expected);
}

// Tests that the callback returns the correct index for a finding
TEST_CASE(__FILE__"/Callback_Index_Matches", "[]")
{
    // Setup
    const std::string in = "Hello, banana.";
    const std::string expected = in;

    // Exercise
    Util::ConditionalReplaceButKeepSigns(
            in,
            "banana",
            "hello",
            [](const std::string& original, const std::string& finding, const std::size_t index) -> bool {
                REQUIRE(index == 7);
                return true;
            }
    );
}

// Tests that the callback returns the correct substring of a finding
TEST_CASE(__FILE__"/Callback_Finding_Matches", "[]")
{
    // Setup
    const std::string in = "Hello, BANAna.";
    const std::string expected = in;

    // Exercise
    Util::ConditionalReplaceButKeepSigns(
            in,
            "banana",
            "hello",
            [](const std::string& original, const std::string& finding, const std::size_t index) -> bool {
                REQUIRE(finding == "BANAna");
                return true;
            }
    );
}

// Tests that the callback function works, here we will be rejecting every other finding
TEST_CASE(__FILE__"/Callback_Conditional_Rejecting_Works", "[]")
{
    // Setup
    const std::string in = "ding ding Ding Ding Ding Ding DING DING";
    const std::string expected = "ding dong Ding Dong Ding Dong DING DONG";

    std::size_t counter = 1; // Start one cycle off
    std::size_t* ptrCounter = &counter;

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(
            in,
            "ding",
            "dong",
            [ptrCounter](const std::string& original, const std::string& finding, const std::size_t index) -> bool {
                return (++*ptrCounter) % 2;
            }
    );

    // Verify
    REQUIRE(expected == result);
}

// Tests that replacing symbols works... Why ever you would use this method for this...
TEST_CASE(__FILE__"/ReplaceSymbols", "[]")
{
    // Setup
    const std::string in = "Hello. I like you.";
    const std::string expected = "Hello! I like you!";

    // Exercise
    const std::string result = Util::ConditionalReplaceButKeepSigns(
            in,
            ".",
            "!"
    );

    // Verify
    REQUIRE(result == expected);
}

// The callback ALWAYS receives the same, identical 'original string' value on each call, without any replacements made to it.
TEST_CASE(__FILE__"/Original-value_on_callback_is_correct", "[]")
{
    // Setup
    const std::string in = "I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn't really happy.”";

    // Exercise & Verify
    const std::string result = Util::ConditionalReplaceButKeepSigns(
            in,
            "happy",
            "",
            [in](const std::string& original, const std::string& finding, const std::size_t index) -> bool {
                REQUIRE(original == in);
                return true;
            }
    );
}