GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/chatutils.cc Lines: 130 130 100.0 %
Date: 2018-07-14 Branches: 220 518 42.5 %

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



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



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



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



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



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



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



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



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



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

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



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



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

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



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



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



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

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



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

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

2
        const bool correct = str == "test player1,player2" ||
184
2
            str == "test player2,player1";
185



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



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



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

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



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

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



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

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



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



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



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

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

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




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




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




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




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

4
}