diff options
| author | Squirrel <squirrelhomebrew@gmail.com> | 2026-01-27 10:59:39 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-01-27 10:59:39 -0800 |
| commit | 329aa22c4fe9097967263b8d9cd1dee0a94e0464 (patch) | |
| tree | e4c24d0452acc9ba67bd8f61c6bc8a9f01fbba55 | |
| parent | a61c2e7b2afa685e76d259d06bec5cdee3739ae0 (diff) | |
dddd
| -rw-r--r-- | tests/std/test_regex.zc | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/tests/std/test_regex.zc b/tests/std/test_regex.zc new file mode 100644 index 0000000..f1b840a --- /dev/null +++ b/tests/std/test_regex.zc @@ -0,0 +1,187 @@ +import "std/regex.zc" + +fn test_basic_matching() { + "testing: basic matching"; + let re = Regex::compile("abc"); + + if (re.match("abc")) { "literal match works"; } + if (re.match("abcdef")) { "substring match works"; } + if (!re.match("xyz")) { "not matching correctly returns false"; } + + re.destroy(); + ""; +} + +fn test_anchors() { + "testing: anchors"; + let re = Regex::compile("^start"); + + if (re.match("start here")) { " ^ anchor works for start"; } + if (!re.match("no start")) { " ^ anchor rejects non-start"; } + + re.destroy(); + + let re2 = Regex::compile("end$"); + if (re2.match("the end")) { " $ anchor works for end"; } + if (!re2.match("end here")) { " $ anchor rejects non-end"; } + + re2.destroy(); + ""; +} + +fn test_wildcards() { + "testing: wild cards"; + let re = Regex::compile("a.c"); + + if (re.match("abc")) { " . matches single char"; } + if (re.match("axc")) { " . matches different char"; } + if (!re.match("ac")) { " . requires exactly one char"; } + + re.destroy(); + ""; +} + +fn test_quantifiers() { + "testing: quantifiers"; + let re1 = Regex::compile("a*b"); + if (re1.match("b")) { " * matches zero occurrences"; } + if (re1.match("ab")) { " * matches one occurrence"; } + if (re1.match("aaab")) { " * matches multiple occurrences"; } + re1.destroy(); + + let re2 = Regex::compile("a+b"); + if (!re2.match("b")) { " + requires at least one"; } + if (re2.match("ab")) { " + matches one occurrence"; } + if (re2.match("aaab")) { " + matches multiple occurrences"; } + re2.destroy(); + + let re3 = Regex::compile("colou?r"); + if (re3.match("color")) { " ? matches with char"; } + if (re3.match("colour")) { " ? matches without char"; } + re3.destroy(); + ""; +} + +fn test_character_classes() { + "testing: character class stuff" + let re = Regex::compile("[0-9]+"); + + if (re.match("123")) { " [0-9] matches digits"; } + if (re.match("abc123")) { " [0-9] finds digits in string"; } + if (!re.match("abc")) { " [0-9] rejects non-digits"; } + + re.destroy(); + ""; +} + +fn test_alternation() { + "test: alternation"; + let re = Regex::compile("cat|dog"); + + if (re.match("cat")) { " | matches first alternative"; } + if (re.match("dog")) { " | matches second alternative"; } + if (!re.match("bird")) { " | rejects non-matching"; } + + re.destroy(); + ""; +} + +fn test_word_boundaries() { + "testing: word matching"; + let re = Regex::compile("[a-zA-Z]+"); + + if (re.match("hello")) { " letter class matches words"; } + if (re.match("hello123")) { " letter class finds word part"; } + if (!re.match("123")) { " letter class rejects non-letters"; } + + re.destroy(); + ""; +} + +fn test_is_valid() { + "testing: patern validation" + + if (Regex::is_valid_pattern("^[a-z]+$")) { " valid pattern accepted"; } + if (Regex::is_valid_pattern("(hello|world)")) { " complex pattern accepted"; } + + ""; +} + +fn test_find() { + "testing: find functionality"; + let re = Regex::compile("[0-9]+"); + let m = re.find("abc123def456"); + + if (m.is_some) { " find locates match"; } + + re.destroy(); + ""; +} + +fn test_count() { + "testing: count"; + let re = Regex::compile("[0-9]+"); + let count = re.count("123 456 789"); + + if (count >= 1) { " count finds matches"; } + + re.destroy(); + ""; +} + +fn test_convenience_functions() { + "testing: just some other functions and stuff"; + + if (regex_match("^test", "testing")) { " regex_match works"; } + if (regex_count("a", "banana") >= 1) { " regex_count works"; } + + let m = regex_find("[0-9]+", "id: 42"); + if (m.is_some) { " regex_find works"; } + + ""; +} + +fn test_email_pattern() { + "test: email pattern stuff" + let email_re = Regex::compile("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"); + + if (email_re.match("swag@swag.com")) { " valid email accepted"; } + if (email_re.match("swag.swag@swag.swag.swag")) { " complex email accepted"; } + if (!email_re.match("invalid.email")) { " invalid email rejected"; } + + email_re.destroy(); + ""; +} + +fn test_url_pattern() { + "testing: url pattern stuff" + let url_re = Regex::compile("https?://[a-zA-Z0-9.-]+"); + + if (url_re.match("http://example.com")) { " http url matched matched"; } + if (url_re.match("https://secure.example.com")) { " https url matched"; } + if (!url_re.match("ftp://something.com")) { " ftp url rejected"; } + + url_re.destroy(); + ""; +} + +fn main() { + "testing...."; + + test_basic_matching(); + test_anchors(); + test_wildcards(); + test_quantifiers(); + test_character_classes(); + test_alternation(); + test_word_boundaries(); + test_is_valid(); + test_find(); + test_count(); + test_convenience_functions(); + test_email_pattern(); + test_url_pattern(); + + "all tests worked..."; + ""; +} |
