From 807dbfbebfe52d1199b53f8d3da902796eddde8f Mon Sep 17 00:00:00 2001 From: Jan Schmidt <jan@centricular.com> Date: Fri, 16 Aug 2024 08:53:39 +1000 Subject: [PATCH] check: Add fail_unless_matches_string() and assert macros Add string check macros for checking expected strings against a regular expression instead of just a direct literal match as provided by the existing fail_unless_equals_string() Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/7365> --- .../gstreamer/libs/gst/check/gstcheck.h | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/subprojects/gstreamer/libs/gst/check/gstcheck.h b/subprojects/gstreamer/libs/gst/check/gstcheck.h index b7ec2c4ab3..a4a1f2f41d 100644 --- a/subprojects/gstreamer/libs/gst/check/gstcheck.h +++ b/subprojects/gstreamer/libs/gst/check/gstcheck.h @@ -452,6 +452,37 @@ G_STMT_START { \ */ #define assert_equals_string(a, b) fail_unless_equals_string(a, b) +/** + * fail_unless_matches_string: + * @a: a string literal or expression + * @b: a regular expression pattern string literal or expression + * + * This macro checks that @a matches the regular expression in @b aborts if + * this is not the case, printing both expressions and the values they + * evaluated to. This macro is for use in unit tests. + * + * Since: 1.26 + */ +#define fail_unless_matches_string(a, b) \ +G_STMT_START { \ + const gchar * first = a; \ + const gchar * second = b; \ + fail_unless(g_regex_match_simple (second, first, 0, 0), \ + "'" #a "' (%s) does not match pattern '" #b"' (%s)", first, second); \ +} G_STMT_END; +/** + * assert_matches_string: + * @a: a string literal or expression + * @b: a regular expression pattern string literal or expression + * + * This macro checks that @a matches the regular expression in @b aborts if + * this is not the case, printing both expressions and the values they + * evaluated to. This macro is for use in unit tests. + * + * Since: 1.26 + */ +#define assert_matches_string(a, b) fail_unless_matches_string(a, b) + /** * fail_unless_equals_float: * @a: a #gdouble or #gfloat value or expression