GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/chatutils.cc Lines: 130 130 100.0 %
Date: 2017-11-29 Branches: 223 522 42.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  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
14
TEST_CASE("chatutils replaceVars", "")
57
{
58
10
    setEnv("SDL_VIDEODRIVER", "dummy");
59
60
10
    client = new Client;
61
10
    XML::initXML();
62
10
    SDL_Init(SDL_INIT_VIDEO);
63

40
    VirtFs::mountDirSilent("data", Append_false);
64

40
    VirtFs::mountDirSilent("../data", Append_false);
65
20
    imageHelper = new SDLImageHelper();
66
10
    mainGraphics = new SDLGraphics;
67
68
10
    Dirs::initRootDir();
69
10
    Dirs::initHomeDir();
70
71
10
    ConfigManager::initConfiguration();
72
10
    setConfigDefaults2(config);
73
10
    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
10
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
82
#endif  // USE_SDL2
83
84
10
    theme = new Theme;
85
10
    Theme::selectSkin();
86
87
10
    gui = new Gui();
88
10
    gui->postInit(mainGraphics);
89
90
10
    ActorSprite::load();
91
10
    localPlayer = new LocalPlayer(static_cast<BeingId>(1),
92
10
        BeingTypeId_zero);
93
10
    actorManager = new ActorManager;
94
20
    std::string str;
95
96



70
    SECTION("empty")
97
    {
98
2
        replaceVars(str);
99



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



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



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



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



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



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



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



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

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



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



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

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



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



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



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

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



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

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

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



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



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



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

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



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

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



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

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



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



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



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

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

40
    VirtFs::unmountDirSilent("../data");
257
10
}
258
259
6
TEST_CASE("chatutils textToMe", "")
260
{
261




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




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




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




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

8
}