def test_type_float(): m = sm.Matcher("{num:float}") assert m.match("123.4") == {"num": 123.4} assert m.match("-123.4") == {"num": -123.4} assert m.match("-123.0") assert m.match("+123.4") == {"num": 123.4} assert m.match("+000123.4") == {"num": 123.4}
def test_type_int(): m = sm.Matcher("{num:int}") assert m.match("123") == {"num": 123} assert m.match("-123") == {"num": -123} assert not m.match("-123.0") assert m.match("+123") == {"num": 123} assert m.match("+000123") == {"num": 123} assert m.match("0123") == {"num": 123}
def test_type_ccard(): m = sm.Matcher("{card:ccard}") assert m.match("4569403961014710") == {"card": "4569403961014710"} assert m.match("5191914942157165") == {"card": "5191914942157165"} assert m.match("370341378581367") == {"card": "370341378581367"} assert m.match("38520000023237") == {"card": "38520000023237"} assert m.match("6011000000000000") == {"card": "6011000000000000"} assert m.match("3566002020360505") == {"card": "3566002020360505"} assert not m.match("1234566660000222")
def test_type_ssn(): m = sm.Matcher("{n:ssn}") assert m.test("123-45-6789") assert not m.test("123 45 6789") assert m.test("333-22-4444") assert not m.test("aaa-bbb-cccc") assert not m.test("900-58-4564") assert not m.test("999-58-4564") assert not m.test("000-45-5454")
def test_type_ipv4(): m = sm.Matcher("{ip:ipv4}") assert m.match("127.0.0.1") == {"ip": "127.0.0.1"} assert m.match("192.168.1.1") == {"ip": "192.168.1.1"} assert m.match("127.0.0.1") == {"ip": "127.0.0.1"} assert m.match("0.0.0.0") == {"ip": "0.0.0.0"} assert m.match("255.255.255.255") == {"ip": "255.255.255.255"} assert not m.match("256.256.256.256") assert not m.match("999.999.999.999") assert not m.match("1.2.3") assert m.match("1.2.3.4") == {"ip": "1.2.3.4"}
def __init__(self, match="*", *, startswith="", contains="", endswith="", case_sensitive=True) -> None: self.matcher = simplematch.Matcher(match, case_sensitive=case_sensitive) self.startswith = self.create_list(startswith, case_sensitive) self.contains = self.create_list(contains, case_sensitive) self.endswith = self.create_list(endswith, case_sensitive) self.case_sensitive = case_sensitive
def test_readme_example_typehints(): matcher = sm.Matcher("{year:int}-{month:int}: {value:float}") assert matcher.match("2021-01: -12.786") == { "year": 2021, "month": 1, "value": -12.786, } assert matcher.match("2021-01-abc: Hello") is None assert matcher.test("1234-01: 123.123") is True assert ( matcher.regex == "^(?P<year>[+-]?[0-9]+)\\-(?P<month>[+-]?[0-9]+):\\ (?P<value>[+-]?(?:[0-9]*[.])?[0-9]+)$" ) assert matcher.converters == {"year": int, "month": int, "value": float}
def test_type_bitcoin(): m = sm.Matcher("{coin:bitcoin}") assert m.match("1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY") == { "coin": "1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY" } assert not m.match("loremipsum") assert m.match("16ftSEQ4ctQFDtVZiUBusQUjRrGhM3JYwe") == { "coin": "16ftSEQ4ctQFDtVZiUBusQUjRrGhM3JYwe" } assert m.match("1EBHA1ckUWzNKN7BMfDwGTx6GKEbADUozX") == { "coin": "1EBHA1ckUWzNKN7BMfDwGTx6GKEbADUozX" } assert not m.match("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae") assert m.match("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq") == { "coin": "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq" }
def test_type_url(): m = sm.Matcher("{url:url}") assert not m.match("abcdef") assert not m.match("www.whatever.com") assert m.match("https://github.com/geongeorge/i-hate-regex") == { "url": "https://github.com/geongeorge/i-hate-regex" } assert m.match("https://www.facebook.com/") == { "url": "https://www.facebook.com/" } assert m.match("https://www.google.com/") == { "url": "https://www.google.com/" } assert m.match("https://xkcd.com/2293/") == { "url": "https://xkcd.com/2293/" } assert not m.match("https://this-shouldn'*****@*****.**") assert m.match("http://www.example.com/") == { "url": "http://www.example.com/" }
def test_manual_specification(): m = sm.Matcher() m.regex = r"^(?P<test>\w+) \d+$" m.converters = {"test": str.upper} assert m.match("hello 123") == {"test": "HELLO"}
def test_type_email(string): matcher = sm.Matcher("{email:email}") assert matcher.test(string) is True assert matcher.match(string) == {"email": string}
def test_type_letter(): m = sm.Matcher("{chars:letters}*") assert m.match("abcf123") == {"chars": "abcf"} assert m.match("abcf123#") == {"chars": "abcf"} assert m.match("ACBAAC_123") == {"chars": "ACBAAC"}