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