diff --git a/README.md b/README.md index ebaac6c..8409800 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Options per Parser, with possible arguments: * `is_phone_number`: Checks if the string is possibly a phone number. * `fully_matches_regex`: Checks if the string fully matches a given regular expression. * `regex`: [Python-compatible regex](https://docs.python.org/3/howto/regex.html) - * `search_regex`: Checks if the string contains some part of a given regular + * `contains_regex`: Checks if the string contains some part of a given regular expression. * `regex`: [Python-compatible regex](https://docs.python.org/3/howto/regex.html) diff --git a/rules/__init__.py b/rules/__init__.py index 6164257..052e3f6 100644 --- a/rules/__init__.py +++ b/rules/__init__.py @@ -38,7 +38,7 @@ def func_mapper(funcs: List[Callable]) -> Dict[str, Callable]: TextRules: RulesMap[str] = func_mapper([ text.contains_at_sign, text.fully_matches_regex, text.is_email, - text.is_phone_number, text.search_regex + text.is_phone_number, text.contains_regex ]) diff --git a/rules/text.py b/rules/text.py index e059dbc..e054920 100644 --- a/rules/text.py +++ b/rules/text.py @@ -44,10 +44,10 @@ def is_email() -> RuleChecker[str]: pattern = re.compile(regex) def _checker(string: str) -> RuleOutput: - if (pattern.search(string)): - return 'String contains a possible email address.' - else: + if pattern.search(string): return None + else: + return 'String does not resemble an email address.' return _checker @@ -59,16 +59,15 @@ def contains_at_sign() -> RuleChecker[str]: """ def _checker(string: str) -> RuleOutput: - if '@' in string: - return 'String contains the @ character.' - else: return None + else: + return 'String does not contain the @ character.' return _checker -def search_regex(regex: str) -> RuleChecker[str]: +def contains_regex(regex: str) -> RuleChecker[str]: """ Checks if the string contains some part of a given regular expression, i.e. string contains the word "Approved". @@ -77,10 +76,10 @@ def search_regex(regex: str) -> RuleChecker[str]: def _checker(string: str) -> RuleOutput: # - if (pattern.search(string)): - return 'String contains a match for the given pattern.' - else: + if pattern.search(string): return None + else: + return 'String does not contain a match for the given pattern.' return _checker @@ -93,10 +92,10 @@ def fully_matches_regex(regex: str) -> RuleChecker[str]: pattern = re.compile(regex) def _checker(string: str) -> RuleOutput: - if (pattern.fullmatch(string)): - return 'String is a full match for the given pattern.' - else: + if pattern.fullmatch(string): return None + else: + return 'String is not a full match for the given pattern.' return _checker @@ -112,10 +111,10 @@ def _checker(string: str) -> RuleOutput: try: int_number = int(string) if 9 < len(string) < 14 and int_number > 999999: - return 'String contains a possible phone number.' - else: return None + else: + raise ValueError except ValueError: - return None + return 'String does not resemble a phone number.' return _checker diff --git a/test/core/test_config.py b/test/core/test_config.py index 68783ed..3472b0d 100644 --- a/test/core/test_config.py +++ b/test/core/test_config.py @@ -43,7 +43,7 @@ def test_invalid_rule(self): def test_mismatched_rule(self): rules: RulesMap = NumericRules with self.assertRaises(ValueError): - generate_selected_rules([RuleConfig(rule="search_regex")], rules) + generate_selected_rules([RuleConfig(rule="contains_regex")], rules) def test_numeric_rule(self): rules: RulesMap = NumericRules @@ -79,11 +79,11 @@ def test_text_rule(self): def test_text_rule_with_args(self): rules: RulesMap = TextRules generated = generate_selected_rules( - [RuleConfig(rule="search_regex", args={"regex": "testpattern"})], + [RuleConfig(rule="contains_regex", args={"regex": "testpattern"})], rules) generated_rule = generated[0] - self.assertIn('search_regex', generated_rule.__qualname__) + self.assertIn('contains_regex', generated_rule.__qualname__) - self.assertIsNone(generated_rule("otherpattern")) - self.assertIsNotNone(generated_rule("testpattern")) + self.assertIsNotNone(generated_rule("otherpattern")) + self.assertIsNone(generated_rule("testpattern")) diff --git a/test/text/test_contains_at_sign.py b/test/text/test_contains_at_sign.py index 92577d4..91f70c2 100644 --- a/test/text/test_contains_at_sign.py +++ b/test/text/test_contains_at_sign.py @@ -19,7 +19,7 @@ from rules.text import contains_at_sign -class ContainsAtSign(unittest.TestCase): +class ContainsAtSignTest(unittest.TestCase): def setUp(self) -> None: self.rule_checker = contains_at_sign() @@ -29,10 +29,10 @@ def test_returns_callable(self): self.assertTrue(callable(self.rule_checker)) def test_string_with_character(self): - self.assertIsNotNone(self.rule_checker("@")) - self.assertIsNotNone(self.rule_checker("john@doe.nl")) - self.assertIsNotNone(self.rule_checker("john@@")) + self.assertIsNone(self.rule_checker("@")) + self.assertIsNone(self.rule_checker("john@doe.nl")) + self.assertIsNone(self.rule_checker("john@@")) def test_string_without_character(self): - self.assertIsNone(self.rule_checker("johndoe.com")) - self.assertIsNone(self.rule_checker("")) + self.assertIsNotNone(self.rule_checker("johndoe.com")) + self.assertIsNotNone(self.rule_checker("")) diff --git a/test/text/test_search_regex.py b/test/text/test_contains_regex.py similarity index 69% rename from test/text/test_search_regex.py rename to test/text/test_contains_regex.py index 9e9ad84..d9c9297 100644 --- a/test/text/test_search_regex.py +++ b/test/text/test_contains_regex.py @@ -16,27 +16,27 @@ import re import unittest -from rules.text import search_regex +from rules.text import contains_regex -class searchRegex(unittest.TestCase): +class ContainsRegexTest(unittest.TestCase): def setUp(self) -> None: test_regex = r'test' - self.rule_checker = search_regex(test_regex) + self.rule_checker = contains_regex(test_regex) return super().setUp() def test_returns_callable(self): self.assertTrue(callable(self.rule_checker)) def test_contains_regex(self): - self.assertIsNotNone(self.rule_checker('test')) - self.assertIsNotNone(self.rule_checker('test123')) + self.assertIsNone(self.rule_checker('test')) + self.assertIsNone(self.rule_checker('test123')) def test_not_contains_regex(self): - self.assertIsNone(self.rule_checker('tes')) - self.assertIsNone(self.rule_checker('tes1t123')) - self.assertIsNone(self.rule_checker('123')) + self.assertIsNotNone(self.rule_checker('tes')) + self.assertIsNotNone(self.rule_checker('tes1t123')) + self.assertIsNotNone(self.rule_checker('123')) def test_invalid_regex_behaviour(self): # pass invalid regex @@ -44,5 +44,5 @@ def test_invalid_regex_behaviour(self): # assert that error is raised with self.assertRaises(re.error): - rule_checker = search_regex(test_regex) + rule_checker = contains_regex(test_regex) rule_checker('123') diff --git a/test/text/test_fully_matches_regex.py b/test/text/test_fully_matches_regex.py index 7156db3..f0a4480 100644 --- a/test/text/test_fully_matches_regex.py +++ b/test/text/test_fully_matches_regex.py @@ -19,7 +19,7 @@ from rules.text import fully_matches_regex -class fullyMatchesRegex(unittest.TestCase): +class FullyMatchesRegexTest(unittest.TestCase): def setUp(self) -> None: test_regex = r'test' @@ -30,12 +30,12 @@ def test_returns_callable(self): self.assertTrue(callable(self.rule_checker)) def test_contains_regex(self): - self.assertIsNotNone(self.rule_checker('test')) + self.assertIsNone(self.rule_checker('test')) def test_non_matching_regex(self): - self.assertIsNone(self.rule_checker('tes')) - self.assertIsNone(self.rule_checker('tes1t123')) - self.assertIsNone(self.rule_checker('123')) + self.assertIsNotNone(self.rule_checker('tes')) + self.assertIsNotNone(self.rule_checker('tes1t123')) + self.assertIsNotNone(self.rule_checker('123')) def test_invalid_regex_behaviour(self): # pass invalid regex diff --git a/test/text/test_is_email.py b/test/text/test_is_email.py index 95d089e..bb9954f 100644 --- a/test/text/test_is_email.py +++ b/test/text/test_is_email.py @@ -19,7 +19,7 @@ from rules.text import is_email -class IsEmail(unittest.TestCase): +class IsEmailTest(unittest.TestCase): def setUp(self) -> None: self.rule_checker = is_email() @@ -30,69 +30,69 @@ def test_returns_callable(self): def test_valid_emails(self): # standard email addresses - self.assertIsNotNone(self.rule_checker("john@doe.com")) - self.assertIsNotNone(self.rule_checker("john@doe.nl")) - self.assertIsNotNone(self.rule_checker("john@doe.co.uk")) + self.assertIsNone(self.rule_checker("john@doe.com")) + self.assertIsNone(self.rule_checker("john@doe.nl")) + self.assertIsNone(self.rule_checker("john@doe.co.uk")) # dot seperated email name - self.assertIsNotNone(self.rule_checker("john.doe@doe.com")) + self.assertIsNone(self.rule_checker("john.doe@doe.com")) # email with number - self.assertIsNotNone(self.rule_checker("john.doe3@doe.com")) + self.assertIsNone(self.rule_checker("john.doe3@doe.com")) # email with capitals - self.assertIsNotNone(self.rule_checker("john.DOE3@doe.cOm")) + self.assertIsNone(self.rule_checker("john.DOE3@doe.cOm")) # leading space - self.assertIsNotNone(self.rule_checker(" john@doe.com")) + self.assertIsNone(self.rule_checker(" john@doe.com")) # trailing space - self.assertIsNotNone(self.rule_checker("john@doe.com ")) + self.assertIsNone(self.rule_checker("john@doe.com ")) # email in random string - self.assertIsNotNone( + self.assertIsNone( self.rule_checker("asdfasdfsdf john@doe.comdsfkjaslkje8kd")) def test_invalid_characters(self): # quotes - self.assertIsNone(self.rule_checker("john\"@doe.com")) - self.assertIsNone(self.rule_checker("john'@doe.com")) - self.assertIsNone(self.rule_checker("john@doe\".com")) - self.assertIsNone(self.rule_checker("john@do'e.com")) + self.assertIsNotNone(self.rule_checker("john\"@doe.com")) + self.assertIsNotNone(self.rule_checker("john'@doe.com")) + self.assertIsNotNone(self.rule_checker("john@doe\".com")) + self.assertIsNotNone(self.rule_checker("john@do'e.com")) # asterisk - self.assertIsNone(self.rule_checker("john**@doe.com")) - self.assertIsNone(self.rule_checker("john@do*e.com")) + self.assertIsNotNone(self.rule_checker("john**@doe.com")) + self.assertIsNotNone(self.rule_checker("john@do*e.com")) # backslash - # self.assertIsNone(self.rule_checker("jo\hn@doe.com")) - self.assertIsNone(self.rule_checker(r"john@do\e.com")) + # self.assertIsNotNone(self.rule_checker("jo\hn@doe.com")) + self.assertIsNotNone(self.rule_checker(r"john@do\e.com")) def test_invalid_email_structure(self): # missing address sign - self.assertIsNone(self.rule_checker("john.com")) + self.assertIsNotNone(self.rule_checker("john.com")) # double at sign - self.assertIsNone(self.rule_checker("john@@com")) + self.assertIsNotNone(self.rule_checker("john@@com")) # wrongly placed at sign - self.assertIsNone(self.rule_checker("johndoe.co@m")) + self.assertIsNotNone(self.rule_checker("johndoe.co@m")) # leading at sign - self.assertIsNone(self.rule_checker("@john.com")) + self.assertIsNotNone(self.rule_checker("@john.com")) # trailing at sign - self.assertIsNone(self.rule_checker("john.com@")) + self.assertIsNotNone(self.rule_checker("john.com@")) # missing dot - self.assertIsNone(self.rule_checker("john@doecom")) + self.assertIsNotNone(self.rule_checker("john@doecom")) # double dot in domain - self.assertIsNone(self.rule_checker("john@doe..com")) + self.assertIsNotNone(self.rule_checker("john@doe..com")) def test_invalid_input(self): # number string - self.assertIsNone(self.rule_checker('252342')) + self.assertIsNotNone(self.rule_checker('252342')) # character string - self.assertIsNone(self.rule_checker('*#^$*(@($&')) + self.assertIsNotNone(self.rule_checker('*#^$*(@($&')) diff --git a/test/text/test_is_phone_number.py b/test/text/test_is_phone_number.py index f32cb37..f2adeaa 100644 --- a/test/text/test_is_phone_number.py +++ b/test/text/test_is_phone_number.py @@ -126,7 +126,7 @@ # yapf: enable -class IsPhoneNumber(unittest.TestCase): +class IsPhoneNumberTest(unittest.TestCase): def setUp(self) -> None: self.rule_checker = is_phone_number() @@ -138,10 +138,10 @@ def test_returns_callable(self): def test_phone_numbers(self): # standard phone number for number in random_phone_numbers: - self.assertIsNotNone(self.rule_checker(number)) + self.assertIsNone(self.rule_checker(number)) def test_non_phone_numbers(self): # standard phone number - self.assertIsNone(self.rule_checker('test')) - self.assertIsNone(self.rule_checker('23598734928057034978')) - self.assertIsNone(self.rule_checker('00000000000')) + self.assertIsNotNone(self.rule_checker('test')) + self.assertIsNotNone(self.rule_checker('23598734928057034978')) + self.assertIsNotNone(self.rule_checker('00000000000'))