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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/*
* tests.cpp - run WeeChat tests
*
* Copyright (C) 2014-2022 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
extern "C"
{
#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H
#endif
#include "src/core/weechat.h"
#include "src/core/wee-arraylist.h"
#include "src/core/wee-dir.h"
#include "src/core/wee-hook.h"
#include "src/core/wee-input.h"
#include "src/core/wee-string.h"
#include "src/plugins/plugin.h"
#include "src/gui/gui-main.h"
#include "src/gui/gui-buffer.h"
#include "src/gui/gui-chat.h"
extern void gui_main_init ();
extern void gui_main_loop ();
}
#include "CppUTest/CommandLineTestRunner.h"
#define LOCALE_TESTS "en_US.UTF-8"
#define WEECHAT_TESTS_HOME "./tmp_weechat_test"
/* lib with tests on plugins when autotools is used to compile */
#define WEECHAT_TESTS_PLUGINS_LIB_DEFAULT \
"./tests/.libs/lib_weechat_unit_tests_plugins.so.0.0.0"
/* import tests from libs */
/* core */
IMPORT_TEST_GROUP(CoreArraylist);
IMPORT_TEST_GROUP(CoreCalc);
IMPORT_TEST_GROUP(CoreConfigFile);
IMPORT_TEST_GROUP(CoreCrypto);
IMPORT_TEST_GROUP(CoreDir);
IMPORT_TEST_GROUP(CoreEval);
IMPORT_TEST_GROUP(CoreHashtable);
IMPORT_TEST_GROUP(CoreHdata);
IMPORT_TEST_GROUP(CoreHook);
IMPORT_TEST_GROUP(CoreInfolist);
IMPORT_TEST_GROUP(CoreList);
IMPORT_TEST_GROUP(CoreNetwork);
IMPORT_TEST_GROUP(CoreSecure);
IMPORT_TEST_GROUP(CoreSignal);
IMPORT_TEST_GROUP(CoreString);
IMPORT_TEST_GROUP(CoreUrl);
IMPORT_TEST_GROUP(CoreUtf8);
IMPORT_TEST_GROUP(CoreUtil);
/* GUI */
IMPORT_TEST_GROUP(GuiBarItem);
IMPORT_TEST_GROUP(GuiBarItemCustom);
IMPORT_TEST_GROUP(GuiBarWindow);
IMPORT_TEST_GROUP(GuiBuffer);
IMPORT_TEST_GROUP(GuiChat);
IMPORT_TEST_GROUP(GuiColor);
IMPORT_TEST_GROUP(GuiLine);
IMPORT_TEST_GROUP(GuiNick);
/* scripts */
IMPORT_TEST_GROUP(Scripts);
struct t_gui_buffer *ptr_core_buffer = NULL;
/* recording of messages: to test if a message is actually displayed */
int record_messages = 0;
struct t_arraylist *recorded_messages = NULL;
/*
* Callback for exec_on_files (to remove all files in WeeChat home directory).
*/
void
exec_on_files_cb (void *data, const char *filename)
{
/* make C++ compiler happy */
(void) data;
unlink (filename);
}
/*
* Callback for any message displayed by WeeChat or a plugin (colors stripped).
*/
int
test_print_cb (const void *pointer, void *data, struct t_gui_buffer *buffer,
time_t date, int tags_count, const char **tags, int displayed,
int highlight, const char *prefix, const char *message)
{
const char *buffer_full_name;
char str_recorded[8192];
/* make C++ compiler happy */
(void) pointer;
(void) data;
(void) buffer;
(void) date;
(void) tags_count;
(void) tags;
(void) displayed;
(void) highlight;
buffer_full_name = gui_buffer_get_string (buffer, "full_name");
if (record_messages)
{
snprintf (str_recorded, sizeof (str_recorded),
"%s: \"%s%s%s\"",
buffer_full_name,
(prefix && prefix[0]) ? prefix : "",
(prefix && prefix[0]) ? " " : "",
(message && message[0]) ? message : "");
arraylist_add (recorded_messages, strdup (str_recorded));
}
/* keep only messages displayed on core buffer */
if (strcmp (buffer_full_name, "core.weechat") == 0)
{
printf ("%s%s%s\n", /* with color: "\33[34m%s%s%s\33[0m\n" */
(prefix && prefix[0]) ? prefix : "",
(prefix && prefix[0]) ? " " : "",
(message && message[0]) ? message : "");
}
return WEECHAT_RC_OK;
}
/*
* Callback used to compare two recorded messages.
*/
int
record_cmp_cb (void *data, struct t_arraylist *arraylist,
void *pointer1, void *pointer2)
{
/* make C++ compiler happy */
(void) data;
(void) arraylist;
return strcmp ((char *)pointer1, (char *)pointer2);
}
/*
* Callback used to free a recorded message.
*/
void
record_free_cb (void *data, struct t_arraylist *arraylist, void *pointer)
{
/* make C++ compiler happy */
(void) data;
(void) arraylist;
free (pointer);
}
/*
* Starts recording of messages displayed.
*/
void
record_start ()
{
record_messages = 1;
if (recorded_messages)
{
arraylist_clear (recorded_messages);
}
else
{
recorded_messages = arraylist_new (16, 0, 1,
&record_cmp_cb, NULL,
&record_free_cb, NULL);
}
}
/*
* Stops recording of messages displayed.
*/
void
record_stop ()
{
record_messages = 0;
}
/*
* Searches if a message has been displayed in a buffer.
*
* The format of "message" argument is: "prefix message" (prefix and message
* separated by a space).
*
* Returns:
* 1: message has been displayed
* 0: message has NOT been displayed
*/
int
record_search (const char *buffer, const char *message)
{
char str_message[8192];
snprintf (str_message, sizeof (str_message),
"%s: \"%s\"",
buffer, message);
return (arraylist_search (recorded_messages, str_message,
NULL, NULL) != NULL);
}
/*
* Adds all recorded messages to the dynamic string "msg".
*/
void
record_dump (char **msg)
{
int i;
for (i = 0; i < arraylist_size (recorded_messages); i++)
{
string_dyn_concat (msg, " ", -1);
string_dyn_concat (msg,
(const char *)arraylist_get (recorded_messages, i),
-1);
string_dyn_concat (msg, "\n", -1);
}
}
/*
* Initializes GUI for tests.
*/
void
test_gui_init ()
{
/*
* Catch all messages to display them directly on stdout
* (Curses library is not used for tests).
*/
hook_print (NULL, /* plugin */
NULL, /* buffer */
NULL, /* tags */
NULL, /* message */
1, /* strip colors */
&test_print_cb,
NULL,
NULL);
/*
* Call the function "gui_main_init" from Curses sources (all Curses
* calls are made with the fake ncurses library).
*/
gui_main_init ();
}
/*
* Displays and runs a command on a buffer.
*/
void
run_cmd (const char *command)
{
printf (">>> Running command: %s\n", command);
input_data (ptr_core_buffer, command, NULL);
}
/*
* Runs a command on a buffer (do not display the command executed).
*/
void
run_cmd_quiet (const char *command)
{
input_data (ptr_core_buffer, command, NULL);
}
/*
* Runs tests in WeeChat environment.
*/
int
main (int argc, char *argv[])
{
int rc, length, weechat_argc;
char *weechat_tests_args, *args, **weechat_argv, *tests_plugins_lib;
const char *tests_plugins_lib_default = WEECHAT_TESTS_PLUGINS_LIB_DEFAULT;
const char *ptr_path;
void *handle;
/* setup environment: English language, no specific timezone */
setenv ("LC_ALL", LOCALE_TESTS, 1);
setenv ("TZ", "", 1);
/* check if locale exists */
if (!setlocale (LC_ALL, ""))
{
fprintf (stderr,
"ERROR: the locale %s must be installed to run WeeChat "
"tests.\n",
LOCALE_TESTS);
return 1;
}
/* clean WeeChat home */
dir_exec_on_files (WEECHAT_TESTS_HOME, 1, 1, &exec_on_files_cb, NULL);
/* build arguments for WeeChat */
weechat_tests_args = getenv ("WEECHAT_TESTS_ARGS");
length = strlen (argv[0]) +
64 + /* --dir ... */
((weechat_tests_args) ? 1 + strlen (weechat_tests_args) : 0) +
1;
args = (char *)malloc (length);
if (!args)
{
fprintf (stderr, "Memory error\n");
return 1;
}
snprintf (args, length,
"%s --dir %s%s%s",
argv[0],
WEECHAT_TESTS_HOME,
(weechat_tests_args) ? " " : "",
(weechat_tests_args) ? weechat_tests_args : "");
weechat_argv = string_split_shell (args, &weechat_argc);
printf ("WeeChat arguments: \"%s\"\n", args);
/* init WeeChat */
weechat_init_gettext ();
weechat_init (weechat_argc, weechat_argv, &test_gui_init);
if (weechat_argv)
string_free_split (weechat_argv);
free (args);
ptr_core_buffer = gui_buffer_search_main ();
/* auto-load plugins from WEECHAT_EXTRA_LIBDIR if no plugin were loaded */
if (!weechat_plugins)
{
gui_chat_printf (NULL,
"Auto-loading plugins from path in "
"environment variable WEECHAT_EXTRA_LIBDIR (\"%s\")",
getenv ("WEECHAT_EXTRA_LIBDIR"));
plugin_auto_load (NULL, 0, 1, 0, 0, NULL);
}
/* load plugins tests */
tests_plugins_lib = getenv ("WEECHAT_TESTS_PLUGINS_LIB");
ptr_path = (tests_plugins_lib && tests_plugins_lib[0]) ?
tests_plugins_lib : tests_plugins_lib_default;
printf ("Loading tests on plugins: \"%s\"\n", ptr_path);
handle = dlopen (ptr_path, RTLD_GLOBAL | RTLD_NOW);
if (!handle)
{
fprintf (stderr, "ERROR: unable to load tests on plugins: %s\n",
dlerror ());
return 1;
}
/* display WeeChat version and directories */
run_cmd ("/command core version");
run_cmd ("/debug dirs");
run_cmd ("/debug libs");
/* run all tests */
printf ("\n");
rc = CommandLineTestRunner::RunAllTests (argc, argv);
/* end WeeChat */
gui_chat_mute = GUI_CHAT_MUTE_ALL_BUFFERS;
weechat_end (&gui_main_end);
dlclose (handle);
return rc;
}
|