


## regex-unit-test.pkg
# Compiled by:
# src/lib/test/unit-tests.lib# Run by:
# src/lib/test/all-unit-tests.pkgpackage regex_unit_test {
include unit_test; # unit_test is from src/lib/src/unit-test.pkg include makelib::scripting_globals;
name = "src/lib/regex/regex-unit-test.pkg";
fun run ()
=
{
printf "\nDoing %s:\n" name;
# These two come from the library reference page:
#
assert (("abab" =~ ./^(.+)\1$/) == TRUE);
assert (("abba" =~ ./^(.+)\1$/) == FALSE);
# This group is harvested from the Full Monte regex tutorial:
assert ((regex::find_first ./f.t/ "the fat father futzed") == "fat");
assert ((regex::find_all ./f.t/ "the fat father futzed") == ["fat", "fat", "fut"]);
assert ((regex::find_all ./\b\w+\b/ "the fat father futzed") == ["the", "fat", "father", "futzed"]);
assert(((regex::find_first_groups_all ./f.q/ "the fat father futzed") except NOT_FOUND = []) == []);
assert ((regex::find_first_groups_all ./f.t/ "the fat father futzed") == []);
assert ((regex::find_first_groups_all ./(f)(.)(t)/ "the fat father futzed") == ["f", "a", "t"]);
assert ((regex::find_first_groups_all ./((f(.))t)/ "the fat father futzed") == ["fat", "fa", "a"]);
assert ((regex::find_first_group 2 ./(f)(.)(t)/ "the fat father futzed") == "a");
assert ((regex::find_first_group 0 ./(f)(.)(t)/ "the fat father futzed") == "fat");
assert ((regex::find_first_group 1 ./(f)(.)(t)/ "the fat father futzed") == "f");
assert ((regex::find_first_group 2 ./(f)(.)(t)/ "the fat father futzed") == "a");
assert ((regex::find_first_group 3 ./(f)(.)(t)/ "the fat father futzed") == "t");
assert ((regex::find_all_group 2 ./(f)(.)(t)/ "the fat father futzed") == ["a", "a", "u"]);
assert ((regex::replace_all ./f.t/ "FAT" "the fat father futzed") == "the FAT FATher FATzed");
assert ((regex::replace_first ./f.t/ "FAT" "the fat father futzed") == "the FAT father futzed");
assert ( (regex::replace_first_via_fn ./(f.t)/ .{ toupper(strcat(#stringlist)); } "the fat father futzed")
==
"the FAT father futzed"
);
assert ( (regex::replace_all_via_fn ./(f.t)/ .{ toupper(strcat(#stringlist)); } "the fat father futzed")
==
"the FAT FATher FUTzed"
);
fun diagnose target_text
=
regex::regex_case
target_text
{ cases => [ (./utilize/, fn _ = sprintf "This guy is verbose!" ),
(./weaponize/, fn _ = sprintf "This guy is from the Pentagon!" ),
(./(\b[bcdfghjklmnpqrstvwxz]+\b)/, fn strings = sprintf "What is this '%s' word?!" (strcat strings) )
],
default => fn _ = sprintf "I can deduce nothing."
};
assert ((diagnose "We must utilize our utmost efforts.") == "This guy is verbose!");
assert ((diagnose "We must weaponize the chalkboards." ) == "This guy is from the Pentagon!");
assert ((diagnose "The crwth is revolting!" ) == "What is this 'crwth' word?!");
assert ((diagnose "We are the people!" ) == "I can deduce nothing.");
# This is the pair Jeffrey Lau reported on
# Wed, 7 Oct 2009 12:26:02 +0800:
# the first was working but the second was hanging
# src/lib/regex/backend/perl-regex-engine-g.pkg # in an infinite loop:
#
assert ("abbccd" =~ ./^a(.*)d$/);
assert ("abbccd" =~ ./^a(.*)*d$)/);
assert ("" =~ ./(.?)*/); # Miminal stimulus for this bug.
# Allen Leung's excellent and admirable
# original perl-regex-engine-g.pkg code
# did not have $ match \n at end of string
# per Perl5 spec:
#
assert ("abc\n" =~ ./^abc$/);
summarize_unit_tests name;
};
};


