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
|
/*
* test-scripts.cpp - test scripting API
*
* Copyright (C) 2017-2023 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 "CppUTest/TestHarness.h"
#include "tests/tests.h"
extern "C"
{
#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H
#endif
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "src/core/weechat.h"
#include "src/core/wee-hdata.h"
#include "src/core/wee-string.h"
#include "src/core/wee-hook.h"
#include "src/core/wee-util.h"
#include "src/plugins/plugin.h"
}
struct t_hook *api_hook_print = NULL;
int api_tests_ok = 0;
int api_tests_errors = 0;
int api_tests_count = 0;
int api_tests_end = 0;
int api_tests_other = 0;
TEST_GROUP(Scripts)
{
/*
* Callback for any message displayed by WeeChat or a plugin.
*/
static 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 *pos;
char *error;
int value;
/* make C++ compiler happy */
(void) pointer;
(void) data;
(void) buffer;
(void) date;
(void) tags_count;
(void) tags;
(void) displayed;
(void) highlight;
(void) prefix;
if (message)
{
pos = strstr (message, "> TESTS: ");
if (pos)
{
error = NULL;
value = (int)strtol (pos + 9, &error, 10);
if (error && !error[0])
api_tests_count = value;
}
else if (strstr (message, "TEST OK"))
api_tests_ok++;
else if (strstr (message, "ERROR"))
api_tests_errors++;
else if (strstr (message, "TESTS END"))
api_tests_end++;
else if ((message[0] != '>') && (message[0] != ' ')
&& (strncmp (message, "## ", 3) != 0))
{
api_tests_other++;
}
}
return WEECHAT_RC_OK;
}
void setup()
{
api_hook_print = hook_print (NULL, /* plugin */
NULL, /* buffer */
NULL, /* tags */
NULL, /* message */
1, /* strip colors */
&test_print_cb,
NULL,
NULL);
}
void teardown()
{
unhook (api_hook_print);
}
};
/*
* Tests scripting API.
*/
TEST(Scripts, API)
{
char path_testapigen[PATH_MAX], path_testapi[PATH_MAX];
char *path_testapi_output_dir, str_command[(PATH_MAX * 2) + 128];
char *test_scripts_dir, str_condition[128];
struct timeval time_start, time_end;
long long diff;
const char *ptr_test_scripts_dir;
const char *languages[][2] = {
{ "python", "py" },
{ "perl", "pl" },
{ "ruby", "rb" },
{ "lua", "lua" },
{ "tcl", "tcl" },
{ "guile", "scm" },
{ "javascript", "js" },
{ "php", "php" },
{ NULL, NULL }
};
int i, turnoff_memleak;
struct t_hdata *hdata;
void *plugins;
printf ("...\n");
ptr_test_scripts_dir = getenv ("WEECHAT_TESTS_SCRIPTS_DIR");
test_scripts_dir = strdup (
(ptr_test_scripts_dir) ?
ptr_test_scripts_dir : "../tests/scripts/python");
/* build paths for scripting API tests */
snprintf (path_testapigen, sizeof (path_testapigen),
"%s%s%s",
test_scripts_dir,
DIR_SEPARATOR,
"testapigen.py");
snprintf (path_testapi, sizeof (path_testapi),
"%s%s%s",
test_scripts_dir,
DIR_SEPARATOR,
"testapi.py");
path_testapi_output_dir = string_eval_path_home (
"${weechat_data_dir}/testapi",
NULL, NULL, NULL);
CHECK(path_testapi_output_dir);
api_tests_ok = 0;
api_tests_errors = 0;
/* load generator script */
snprintf (str_command, sizeof (str_command),
"/script load %s", path_testapigen);
run_cmd (str_command);
/* generate scripts to test API */
snprintf (str_command, sizeof (str_command),
"/testapigen %s %s",
path_testapi,
path_testapi_output_dir);
run_cmd (str_command);
/* check that there was no errors in script generation */
LONGS_EQUAL(0, api_tests_errors);
/* unload generator script */
snprintf (str_command, sizeof (str_command),
"/script unload testapigen.py");
run_cmd (str_command);
hdata = hook_hdata_get (NULL, "plugin");
plugins = hdata_get_list (hdata, "weechat_plugins");
/* test the scripting API */
for (i = 0; languages[i][0]; i++)
{
/* test if the plugin is loaded; if not, tests are skipped */
snprintf (str_condition, sizeof (str_condition),
"${plugin.name} == %s",
languages[i][0]);
if (!hdata_search (hdata, plugins, str_condition, NULL, NULL, NULL, 1))
continue;
/*
* TODO: fix memory leaks in javascript plugin
* and keep memory leak detection enabled
*/
turnoff_memleak = (strcmp (languages[i][0], "javascript") == 0);
if (turnoff_memleak)
MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
api_tests_ok = 0;
api_tests_errors = 0;
api_tests_count = 0;
api_tests_end = 0;
api_tests_other = 0;
/* load script (run tests) */
snprintf (str_command, sizeof (str_command),
"/script load -q %s/weechat_testapi.%s",
path_testapi_output_dir,
languages[i][1]);
run_cmd (str_command);
/* get date/time before running tests */
gettimeofday (&time_start, NULL);
/* run tests */
snprintf (str_command, sizeof (str_command),
"/weechat_testapi.%s",
languages[i][1]);
run_cmd (str_command);
/* compute elapsed time */
gettimeofday (&time_end, NULL);
diff = util_timeval_diff (&time_start, &time_end);
/* display results */
printf ("\n");
printf (">>> Tests %s: %d tests, %d OK, %d errors, "
"%d unexpected messages, %lld ms\n",
languages[i][0],
api_tests_count,
api_tests_ok,
api_tests_errors,
api_tests_other,
diff / 1000);
printf ("\n");
/* unload script */
snprintf (str_command, sizeof (str_command),
"/script unload -q weechat_testapi.%s",
languages[i][1]);
run_cmd (str_command);
/* check that tests were found in script */
CHECK(api_tests_count > 0);
/* check that all tests are OK */
LONGS_EQUAL(api_tests_count, api_tests_ok);
/* check that there was no errors */
LONGS_EQUAL(0, api_tests_errors);
/* check that end of script was reached (no syntax error) */
LONGS_EQUAL(1, api_tests_end);
/*
* check that there was no warning/error from plugin
* (if everything is OK, there are 2 messages when the script is loaded
* and 2 messages when it is unloaded, so total is 4)
*/
LONGS_EQUAL(0, api_tests_other);
if (turnoff_memleak)
MemoryLeakWarningPlugin::turnOnThreadSafeNewDeleteOverloads();
}
free (path_testapi_output_dir);
free (test_scripts_dir);
printf ("TEST(Scripts, API)");
}
|