## 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 package unit_test; # unit_test is from
src/lib/src/unit-test.pkg include package 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 (the (regex::find_first_match_to_regex ./f.t/ "the fat father futzed") == "fat");
assert ( (regex::find_all_matches_to_regex ./f.t/ "the fat father futzed") == ["fat", "fat", "fut"]);
assert ( (regex::find_all_matches_to_regex ./\b\w+\b/ "the fat father futzed") == ["the", "fat", "father", "futzed"]);
assert ( (regex::find_first_match_to_regex_and_return_all_groups ./f.q/ "the fat father futzed") == NULL);
assert (the (regex::find_first_match_to_regex_and_return_all_groups ./f.t/ "the fat father futzed") == []);
assert (the (regex::find_first_match_to_regex_and_return_all_groups ./(f)(.)(t)/ "the fat father futzed") == ["f", "a", "t"]);
assert (the (regex::find_first_match_to_regex_and_return_all_groups ./((f(.))t)/ "the fat father futzed") == ["fat", "fa", "a"]);
assert (the (regex::find_first_match_to_ith_group 2 ./(f)(.)(t)/ "the fat father futzed") == "a");
assert (the (regex::find_first_match_to_ith_group 0 ./(f)(.)(t)/ "the fat father futzed") == "fat");
assert (the (regex::find_first_match_to_ith_group 1 ./(f)(.)(t)/ "the fat father futzed") == "f");
assert (the (regex::find_first_match_to_ith_group 2 ./(f)(.)(t)/ "the fat father futzed") == "a");
assert (the (regex::find_first_match_to_ith_group 3 ./(f)(.)(t)/ "the fat father futzed") == "t");
assert ( (regex::find_all_matches_to_regex_and_return_values_of_ith_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/, \\ _ = sprintf "This guy is verbose!" ),
(./weaponize/, \\ _ = sprintf "This guy is from the Pentagon!" ),
(./(\b[bcdfghjklmnpqrstvwxz]+\b)/, \\ strings = sprintf "What is this '%s' word?!" (strcat strings) )
],
default => \\ _ = 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;
};
};