diff tests.c @ 768:600a3c08747f

Add handle_escapes parameter to th_str{case}match() functions to enable handling of glob token escaping \* \?. This breaks the API. Also update the testcases and add few testcases for checking the escape functionality.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 10 Feb 2023 02:46:57 +0200
parents 0abba6091bd1
children c17eadc60c3d
line wrap: on
line diff
--- a/tests.c	Fri Feb 10 02:45:34 2023 +0200
+++ b/tests.c	Fri Feb 10 02:46:57 2023 +0200
@@ -364,7 +364,7 @@
 
 #define TEST_3B(fun, str1, str2, sbool, ret) do { \
         test_ctx ctx; \
-        test_start(&ctx, # fun  "('%s', '%s', %s)", str1, str2, sbool ? "true" : "false"); \
+        test_start(&ctx, # fun  "('%s', '%s', %s) == %s", str1, str2, sbool ? "true" : "false", ret ? "true" : "false"); \
         test_result(&ctx, fun (str1, str2, sbool) == ret); \
         test_end(&ctx); \
     } while (0)
@@ -1103,25 +1103,35 @@
         TEST_2C(th_strrcasecmp, "foo aSdFq baz", "asdfq", false);
     }
 
-    if (test_set_start("String matching #1"))
+    if (test_set_start("String matching #1 (case-sensitive)"))
     {
-        TEST_2B(th_strmatch, "abba ABBAkukka lol"      , "*lol"       , true);
-        TEST_2B(th_strmatch, "abba ABBAkukka lol"      , "*lo*"       , true);
-        TEST_2B(th_strmatch, "abba ABBAkukka lol"      , "*lo"        , false);
-        TEST_2B(th_strmatch, "abba ABBAkukka lol"      , "abba"       , false);
-        TEST_2B(th_strmatch, "abba ABBAkukka lol"      , "*bba*"      , true);
-        TEST_2B(th_strmatch, "abba ABBAkukka lol"      , "abba*"      , true);
-        TEST_2B(th_strmatch, "abba ABBAkukka lol"      , "abbak*"     , false);
-        TEST_2B(th_strmatch, "abba ABBAöökukka lol"    , "*abbaö?"    , false);
+        TEST_3B(th_strmatch, "abba ABBAkukka lol"      , "*lol"            , false, true);
+        TEST_3B(th_strmatch, "abba ABBAkukka lol"      , "*lo*"            , false, true);
+        TEST_3B(th_strmatch, "abba ABBAkukka lol"      , "*lo"             , false, false);
+        TEST_3B(th_strmatch, "abba ABBAkukka lol"      , "abba"            , false, false);
+        TEST_3B(th_strmatch, "abba ABBAkukka lol"      , "*bba*"           , false, true);
+        TEST_3B(th_strmatch, "abba ABBAkukka lol"      , "abba*"           , false, true);
+        TEST_3B(th_strmatch, "abba ABBAkukka lol"      , "abbak*"          , false, false);
+        TEST_3B(th_strmatch, "abba ABBAöökukka lol"    , "*abbaö?"         , false, false);
     }
 
-    if (test_set_start("String matching #2"))
+    if (test_set_start("String matching #2 (case-insensitive)"))
     {
-        TEST_2B(th_strcasematch, "abba ABBAkukka lol"  , "abbak*"     , false);
-        TEST_2B(th_strcasematch, "abba ABBAkukka lol"  , "*abbak*"    , true);
-        TEST_2B(th_strcasematch, "abba ABBAkukka lol"  , "*ab?ak*"    , true);
-        TEST_2B(th_strcasematch, "abba ABBAkukka lol"  , "*abbak?"    , false);
-        TEST_2B(th_strcasematch, "abba ABBAkukka lol"  , "?bba?abba*" , true);
+        TEST_3B(th_strcasematch, "abba ABBAkukka lol"    , "abbak*"        , false, false);
+        TEST_3B(th_strcasematch, "abba ABBAkukka lol"    , "*abbak*"       , false, true);
+        TEST_3B(th_strcasematch, "abba ABBAkukka lol"    , "*ab?ak*"       , false, true);
+        TEST_3B(th_strcasematch, "abba ABBAkukka lol"    , "*abbak?"       , false, false);
+        TEST_3B(th_strcasematch, "abba ABBAkukka lol"    , "?bba?abba*"    , false, true);
+    }
+
+    if (test_set_start("String matching #3 (escapes)"))
+    {
+        TEST_3B(th_strcasematch, "abba ABBA*kukka lol"   , "*abba\\*ku*"   , true, true);
+        TEST_3B(th_strcasematch, "abba ABBAkuk?ka lol"   , "*kuk\\?ka*"    , true, true);
+        TEST_3B(th_strcasematch, "abba ABBA*kukka lol"   , "*abba*ku*"     , true, true);
+        TEST_3B(th_strcasematch, "abba ABBAkuk?ka lol"   , "*kuk?ka*"      , true, true);
+        TEST_3B(th_strcasematch, "abba ABBAkuk?ka lol"   , "*kuk?ka\\*"    , true, false);
+        TEST_3B(th_strcasematch, "abba ABBAkuk\\ka lol"  , "*kuk\\\\ka*"   , true, true);
     }
 
     // Tests that test for things that do not work correctly yet
@@ -1131,7 +1141,7 @@
     {
         TEST_2A(th_strcasecmp, "ÖÄÅ", "öäå", false); // if it worked, SHOULD match
         TEST_3A(th_strncasecmp, "Aäöå", "aöå", 2, true); // if worked, it should NOT match
-        TEST_2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", false); // should match
+        TEST_3B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", false, false); // should match
     }
 
     //