1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-28 22:06:38 +02:00

tests: add a function to search a message displayed with a regex

This commit is contained in:
Sébastien Helleu
2025-11-10 07:52:27 +01:00
parent 3a3dec985d
commit 5f0eebc0df
2 changed files with 62 additions and 0 deletions
+60
View File
@@ -24,6 +24,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#include "tests-record.h"
@@ -172,6 +173,26 @@ record_match (struct t_hashtable *recorded_msg,
|| (ptr_value && value && (strcmp (ptr_value, value) == 0)));
}
/*
* Checks if a recorded message field matches a regular expression.
*
* Returns:
* 1: value matches the regex
* 0: value does NOT match the regex
*/
int
record_match_regex (struct t_hashtable *recorded_msg,
const char *field, regex_t *regex)
{
const char *ptr_value;
ptr_value = (const char *)hashtable_get (recorded_msg, field);
return ((!ptr_value && !regex)
|| (regexec (regex, ptr_value, 0, NULL, 0) == 0));
}
/*
* Searches if a prefix/message has been displayed in a buffer.
*
@@ -206,6 +227,45 @@ record_search (const char *buffer, const char *prefix, const char *message,
return NULL;
}
/*
* Searches if a message has been displayed in a buffer using a regular
* expression.
*
* Returns pointer to hashtable with the first message found, NULL if no
* message is matching the regular expression.
*/
struct t_hashtable *
record_search_msg_regex (const char *buffer, const char *regex)
{
int i, size;
struct t_hashtable *rec_msg;
regex_t preg;
if (string_regcomp (&preg, regex, REG_EXTENDED | REG_NOSUB) != 0)
return NULL;
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_regex (rec_msg, "message_no_color", &preg))
{
regfree (&preg);
return rec_msg;
}
}
regfree (&preg);
/* no message matching the regex */
return NULL;
}
/*
* Returns the number of messages displayed during the recording.
*/
+2
View File
@@ -58,6 +58,8 @@ extern void record_start ();
extern void record_stop ();
extern struct t_hashtable *record_search (const char *buffer, const char *prefix,
const char *message, const char *tags);
extern struct t_hashtable *record_search_msg_regex (const char *buffer,
const char *regex);
extern int record_count_messages ();
extern void record_dump (char **msg);
extern void record_error_missing (const char *message);