class TestRegex(unittest.TestCase): def setUp(self): """Inicia novo objeto em todo os testes. """ self.regex = Regex("[A-z]") def test_eq(self): """Verifica se o método __eq__ funciona. """ self.assertTrue(self.regex == "[A-z]") def test_iadd(self): """Verifica se o método __iadd__ funciona. """ self.regex += "{3}" self.assertTrue(self.regex == "[A-z]{3}") def test_set(self): """Verifica se o método _set_regex funciona. """ self.regex._set_regex("[0-9]{5}") self.assertTrue(self.regex == "[0-9]{5}") def test_add(self): """Verifica se o método __add__ funciona. """ self.regex = Regex("[0-9]{5}") + "-" + Regex("[0-9]{3}") self.assertTrue(self.regex == "[0-9]{5}-[0-9]{3}")
def test_reference_named_group(self): named_group_tag = Regex('<', Group('h[1-6]').name('tag'), '>') reference_named_group = Regex('</', Group().backreference_named('tag'), '>') regex = Group(named_group_tag, non_capturing=True) \ + self.group_value \ + Group(reference_named_group, non_capturing=True) self.assertTrue( regex == '(?:<(?P<tag>h[1-6])>)([\\w\\s]+)(?:</(?P=tag)>)')
def test_Reference(self): """Verifica se o regex para a tag <h1> com non-capturing e referência funciona. """ group_tag = Regex('<', Group('h[1-6]'), '>') group_ref = \ Group(group_tag, non_capturing=True) \ + self.group_value \ + Group(Regex('<', Regex.SLASH, 1, '>'), non_capturing=True) self.assertTrue(group_ref == "(?:<(h[1-6])>)([\w\s]+)(?:<\/\\1>)")
def test_letters(self): self.assertTrue( Regex('[A-z]') == Set(Range().letters(uppercase=True, lowercase=True))) self.assertTrue( Regex('[A-Z]') == Set(Range().letters(uppercase=True, lowercase=False))) self.assertTrue( Regex('[a-z]') == Set(Range().letters(uppercase=False, lowercase=True))) self.assertTrue( Regex('[A-z]') == Set(Range().letters(uppercase=False, lowercase=False)))
def test_CPF(self): all_digits = Set(Range(0, 9)) dot = Regex('.').quantifier(0, 1) hyphen = Regex('-').quantifier(0, 1) rgx_cpf = Regex( all_digits.quantifier(3), dot, all_digits.quantifier(3), dot, all_digits.quantifier(3), hyphen, all_digits.quantifier(2), ) self.assertTrue(rgx_cpf == '[0-9]{3}.?[0-9]{3}.?[0-9]{3}-?[0-9]{2}')
def test_set_comment(self): # All digits digits = Set(Regex('0-9')) # Add comment digits = digits.comment('Get all digits') self.assertTrue(digits == '[0-9](?#Get all digits)')
class TestQuantifier(unittest.TestCase): def setUp(self): """Inicia novo objeto em todo os testes. """ self.digits = Regex("[0-9]") def test_Quantifier_One(self): """Verifica se o regex com Quantifier n=1 funciona. """ self.assertTrue((self.digits + Quantifier(n=1)) == "[0-9]{1}") def test_Quantifier_One_Five(self): """Verifica se o regex com Quantifier n=1 e m=5 funciona. """ self.assertTrue((self.digits + Quantifier(n=1, m=5)) == "[0-9]{1,5}") def test_Quantifier_Two_WithoutMaximum(self): """Verifica se o regex com Quantifier n=2 e without_maximum=True funciona. """ self.assertTrue((self.digits + Quantifier(n=2, without_maximum=True)) == "[0-9]{2,}") def test_CEP(self): """Verifica se o regex de CEP funciona. """ test_cep = (self.digits + Quantifier(n=5)) + "-" + (self.digits + Quantifier(n=3)) self.assertTrue(test_cep == "[0-9]{5}-[0-9]{3}") def test_Quantifier_WithoutMaximum(self): """Verifica se o regex com Quantifier n=2 e without_maximum=True funciona. """ self.assertTrue( self.digits.quantifier(without_maximum=True) == "[0-9]*")
def test_Set_comment(self): # All digits digits = Set(Regex("0-9")) # Add comment digits = digits.comment('Get all digits') self.assertTrue(digits == "[0-9](?#Get all digits)")
def test_CPF(self): """Verifica se os modos de criar uma máscara para CPF funcionam. CPF [0-9]{3}.?[0-9]{3}.?[0-9]{3}-?[0-9]{2} """ self.assertTrue(self.regex == Set(Range().digits())) all_digits = Set(Range(0, 9)) dot = Regex('.').quantifier(0, 1) hyphen = Regex('-').quantifier(0, 1) rgx_cpf = Regex( all_digits.quantifier(3), dot, all_digits.quantifier(3), dot, all_digits.quantifier(3), hyphen, all_digits.quantifier(2), ) self.assertTrue(rgx_cpf == "[0-9]{3}.?[0-9]{3}.?[0-9]{3}-?[0-9]{2}")
def test_comment_method(self): # All digits digits = Regex('[0-9]') # CEP regex rgx_cep = Regex( digits.quantifier(5), Regex('-').quantifier(0, 1), digits.quantifier(3), ).comment('Get zip code Brazil on input') self.assertTrue(rgx_cep == '[0-9]{5}-?[0-9]{3}(?#Get zip code Brazil on input)')
def test_Comment_Method(self): """Tests the comment method. """ # All digits digits = Regex("[0-9]") # CEP regex rgx_cep = Regex( digits.quantifier(5), Regex("-").quantifier(0, 1), digits.quantifier(3)).comment('Get zip code Brazil on input') self.assertTrue( rgx_cep == "[0-9]{5}-?[0-9]{3}(?#Get zip code Brazil on input)")
class TestQuantifier(TestCase): def setUp(self): self.digits = Regex('[0-9]') def test_quantifier_one(self): self.assertTrue((self.digits + Quantifier(n=1)) == '[0-9]{1}') def test_quantifier_one_five(self): self.assertTrue((self.digits + Quantifier(n=1, m=5)) == '[0-9]{1,5}') def test_quantifier_two_without_maximum(self): self.assertTrue((self.digits + Quantifier(n=2, without_maximum=True)) == '[0-9]{2,}') def test_CEP(self): test_cep = (self.digits + Quantifier(n=5)) + '-' + (self.digits + Quantifier(n=3)) self.assertTrue(test_cep == '[0-9]{5}-[0-9]{3}') def test_quantifier_without_maximum(self): self.assertTrue(self.digits.quantifier(without_maximum=True) == '[0-9]*')
def test_Comment_Class(self): """Tests the Comment class. """ # All digits digits = Regex("[0-9]") # CEP comment cep_comment = Comment('Get zip code Brazil on input') # CEP regex rgx_cep = Regex(digits.quantifier(5), Regex("-").quantifier(0, 1), digits.quantifier(3), cep_comment) self.assertTrue( rgx_cep == "[0-9]{5}-?[0-9]{3}(?#Get zip code Brazil on input)")
def setUp(self): self.digits = Regex('[0-9]')
def test_iadd(self): regex = Regex('[A-z]') regex += '{3}' self.assertTrue(regex == '[A-z]{3}')
from mre import Regex, Set, Group # # group + group group_value = Group( Set(Regex.WORD_CHARS + Regex.WHITESPACE).quantifier(n=1, without_maximum=True)) groups = \ Group("<h1>") \ + group_value \ + Group("</h1>") # tag_h_one = "<h1>Hello World</h1>" assert groups == "(<h1>)([\w\s]+)(</h1>)" # group + group with non-capturing groups_nc = \ Group("<h1>", non_capturing=True) \ + group_value \ + Group("</h1>", non_capturing=True) assert groups_nc == "(?:<h1>)([\w\s]+)(?:</h1>)" group_tag = Regex('<', Group('h[1-6]'), '>') group_ref = \ Group(group_tag, non_capturing=True) \ + group_value \ + Group(Regex('<', Regex.SLASH, 1, '>'), non_capturing=True) assert group_ref == "(?:<(h[1-6])>)([\w\s]+)(?:<\/\\1>)"
from mre import Regex, Set from mre.helper import Range digits_one = Regex("[0-9]") digits_two = Set(Range().digits()) # [0-9] assert digits_one == digits_two # CPF [0-9]{3}.?[0-9]{3}.?[0-9]{3}-?[0-9]{2} all_digits = Set(Range(0, 9)) dot = Regex('.').quantifier(0, 1) hyphen = Regex('-').quantifier(0, 1) rgx_cpf = Regex( all_digits.quantifier(3), dot, all_digits.quantifier(3), dot, all_digits.quantifier(3), hyphen, all_digits.quantifier(2), ) assert rgx_cpf == "[0-9]{3}.?[0-9]{3}.?[0-9]{3}-?[0-9]{2}"
def test_init(self): regex = RegexBase(Regex('[A-z]')) self.assertTrue(regex == '[A-z]')
from mre import Regex, Quantifier, Anchor regex = Regex(Quantifier(Regex.DIGIT, 4), '-', Quantifier(Regex.WORD_CHARS, 1, without_maximum=True), '.txt') rgx_anchor = Anchor(regex) assert rgx_anchor == "^\\d{4}-\\w+.txt$" rgx_anchor_negate = Anchor(regex, negate=True) assert rgx_anchor_negate == "\\b\\d{4}-\\w+.txt\\B"
from mre import Regex, Quantifier, Set digits = Regex("0-9") cep = Set(digits).quantifier(5) + Quantifier("-", 0, 1) + Set(digits).quantifier(3) assert cep == "[0-9]{5}-?[0-9]{3}" cep = Set(digits).quantifier(5) + Set(digits).quantifier(3) assert cep == "[0-9]{5}[0-9]{3}"
from mre import Regex, Quantifier digits = Regex("[0-9]") # n = 1 assert (digits + Quantifier(n=1)) == "[0-9]{1}" # n = 1, m = 5 assert (digits + Quantifier(n=1, m=5)) == "[0-9]{1,5}" # n = 2, at_minimum = True assert (digits + Quantifier(n=2, without_maximum=True)) == "[0-9]{2,}" # CEP test_cep = (digits + Quantifier(n=5)) + "-" + (digits + Quantifier(n=3)) assert test_cep == "[0-9]{5}-[0-9]{3}" # quantifier method assert Regex("[0-9]").quantifier(without_maximum=True) == "[0-9]*"
def test_set(self): regex = Regex('[A-z]') regex._set_regex('[0-9]{5}') self.assertTrue(regex == '[0-9]{5}')
def test_add(self): regex = Regex('[0-9]{5}') + '-' + Regex('[0-9]{3}') self.assertTrue(regex == '[0-9]{5}-[0-9]{3}')
def setUp(self): """Inicia novo objeto em todo os testes. """ self.regex = Regex( Quantifier(Regex.DIGIT, 4), '-', Quantifier(Regex.WORD_CHARS, 1, without_maximum=True), '.txt')
def setUp(self): chars = Quantifier(Regex.WORD_CHARS, 1, without_maximum=True) self.regex = Regex(Quantifier(Regex.DIGIT, 4), '-', chars, '.txt')
def test_positive_lookahead_regex(self): lookahead = self.regex + Lookahead(Regex('Test')) self.assertTrue(lookahead == '[A-z](?=Test)')
def setUp(self): """Inicia novo objeto em todo os testes. """ self.digits = Regex("[0-9]")
def setUp(self): self.regex = Regex('[A-z]')
def test_str(self): regex = Regex('[A-z]') string_print = regex.__str__() self.assertTrue(string_print == '[A-z]')
def test_eq(self): self.assertTrue(Regex('[A-z]') == '[A-z]')