1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-12 14:14:48 +02:00

tests: separate prefix from message in recorded messages

The record functions are moved to tests-record.cpp.
This commit is contained in:
Sébastien Helleu
2023-05-21 14:54:02 +02:00
parent 126d3559ca
commit eb7435f8b9
11 changed files with 909 additions and 733 deletions
+1
View File
@@ -390,6 +390,7 @@ WeeChat "core" is located in following directories:
| Path/file | Description
| tests/ | Root of tests.
|    tests.cpp | Program used to run all tests.
|    tests-record.cpp | Record and search in messages displayed.
|    scripts/ | Root of scripting API tests.
|       test-scripts.cpp | Program used to run the scripting API tests.
|       python/ | Python scripts to generate and run the scripting API tests.
+1
View File
@@ -392,6 +392,7 @@ Le cœur de WeeChat est situé dans les répertoires suivants :
| Chemin/fichier | Description
| tests/ | Racine des tests.
|    tests.cpp | Programme utilisé pour lancer tous les tests.
|    tests-record.cpp | Enregistrement et recherche dans les messages affichés.
|    scripts/ | Racine des tests de l'API script.
|       test-scripts.cpp | Programme utilisé pour lancer les tests de l'API script.
|       python/ | Scripts Python pour générer et lancer les tests de l'API script.
+2
View File
@@ -413,6 +413,8 @@ WeeChat "core" は以下のディレクトリに配置されています:
| パス/ファイル名 | 説明
| tests/ | テスト用のルートディレクトリ
|    tests.cpp | 全テストの実行時に使われるプログラム
// TRANSLATION MISSING
|    tests-record.cpp | Record and search in messages displayed.
|    scripts/ | スクリプト API テスト用のルートディレクトリ
|       test-scripts.cpp | スクリプト API テストの実行時に使われるプログラム
|       python/ | スクリプト API テストを生成、実行する Python スクリプト
+2
View File
@@ -393,6 +393,8 @@ WeeChat „језгро” се налази у следећим директо
| Путања/фајл | Опис
| tests/ | Корен тестова.
|    tests.cpp | Програм који се користи за извршавање свих тестова.
// TRANSLATION MISSING
|    tests-record.cpp | Record and search in messages displayed.
|    scripts/ | Корен тестова за API скриптовања.
|       test-scripts.cpp | Програм који се користи за извршавање тестова API скриптовања.
|       python/ | Python скрипте које генеришу и покрећу тестове API скриптовања.
+4 -1
View File
@@ -135,7 +135,10 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
endif()
# binary to run tests
set(WEECHAT_TESTS_SRC tests.cpp tests.h)
set(WEECHAT_TESTS_SRC
tests.cpp tests.h
tests-record.cpp tests-record.h
)
add_executable(tests ${WEECHAT_TESTS_SRC})
target_link_libraries(tests
weechat_core
+238
View File
@@ -0,0 +1,238 @@
/*
* tests-record.cpp - record and search in messages displayed
*
* Copyright (C) 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 <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "tests-record.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-hashtable.h"
#include "src/core/wee-hook.h"
#include "src/core/wee-string.h"
#include "src/gui/gui-color.h"
}
/* recording of messages: to test if a message is actually displayed */
int record_messages = 0;
struct t_arraylist *recorded_messages = NULL;
struct t_hook *record_hook_line = NULL;
/*
* 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 (pointer1 < pointer2) ?
-1 : ((pointer1 > pointer2) ? 1 : 0);
}
/*
* 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;
if (pointer)
hashtable_free ((struct t_hashtable *)pointer);
}
/*
* Callback for hook line, used when message are recorded.
*/
struct t_hashtable *
record_hook_line_cb (const void *pointer, void *data, struct t_hashtable *line)
{
struct t_hashtable *hashtable;
const char *ptr_string;
/* make C++ compiler happy */
(void) pointer;
(void) data;
hashtable = hashtable_dup (line);
ptr_string = (const char *)hashtable_get (hashtable, "prefix");
hashtable_set (
hashtable,
"prefix_no_color",
(ptr_string) ? gui_color_decode (ptr_string, NULL) : NULL);
ptr_string = (const char *)hashtable_get (hashtable, "message");
hashtable_set (
hashtable,
"message_no_color",
(ptr_string) ? gui_color_decode (ptr_string, NULL) : NULL);
arraylist_add (recorded_messages, hashtable);
return NULL;
}
/*
* 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);
}
if (!record_hook_line)
{
record_hook_line = hook_line (NULL, "*", NULL, NULL,
&record_hook_line_cb, NULL, NULL);
}
}
/*
* Stops recording of messages displayed.
*/
void
record_stop ()
{
record_messages = 0;
if (record_hook_line)
{
unhook (record_hook_line);
record_hook_line = NULL;
}
}
/*
* Checks if a recorded message field matches a value.
*
* Returns:
* 1: value matches
* 0: value does NOT match
*/
int
record_match (struct t_hashtable *recorded_msg,
const char *field, const char *value)
{
const char *ptr_value;
ptr_value = (const char *)hashtable_get (recorded_msg, field);
return ((!ptr_value && !value)
|| (ptr_value && value && (strcmp (ptr_value, value) == 0)));
}
/*
* Searches if a prefix/message has been displayed in a buffer.
*
* Returns index of message displayed (≥ 0), -1 if message has NOT been
* displayed.
*/
int
record_search (const char *buffer, const char *prefix, const char *message)
{
int i, size;
struct t_hashtable *rec_msg;
size = arraylist_size (recorded_messages);
for (i = 0; i < size; i++)
{
rec_msg = (struct t_hashtable *)arraylist_get (recorded_messages, i);
if (!rec_msg)
continue;
if (record_match (rec_msg, "buffer_name", buffer)
&& record_match (rec_msg, "prefix_no_color", prefix)
&& record_match (rec_msg, "message_no_color", message))
{
return i;
}
}
return -1;
}
/*
* Adds all recorded messages to the dynamic string "msg".
*/
void
record_dump (char **msg)
{
struct t_hashtable *rec_msg;
const char *ptr_buffer_name, *ptr_prefix, *ptr_msg;
int i, size;
size = arraylist_size (recorded_messages);
for (i = 0; i < size; i++)
{
rec_msg = (struct t_hashtable *)arraylist_get (recorded_messages, i);
if (!rec_msg)
continue;
ptr_buffer_name = (const char *)hashtable_get (rec_msg, "buffer_name");
ptr_prefix = (const char *)hashtable_get (rec_msg, "prefix_no_color");
ptr_msg = (const char *)hashtable_get (rec_msg, "message_no_color");
string_dyn_concat (msg, " ", -1);
string_dyn_concat (msg, ptr_buffer_name, -1);
string_dyn_concat (msg, ": prefix=\"", -1);
string_dyn_concat (msg, ptr_prefix, -1);
string_dyn_concat (msg, "\", message=\"", -1);
string_dyn_concat (msg, ptr_msg, -1);
string_dyn_concat (msg, "\"\n", -1);
}
}
+32
View File
@@ -0,0 +1,32 @@
/*
* Copyright (C) 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/>.
*/
#ifndef WEECHAT_TESTS_RECORD_H
#define WEECHAT_TESTS_RECORD_H
extern struct t_arraylist *recorded_messages;
extern void record_start ();
extern void record_stop ();
extern int record_search (const char *buffer, const char *prefix,
const char *message);
extern void record_dump (char **msg);
extern void record_error_missing (const char *message);
#endif /* WEECHAT_TESTS_RECORD_H */
+3 -125
View File
@@ -28,6 +28,8 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "tests-record.h"
extern "C"
{
#ifndef HAVE_CONFIG_H
@@ -93,10 +95,6 @@ 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).
@@ -120,9 +118,6 @@ 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;
@@ -133,21 +128,8 @@ test_print_cb (const void *pointer, void *data, struct t_gui_buffer *buffer,
(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 && 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)
if (strcmp (gui_buffer_get_string (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 : "",
@@ -158,110 +140,6 @@ test_print_cb (const void *pointer, void *data, struct t_gui_buffer *buffer,
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 index of message displayed (≥ 0), -1 if message has NOT been
* displayed.
*/
int
record_search (const char *buffer, const char *message)
{
char str_message[8192];
int index;
snprintf (str_message, sizeof (str_message),
"%s: \"%s\"",
buffer, message);
arraylist_search (recorded_messages, str_message, &index, NULL);
return index;
}
/*
* 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.
*/
-8
View File
@@ -32,14 +32,6 @@
} \
free (str);
extern struct t_arraylist *recorded_messages;
extern void record_start ();
extern void record_stop ();
extern int record_search (const char *buffer, const char *message);
extern void record_dump (char **msg);
extern void record_error_missing (const char *message);
extern void run_cmd (const char *command);
extern void run_cmd_quiet (const char *command);
+25 -18
View File
@@ -22,6 +22,7 @@
#include "CppUTest/TestHarness.h"
#include "tests/tests.h"
#include "tests/tests-record.h"
extern "C"
{
@@ -41,21 +42,19 @@ extern "C"
#define WEE_CMD_CORE(__command) \
command_record ("core.weechat", __command);
#define WEE_CHECK_MSG_BUFFER(__buffer_name, __message) \
if (record_search (__buffer_name, __message) < 0) \
#define WEE_CHECK_MSG_BUFFER(__buffer_name, __prefix, __message) \
if (record_search (__buffer_name, __prefix, __message) < 0) \
{ \
char **msg = command_build_error ( \
"Message not displayed on buffer " __buffer_name ": " \
"\"" __message "\n" \
"All messages displayed:\n"); \
char **msg = command_build_error (__buffer_name, __prefix, \
__message); \
record_dump (msg); \
FAIL(string_dyn_free (msg, 0)); \
}
#define WEE_CHECK_MSG_CORE(__message) \
WEE_CHECK_MSG_BUFFER("core.weechat", __message);
#define WEE_SEARCH_MSG_CORE(__message) \
record_search ("core.weechat", __message)
#define WEE_CHECK_MSG_CORE(__prefix, __message) \
WEE_CHECK_MSG_BUFFER("core.weechat", __prefix, __message);
#define WEE_SEARCH_MSG_CORE(__prefix, __message) \
record_search ("core.weechat", __prefix, __message)
TEST_GROUP(CoreCommand)
@@ -74,12 +73,20 @@ TEST_GROUP(CoreCommand)
record_stop ();
}
char **command_build_error (const char *message)
char **command_build_error (const char *buffer_name, const char *prefix,
const char *message)
{
char **msg;
msg = string_dyn_alloc (1024);
string_dyn_concat (msg, "Message not displayed on buffer ", -1);
string_dyn_concat (msg, buffer_name, -1);
string_dyn_concat (msg, ": prefix=\"", -1);
string_dyn_concat (msg, prefix, -1);
string_dyn_concat (msg, "\", message=\"", -1);
string_dyn_concat (msg, message, -1);
string_dyn_concat (msg, "\"\n", -1);
string_dyn_concat (msg, "All messages displayed:\n", -1);
return msg;
}
};
@@ -216,9 +223,9 @@ TEST(CoreCommand, Debug)
/* test command "/debug unicode" */
WEE_CMD_CORE(command_debug_unicode);
WEE_CHECK_MSG_CORE(" \"\u00E9\u26C4\": 5 / 2, 2 / 3, 3, 3");
WEE_CHECK_MSG_CORE(" \"\u00E9\" (U+00E9, 233, 0xC3 0xA9): 2 / 1, 1 / 1, 1, 1, 1");
WEE_CHECK_MSG_CORE(" \"\u26C4\" (U+26C4, 9924, 0xE2 0x9B 0x84): 3 / 1, 1 / 2, 2, 2, 2");
WEE_CHECK_MSG_CORE("", " \"\u00E9\u26C4\": 5 / 2, 2 / 3, 3, 3");
WEE_CHECK_MSG_CORE("", " \"\u00E9\" (U+00E9, 233, 0xC3 0xA9): 2 / 1, 1 / 1, 1, 1, 1");
WEE_CHECK_MSG_CORE("", " \"\u26C4\" (U+26C4, 9924, 0xE2 0x9B 0x84): 3 / 1, 1 / 2, 2, 2, 2");
/* test command "/debug windows" */
/* TODO: write tests */
@@ -373,10 +380,10 @@ TEST(CoreCommand, Reload)
{
WEE_CMD_CORE("/save");
WEE_CMD_CORE("/reload");
LONGS_EQUAL(0, WEE_SEARCH_MSG_CORE("Options reloaded from sec.conf"));
LONGS_EQUAL(1, WEE_SEARCH_MSG_CORE("Options reloaded from weechat.conf"));
LONGS_EQUAL(2, WEE_SEARCH_MSG_CORE("Options reloaded from plugins.conf"));
LONGS_EQUAL(3, WEE_SEARCH_MSG_CORE("Options reloaded from charset.conf"));
LONGS_EQUAL(0, WEE_SEARCH_MSG_CORE("", "Options reloaded from sec.conf"));
LONGS_EQUAL(1, WEE_SEARCH_MSG_CORE("", "Options reloaded from weechat.conf"));
LONGS_EQUAL(2, WEE_SEARCH_MSG_CORE("", "Options reloaded from plugins.conf"));
LONGS_EQUAL(3, WEE_SEARCH_MSG_CORE("", "Options reloaded from charset.conf"));
}
/*
File diff suppressed because it is too large Load Diff