1From 23c1b401d8c78c2c66d55b94d7d833210d518853 Mon Sep 17 00:00:00 2001 2From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net> 3Date: Tue, 6 Sep 2022 14:21:27 +0200 4Subject: [PATCH] tests/regex: Add debug strings for compile and match option 5 flags 6 7In case of failures they give a better info. 8--- 9 glib/tests/regex.c | 132 +++++++++++++++++++++++++++++++++++++++++---- 10 1 file changed, 122 insertions(+), 10 deletions(-) 11 12diff --git a/glib/tests/regex.c b/glib/tests/regex.c 13index acb082b704..567b6e2202 100644 14--- a/glib/tests/regex.c 15+++ b/glib/tests/regex.c 16@@ -184,6 +184,108 @@ test_match_simple (gconstpointer d) 17 #define TEST_MATCH_NOTEMPTY_ATSTART(_pattern, _string, _expected) \ 18 TEST_MATCH_SIMPLE_NAMED("notempty-atstart", _pattern, _string, 0, G_REGEX_MATCH_NOTEMPTY_ATSTART, _expected) 19 20+static char * 21+compile_options_to_string (GRegexCompileFlags compile_flags) 22+{ 23+ GStrvBuilder *builder = g_strv_builder_new(); 24+ GStrv strv; 25+ char *ret; 26+ 27+ if (compile_flags & G_REGEX_DEFAULT) 28+ g_strv_builder_add (builder, "default"); 29+ if (compile_flags & G_REGEX_CASELESS) 30+ g_strv_builder_add (builder, "caseless"); 31+ if (compile_flags & G_REGEX_MULTILINE) 32+ g_strv_builder_add (builder, "multiline"); 33+ if (compile_flags & G_REGEX_DOTALL) 34+ g_strv_builder_add (builder, "dotall"); 35+ if (compile_flags & G_REGEX_EXTENDED) 36+ g_strv_builder_add (builder, "extended"); 37+ if (compile_flags & G_REGEX_ANCHORED) 38+ g_strv_builder_add (builder, "anchored"); 39+ if (compile_flags & G_REGEX_DOLLAR_ENDONLY) 40+ g_strv_builder_add (builder, "dollar-endonly"); 41+ if (compile_flags & G_REGEX_UNGREEDY) 42+ g_strv_builder_add (builder, "ungreedy"); 43+ if (compile_flags & G_REGEX_RAW) 44+ g_strv_builder_add (builder, "raw"); 45+ if (compile_flags & G_REGEX_NO_AUTO_CAPTURE) 46+ g_strv_builder_add (builder, "no-auto-capture"); 47+ if (compile_flags & G_REGEX_OPTIMIZE) 48+ g_strv_builder_add (builder, "optimize"); 49+ if (compile_flags & G_REGEX_FIRSTLINE) 50+ g_strv_builder_add (builder, "firstline"); 51+ if (compile_flags & G_REGEX_DUPNAMES) 52+ g_strv_builder_add (builder, "dupnames"); 53+ if (compile_flags & G_REGEX_NEWLINE_CR) 54+ g_strv_builder_add (builder, "newline-cr"); 55+ if (compile_flags & G_REGEX_NEWLINE_LF) 56+ g_strv_builder_add (builder, "newline-lf"); 57+ if (compile_flags & G_REGEX_NEWLINE_CRLF) 58+ g_strv_builder_add (builder, "newline-crlf"); 59+ if (compile_flags & G_REGEX_NEWLINE_ANYCRLF) 60+ g_strv_builder_add (builder, "newline-anycrlf"); 61+ if (compile_flags & G_REGEX_BSR_ANYCRLF) 62+ g_strv_builder_add (builder, "bsr-anycrlf"); 63+ 64+ strv = g_strv_builder_end (builder); 65+ ret = g_strjoinv ("|", strv); 66+ 67+ g_strfreev (strv); 68+ g_strv_builder_unref (builder); 69+ 70+ return ret; 71+} 72+ 73+static char * 74+match_options_to_string (GRegexMatchFlags match_flags) 75+{ 76+ GStrvBuilder *builder = g_strv_builder_new(); 77+ GStrv strv; 78+ char *ret; 79+ 80+ if (match_flags & G_REGEX_MATCH_DEFAULT) 81+ g_strv_builder_add (builder, "default"); 82+ if (match_flags & G_REGEX_MATCH_ANCHORED) 83+ g_strv_builder_add (builder, "anchored"); 84+ if (match_flags & G_REGEX_MATCH_NOTBOL) 85+ g_strv_builder_add (builder, "notbol"); 86+ if (match_flags & G_REGEX_MATCH_NOTEOL) 87+ g_strv_builder_add (builder, "noteol"); 88+ if (match_flags & G_REGEX_MATCH_NOTEMPTY) 89+ g_strv_builder_add (builder, "notempty"); 90+ if (match_flags & G_REGEX_MATCH_PARTIAL) 91+ g_strv_builder_add (builder, "partial"); 92+ if (match_flags & G_REGEX_MATCH_NEWLINE_CR) 93+ g_strv_builder_add (builder, "newline-cr"); 94+ if (match_flags & G_REGEX_MATCH_NEWLINE_LF) 95+ g_strv_builder_add (builder, "newline-lf"); 96+ if (match_flags & G_REGEX_MATCH_NEWLINE_CRLF) 97+ g_strv_builder_add (builder, "newline-crlf"); 98+ if (match_flags & G_REGEX_MATCH_NEWLINE_ANY) 99+ g_strv_builder_add (builder, "newline-any"); 100+ if (match_flags & G_REGEX_MATCH_NEWLINE_ANYCRLF) 101+ g_strv_builder_add (builder, "newline-anycrlf"); 102+ if (match_flags & G_REGEX_MATCH_BSR_ANYCRLF) 103+ g_strv_builder_add (builder, "bsr-anycrlf"); 104+ if (match_flags & G_REGEX_MATCH_BSR_ANY) 105+ g_strv_builder_add (builder, "bsr-any"); 106+ if (match_flags & G_REGEX_MATCH_PARTIAL_SOFT) 107+ g_strv_builder_add (builder, "partial-soft"); 108+ if (match_flags & G_REGEX_MATCH_PARTIAL_HARD) 109+ g_strv_builder_add (builder, "partial-hard"); 110+ if (match_flags & G_REGEX_MATCH_NOTEMPTY_ATSTART) 111+ g_strv_builder_add (builder, "notempty-atstart"); 112+ 113+ strv = g_strv_builder_end (builder); 114+ ret = g_strjoinv ("|", strv); 115+ 116+ g_strfreev (strv); 117+ g_strv_builder_unref (builder); 118+ 119+ return ret; 120+} 121+ 122 static void 123 test_match (gconstpointer d) 124 { 125@@ -191,6 +293,9 @@ test_match (gconstpointer d) 126 GRegex *regex; 127 gboolean match; 128 GError *error = NULL; 129+ gchar *compile_opts_str; 130+ gchar *match_opts_str; 131+ gchar *match_opts2_str; 132 133 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error); 134 g_assert (regex != NULL); 135@@ -199,31 +304,35 @@ test_match (gconstpointer d) 136 match = g_regex_match_full (regex, data->string, data->string_len, 137 data->start_position, data->match_opts2, NULL, NULL); 138 139+ compile_opts_str = compile_options_to_string (data->compile_opts); 140+ match_opts_str = match_options_to_string (data->match_opts); 141+ match_opts2_str = match_options_to_string (data->match_opts2); 142+ 143 if (data->expected) 144 { 145 if (!match) 146- g_error ("Regex '%s' (with compile options %u and " 147- "match options %u) should have matched '%.*s' " 148- "(of length %d, at position %d, with match options %u) but did not", 149- data->pattern, data->compile_opts, data->match_opts, 150+ g_error ("Regex '%s' (with compile options '%s' and " 151+ "match options '%s') should have matched '%.*s' " 152+ "(of length %d, at position %d, with match options '%s') but did not", 153+ data->pattern, compile_opts_str, match_opts_str, 154 data->string_len == -1 ? (int) strlen (data->string) : 155 (int) data->string_len, 156 data->string, (int) data->string_len, 157- data->start_position, data->match_opts2); 158+ data->start_position, match_opts2_str); 159 160 g_assert_cmpint (match, ==, TRUE); 161 } 162 else 163 { 164 if (match) 165- g_error ("Regex '%s' (with compile options %u and " 166- "match options %u) should not have matched '%.*s' " 167- "(of length %d, at position %d, with match options %u) but did", 168- data->pattern, data->compile_opts, data->match_opts, 169+ g_error ("Regex '%s' (with compile options '%s' and " 170+ "match options '%s') should not have matched '%.*s' " 171+ "(of length %d, at position %d, with match options '%s') but did", 172+ data->pattern, compile_opts_str, match_opts_str, 173 data->string_len == -1 ? (int) strlen (data->string) : 174 (int) data->string_len, 175 data->string, (int) data->string_len, 176- data->start_position, data->match_opts2); 177+ data->start_position, match_opts2_str); 178 } 179 180 if (data->string_len == -1 && data->start_position == 0) 181@@ -232,6 +341,9 @@ test_match (gconstpointer d) 182 g_assert_cmpint (match, ==, data->expected); 183 } 184 185+ g_free (compile_opts_str); 186+ g_free (match_opts_str); 187+ g_free (match_opts2_str); 188 g_regex_unref (regex); 189 } 190 191-- 192GitLab 193 194