From 5f0eebc0dfdad9971d556e61995715d1116cb157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Mon, 10 Nov 2025 07:52:27 +0100 Subject: [PATCH] tests: add a function to search a message displayed with a regex --- tests/unit/tests-record.cpp | 60 +++++++++++++++++++++++++++++++++++++ tests/unit/tests-record.h | 2 ++ 2 files changed, 62 insertions(+) diff --git a/tests/unit/tests-record.cpp b/tests/unit/tests-record.cpp index 21826267e..192fd048c 100644 --- a/tests/unit/tests-record.cpp +++ b/tests/unit/tests-record.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #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. */ diff --git a/tests/unit/tests-record.h b/tests/unit/tests-record.h index 1513f8a90..ac1ed9847 100644 --- a/tests/unit/tests-record.h +++ b/tests/unit/tests-record.h @@ -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);