101 lines
2.8 KiB
Python
101 lines
2.8 KiB
Python
"""
|
|
Test check andling for regex checks
|
|
"""
|
|
|
|
from corelibs_text_colors.text_colors import Colors
|
|
from corelibs.check_handling.regex_constants import (
|
|
compile_re, DOMAIN_WITH_LOCALHOST_REGEX, EMAIL_BASIC_REGEX, NAME_EMAIL_BASIC_REGEX, SUB_EMAIL_BASIC_REGEX
|
|
)
|
|
|
|
NAME_EMAIL_SIMPLE_REGEX = r"""
|
|
^\s*(?:"(?P<name1>[^"]+)"\s*<(?P<email1>[^>]+)>|
|
|
(?P<name2>.+?)\s*<(?P<email2>[^>]+)>|
|
|
<(?P<email3>[^>]+)>|
|
|
(?P<email4>[^\s<>]+))\s*$
|
|
"""
|
|
|
|
|
|
def domain_test():
|
|
"""
|
|
domain regex test
|
|
"""
|
|
print("=" * 30)
|
|
test_domains = [
|
|
"example.com",
|
|
"localhost",
|
|
"subdomain.localhost",
|
|
"test.localhost.com",
|
|
"some-domain.org"
|
|
]
|
|
|
|
regex_domain_check = compile_re(DOMAIN_WITH_LOCALHOST_REGEX)
|
|
print(f"REGEX: {DOMAIN_WITH_LOCALHOST_REGEX}")
|
|
print(f"Check regex: {regex_domain_check.search('localhost')}")
|
|
|
|
for domain in test_domains:
|
|
if regex_domain_check.search(domain):
|
|
print(f"Matched: {domain}")
|
|
else:
|
|
print(f"Did not match: {domain}")
|
|
|
|
|
|
def email_test():
|
|
"""
|
|
email regex test
|
|
"""
|
|
print("=" * 30)
|
|
email_list = """
|
|
e@bar.com
|
|
<f@foobar.com>
|
|
"Master" <foobar@bar.com>
|
|
"not valid" not@valid.com
|
|
also not valid not@valid.com
|
|
some header <something@bar.com>
|
|
test master <master@master.com>
|
|
日本語 <japan@jp.net>
|
|
"ひほん カケ苦" <foo@bar.com>
|
|
single@entry.com
|
|
arsch@popsch.com
|
|
test open <open@open.com>
|
|
"""
|
|
|
|
basic_email = compile_re(EMAIL_BASIC_REGEX)
|
|
sub_basic_email = compile_re(SUB_EMAIL_BASIC_REGEX)
|
|
simple_name_email_regex = compile_re(NAME_EMAIL_SIMPLE_REGEX)
|
|
full_name_email_regex = compile_re(NAME_EMAIL_BASIC_REGEX)
|
|
for email in email_list.splitlines():
|
|
email = email.strip()
|
|
if not email:
|
|
continue
|
|
print(f">>> Testing: {email}")
|
|
if not basic_email.match(email):
|
|
print(f"{Colors.red}[EMAIL ] No match: {email}{Colors.reset}")
|
|
else:
|
|
print(f"{Colors.green}[EMAIL ] Matched : {email}{Colors.reset}")
|
|
if not sub_basic_email.match(email):
|
|
print(f"{Colors.red}[SUB ] No match: {email}{Colors.reset}")
|
|
else:
|
|
print(f"{Colors.green}[SUB ] Matched : {email}{Colors.reset}")
|
|
if not simple_name_email_regex.match(email):
|
|
print(f"{Colors.red}[SIMPLE] No match: {email}{Colors.reset}")
|
|
else:
|
|
print(f"{Colors.green}[SIMPLE] Matched : {email}{Colors.reset}")
|
|
if not full_name_email_regex.match(email):
|
|
print(f"{Colors.red}[FULL ] No match: {email}{Colors.reset}")
|
|
else:
|
|
print(f"{Colors.green}[FULL ] Matched : {email}{Colors.reset}")
|
|
|
|
|
|
def main():
|
|
"""
|
|
Test regex checks
|
|
"""
|
|
domain_test()
|
|
email_test()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# __END__
|