[glib/wip/oholy/x-gvfs-notrash: 1/10] tests: Rewrite a URI test to use an array of test strings




commit dfbd8e76ed2df1598beb15dc4e18b18079aee5bd
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Jul 1 11:32:24 2020 +0100

    tests: Rewrite a URI test to use an array of test strings
    
    This introduces no functional changes, but will make it easier to add
    more tests in future.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 glib/tests/uri.c | 73 ++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 50 insertions(+), 23 deletions(-)
---
diff --git a/glib/tests/uri.c b/glib/tests/uri.c
index dca99a28d..2e8113ed4 100644
--- a/glib/tests/uri.c
+++ b/glib/tests/uri.c
@@ -1189,29 +1189,56 @@ test_uri_is_valid (void)
 static void
 test_uri_parse_params (void)
 {
-  GHashTable *params;
-
-  params = g_uri_parse_params ("", G_URI_FLAGS_NONE, '&', FALSE);
-  g_assert_cmpint (g_hash_table_size (params), ==, 0);
-  g_hash_table_unref (params);
-
-  params = g_uri_parse_params ("p1=foo&p2=bar", -1, '&', FALSE);
-  g_assert_cmpint (g_hash_table_size (params), ==, 2);
-  g_assert_cmpstr (g_hash_table_lookup (params, "p1"), ==, "foo");
-  g_assert_cmpstr (g_hash_table_lookup (params, "p2"), ==, "bar");
-  g_hash_table_unref (params);
-
-  params = g_uri_parse_params ("p1=foo&&P1=bar", -1, '&', FALSE);
-  g_assert_null (params);
-  params = g_uri_parse_params ("%00=foo", -1, '&', FALSE);
-  g_assert_null (params);
-  params = g_uri_parse_params ("p1=%00", -1, '&', FALSE);
-  g_assert_null (params);
-
-  params = g_uri_parse_params ("p1=foo&P1=bar", -1, '&', TRUE);
-  g_assert_cmpint (g_hash_table_size (params), ==, 1);
-  g_assert_cmpstr (g_hash_table_lookup (params, "p1"), ==, "bar");
-  g_hash_table_unref (params);
+  const struct
+    {
+      /* Inputs */
+      const gchar *uri;
+      gchar separator;
+      gboolean case_insensitive;
+      /* Outputs */
+      gssize expected_n_params;  /* -1 => error expected */
+      /* key, value, key, value, …, limited to length 2*expected_n_params */
+      const gchar *expected_param_key_values[4];
+    }
+  tests[] =
+    {
+      { "", '&', FALSE, 0, { NULL, }},
+      { "p1=foo&p2=bar", '&', FALSE, 2, { "p1", "foo", "p2", "bar" }},
+      { "p1=foo&&P1=bar", '&', FALSE, -1, { NULL, }},
+      { "%00=foo", '&', FALSE, -1, { NULL, }},
+      { "p1=%00", '&', FALSE, -1, { NULL, }},
+      { "p1=foo&P1=bar", '&', TRUE, 1, { "p1", "bar", NULL, }},
+    };
+  gsize i;
+
+  for (i = 0; i < G_N_ELEMENTS (tests); i++)
+    {
+      GHashTable *params;
+
+      g_test_message ("URI %" G_GSIZE_FORMAT ": %s", i, tests[i].uri);
+
+      g_assert (tests[i].expected_n_params < 0 ||
+                tests[i].expected_n_params <= G_N_ELEMENTS (tests[i].expected_param_key_values) / 2);
+
+      params = g_uri_parse_params (tests[i].uri, -1, tests[i].separator, tests[i].case_insensitive);
+
+      if (tests[i].expected_n_params < 0)
+        {
+          g_assert_null (params);
+        }
+      else
+        {
+          gsize j;
+
+          g_assert_cmpint (g_hash_table_size (params), ==, tests[i].expected_n_params);
+
+          for (j = 0; j < tests[i].expected_n_params; j += 2)
+            g_assert_cmpstr (g_hash_table_lookup (params, tests[i].expected_param_key_values[j]), ==,
+                             tests[i].expected_param_key_values[j + 1]);
+        }
+
+      g_clear_pointer (&params, g_hash_table_unref);
+    }
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]