Skip to content

Commit 4ac82e6

Browse files
authored
Merge pull request #206 from PabloLec/main
Test regex names capitalization
2 parents 669682c + 55b3465 commit 4ac82e6

File tree

4 files changed

+60
-39
lines changed

4 files changed

+60
-39
lines changed

pywhat/Data/regex.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@
15751575
]
15761576
},
15771577
{
1578-
"Name": "Amazon Web Services Organization identifier",
1578+
"Name": "Amazon Web Services Organization Identifier",
15791579
"Regex": "^(o-[a-z0-9]{10,32})$",
15801580
"plural_name": false,
15811581
"Description": null,
@@ -1746,7 +1746,7 @@
17461746
]
17471747
},
17481748
{
1749-
"Name": "Amazon Web Services EC2 Instance identifier",
1749+
"Name": "Amazon Web Services EC2 Instance Identifier",
17501750
"Regex": "(?i)^(\\b[a-z]+-[a-z0-9]+)$",
17511751
"plural_name": false,
17521752
"Description": null,

tests/test_identifier.py

-16
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@
77
r = identifier.Identifier()
88

99

10-
def test_check_keys_in_json():
11-
database = load_regexes()
12-
13-
for entry in database:
14-
keys = list(entry.keys())
15-
entry_name = entry["Name"]
16-
17-
assert "Name" in keys, entry_name
18-
assert "Regex" in keys, entry_name
19-
assert "plural_name" in keys, entry_name
20-
assert "Description" in keys, entry_name
21-
assert "Rarity" in keys, entry_name
22-
assert "URL" in keys, entry_name
23-
assert "Tags" in keys, entry_name
24-
25-
2610
def test_identifier_works():
2711
out = r.identify("DANHz6EQVoWyZ9rER56DwTXHWUxfkv9k2o")
2812
assert (

tests/test_regex_formatting.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import re
2+
3+
from pywhat.helper import load_regexes
4+
5+
database = load_regexes()
6+
7+
8+
def test_name_capitalization():
9+
for entry in database:
10+
entry_name = entry["Name"]
11+
for word in entry_name.split():
12+
upper_and_num_count = sum(1 for c in word if c.isupper() or c.isnumeric())
13+
if upper_and_num_count > 0:
14+
continue
15+
cleaned_word = word.translate({ord(c): None for c in "(),."})
16+
if cleaned_word in ["a", "of", "etc"]:
17+
continue
18+
19+
assert word.title() == word, (
20+
f'Wrong capitalization in regex name: "{entry_name}"\n'
21+
f'Expected: "{entry_name.title()}"\n'
22+
"Please capitalize every the first letter of each word."
23+
)
24+
25+
26+
def test_regex_format():
27+
for regex in database:
28+
assert re.findall(
29+
r"^(?:\(\?i\))?\^\(.*\)\$$", regex["Regex"]
30+
), r"Please use ^(regex)$ regex format. If there is '\n' character, you have to escape it. If there is '(?i)', it is allowed and should be before the '^'."
31+
32+
assert (
33+
re.findall(r"\^\||\|\^|\$\|\^|\$\||\|\$", regex["Regex"]) == []
34+
), "Remove in-between boundaries. For example, '^|$' should only be '|'."
35+
36+
37+
def test_check_keys():
38+
for entry in database:
39+
keys = list(entry.keys())
40+
entry_name = entry["Name"]
41+
42+
assert "Name" in keys, entry_name
43+
assert "Regex" in keys, entry_name
44+
assert "plural_name" in keys, entry_name
45+
assert "Description" in keys, entry_name
46+
assert "Rarity" in keys, entry_name
47+
assert "URL" in keys, entry_name
48+
assert "Tags" in keys, entry_name
49+
50+
51+
def test_sorted_by_rarity():
52+
rarity_num = [regex["Rarity"] for regex in database]
53+
54+
assert rarity_num == sorted(
55+
rarity_num, reverse=True
56+
), "Regexes should be sorted by rarity in 'regex.json'. Regexes with rarity '1' are at the top of the file and '0' is at the bottom."

tests/test_regex_identifier.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,6 @@ def test_if_all_tests_exist():
4242
), "No test for this regex found in 'test_regex_identifier.py'. Note that a test needs to assert the whole name."
4343

4444

45-
def test_regex_format():
46-
for regex in database:
47-
assert re.findall(
48-
r"^(?:\(\?i\))?\^\(.*\)\$$", regex["Regex"]
49-
), r"Please use ^(regex)$ regex format. If there is '\n' character, you have to escape it. If there is '(?i)', it is allowed and should be before the '^'."
50-
51-
assert (
52-
re.findall(r"\^\||\|\^|\$\|\^|\$\||\|\$", regex["Regex"]) == []
53-
), "Remove in-between boundaries. For example, '^|$' should only be '|'."
54-
55-
56-
def test_sorted_by_rarity():
57-
rarity_num = [regex["Rarity"] for regex in database]
58-
59-
assert rarity_num == sorted(
60-
rarity_num, reverse=True
61-
), "Regexes should be sorted by rarity in 'regex.json'. Regexes with rarity '1' are at the top of the file and '0' is at the bottom."
62-
63-
6445
def test_dogecoin():
6546
res = r.check(["DANHz6EQVoWyZ9rER56DwTXHWUxfkv9k2o"])
6647
_assert_match_first_item("Dogecoin (DOGE) Wallet Address", res)
@@ -664,12 +645,12 @@ def test_aws_secret_access_key():
664645

665646
def test_aws_ec2_id():
666647
res = r.check(["i-1234567890abcdef0"])
667-
assert "Amazon Web Services EC2 Instance identifier" in str(res)
648+
assert "Amazon Web Services EC2 Instance Identifier" in str(res)
668649

669650

670651
def test_aws_org_id():
671652
res = r.check(["o-aa111bb222"])
672-
assert "Amazon Web Services Organization identifier" in str(res)
653+
assert "Amazon Web Services Organization Identifier" in str(res)
673654

674655

675656
def test_aws_sns():

0 commit comments

Comments
 (0)