GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/chatutils.cc Lines: 131 131 100.0 %
Date: 2021-03-17 Branches: 219 518 42.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "unittests/unittests.h"
23
24
#include "client.h"
25
#include "configuration.h"
26
#include "configmanager.h"
27
#include "dirs.h"
28
29
#include "actormanager.h"
30
#include "graphicsmanager.h"
31
#include "party.h"
32
33
#include "being/localplayer.h"
34
35
#include "fs/virtfs/fs.h"
36
37
#include "gui/gui.h"
38
#include "gui/theme.h"
39
40
#include "utils/chatutils.h"
41
#include "utils/delete2.h"
42
#include "utils/env.h"
43
44
#include "render/sdlgraphics.h"
45
46
#include "resources/sdlimagehelper.h"
47
48
PRAGMA48(GCC diagnostic push)
49
PRAGMA48(GCC diagnostic ignored "-Wshadow")
50
#ifndef USE_SDL2
51
#include <SDL.h>
52
#endif  // USE_SDL2
53
PRAGMA48(GCC diagnostic pop)
54
55
#include "debug.h"
56
57
7
TEST_CASE("chatutils replaceVars", "")
58
{
59
5
    setEnv("SDL_VIDEODRIVER", "dummy");
60
61
5
    client = new Client;
62
5
    XML::initXML();
63
5
    SDL_Init(SDL_INIT_VIDEO);
64
15
    VirtFs::mountDirSilent("data", Append_false);
65
15
    VirtFs::mountDirSilent("../data", Append_false);
66
10
    imageHelper = new SDLImageHelper();
67
5
    mainGraphics = new SDLGraphics;
68
69
5
    Dirs::initRootDir();
70
5
    Dirs::initHomeDir();
71
72
5
    ConfigManager::initConfiguration();
73
5
    setConfigDefaults2(config);
74
5
    setBrandingDefaults(branding);
75
76
#ifdef USE_SDL2
77
5
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
78
        GraphicsManager::createWindow(640, 480, 0,
79
5
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
80
#else  // USE_SDL2
81
82
    GraphicsManager::createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
83
#endif  // USE_SDL2
84
85
5
    theme = new Theme;
86
5
    Theme::selectSkin();
87
88
5
    gui = new Gui();
89
5
    gui->postInit(mainGraphics);
90
91
5
    ActorSprite::load();
92
5
    localPlayer = new LocalPlayer(static_cast<BeingId>(1),
93
5
        BeingTypeId_zero);
94
5
    actorManager = new ActorManager;
95
10
    std::string str;
96
97



35
    SECTION("empty")
98
    {
99
1
        replaceVars(str);
100



4
        REQUIRE(str.empty());
101
102
1
        str = "test line";
103
1
        replaceVars(str);
104



4
        REQUIRE(str == "test line");
105
106
1
        str = "test <PLAYER>";
107
1
        replaceVars(str);
108



4
        REQUIRE(str == "test ");
109
110
1
        str = "test <MONSTER>";
111
1
        replaceVars(str);
112



4
        REQUIRE(str == "test ");
113
114
1
        str = "test <PEOPLE>";
115
1
        replaceVars(str);
116



4
        REQUIRE(str == "test ");
117
118
1
        str = "test <PARTY>";
119
1
        replaceVars(str);
120



4
        REQUIRE(str == "test ");
121
122
1
        str = "test <SOMETHING>";
123
1
        replaceVars(str);
124



4
        REQUIRE(str == "test <SOMETHING>");
125
    }
126
127



35
    SECTION("player")
128
    {
129
        Being *player1 = Being::createBeing(static_cast<BeingId>(2),
130
            ActorType::Player,
131
            BeingTypeId_zero,
132
1
            nullptr);
133

3
        player1->setName("player1");
134
2
        actorManager->mActors.insert(player1);
135
136
1
        localPlayer->setTarget(player1);
137
1
        str = "test <PLAYER>";
138
1
        replaceVars(str);
139



4
        REQUIRE(str == "test player1");
140
    }
141
142



35
    SECTION("monster")
143
    {
144
        Being *const monster = Being::createBeing(static_cast<BeingId>(3),
145
            ActorType::Monster,
146
            BeingTypeId_zero,
147
1
            nullptr);
148

3
        monster->setName("monster1");
149
2
        actorManager->mActors.insert(monster);
150
151
1
        localPlayer->setTarget(monster);
152
1
        str = "test <MONSTER>";
153
1
        replaceVars(str);
154



4
        REQUIRE(str == "test monster1");
155
    }
156
157



35
    SECTION("people")
158
    {
159
2
        actorManager->mActors.insert(localPlayer);
160
1
        str = "test <PEOPLE>";
161
1
        replaceVars(str);
162



4
        REQUIRE(str == "test ");
163
164
        Being *const player1 = Being::createBeing(static_cast<BeingId>(2),
165
            ActorType::Player,
166
            BeingTypeId_zero,
167
1
            nullptr);
168

3
        player1->setName("player1");
169
2
        actorManager->mActors.insert(player1);
170
171
1
        str = "test <PEOPLE>";
172
1
        replaceVars(str);
173



4
        REQUIRE(str == "test player1");
174
175
        Being *const player2 = Being::createBeing(static_cast<BeingId>(4),
176
            ActorType::Player,
177
            BeingTypeId_zero,
178
1
            nullptr);
179

3
        player2->setName("player2");
180
2
        actorManager->mActors.insert(player2);
181
182
1
        str = "test <PEOPLE>";
183
1
        replaceVars(str);
184

1
        const bool correct = str == "test player1,player2" ||
185
1
            str == "test player2,player1";
186



4
        REQUIRE(correct == true);
187
    }
188
189



35
    SECTION("party")
190
    {
191
2
        actorManager->mActors.insert(localPlayer);
192
193
1
        Party *const party1 = Party::getParty(1);
194
1
        localPlayer->setParty(party1);
195
196
1
        str = "test <PARTY>";
197
1
        replaceVars(str);
198



4
        REQUIRE(str == "test ");
199
200
        Being *const player1 = Being::createBeing(static_cast<BeingId>(2),
201
            ActorType::Player,
202
            BeingTypeId_zero,
203
1
            nullptr);
204

3
        player1->setName("player1");
205
2
        actorManager->mActors.insert(player1);
206
1
        player1->setParty(party1);
207
208
1
        str = "test <PARTY>";
209
1
        replaceVars(str);
210



4
        REQUIRE(str == "test player1");
211
212
        Being *const player2 = Being::createBeing(static_cast<BeingId>(4),
213
            ActorType::Player,
214
            BeingTypeId_zero,
215
1
            nullptr);
216

3
        player2->setName("player2");
217
2
        actorManager->mActors.insert(player2);
218
1
        player2->setParty(party1);
219
220
1
        str = "test <PARTY>";
221
1
        replaceVars(str);
222



4
        REQUIRE(str == "test player1,player2");
223
224
1
        Party *const party2 = Party::getParty(2);
225
226
        Being *const player3 = Being::createBeing(static_cast<BeingId>(5),
227
            ActorType::Player,
228
            BeingTypeId_zero,
229
1
            nullptr);
230

3
        player3->setName("player3");
231
2
        actorManager->mActors.insert(player3);
232
1
        player3->setParty(party2);
233
234
1
        str = "test <PARTY>";
235
1
        replaceVars(str);
236



4
        REQUIRE(str == "test player1,player2");
237
238
1
        player3->setParty(party1);
239
240
1
        str = "test <PARTY>";
241
1
        replaceVars(str);
242



4
        REQUIRE(str == "test player1,player2,player3");
243
244
1
        player2->setParty(party2);
245
246
1
        str = "test <PARTY>";
247
1
        replaceVars(str);
248



4
        REQUIRE(str == "test player1,player3");
249
250
1
        Party::clearParties();
251
    }
252
253
5
    delete2(actorManager)
254
5
    delete2(localPlayer)
255
5
    delete2(client)
256

15
    VirtFs::unmountDirSilent("data");
257

20
    VirtFs::unmountDirSilent("../data");
258
5
}
259
260
3
TEST_CASE("chatutils textToMe", "")
261
{
262




8
    REQUIRE(textToMe("") == "**");
263




8
    REQUIRE(textToMe("123") == "*123*");
264




8
    REQUIRE(textToMe("*") == "***");
265




8
    REQUIRE(textToMe("test line") == "*test line*");
266

4
}