Ejemplo n.º 1
0
def urtext(regex, dictionary=None):
    '''
    Generate representative, readable example of string that matches a regular expression.
    If dictionary is set to None, use the default dictionary.
    '''
    tree = regex_parse.parse_regex(regex)
    dictionary = get_default_dict() if dictionary is None else dictionary
    urtext_visitor = UrtextVisitor(dictionary)
    message = urtext_visitor.visit(tree)
    return message
Ejemplo n.º 2
0
 def test_in_node_negated(self):
     root = parse_regex('[^a-z]')
     in_node = root.children[0]
     self.assertTrue(in_node.negated)
Ejemplo n.º 3
0
 def test_parse_repeating_literal(self):
     root = parse_regex('a+')
     rpt_node = root.children[0]
     self.assertEqual(type(rpt_node), RepeatNode)
     lit_node = rpt_node.children[0]
     self.assertEqual(type(lit_node), LiteralNode)
Ejemplo n.º 4
0
 def test_literal_node(self):
     root = parse_regex('abc')
     child = self._get_first_child(root)
     self.assertEqual(len(root.children), 3)
     self.assertEqual(type(child), LiteralNode)
Ejemplo n.º 5
0
 def test_range_node(self):
     root = parse_regex('[a-z]')
     rng_node = root.children[0].children[0]
     self.assertEqual(type(rng_node), RangeNode)
     self.assertEqual(rng_node.lo, ord('a'))
     self.assertEqual(rng_node.hi, ord('z'))
Ejemplo n.º 6
0
 def test_branch_node(self):
     ''' This one is tricky -- we have to get all 'or's on the same level '''
     root = parse_regex('abra|kadabra')
     child = self._get_first_child(root)
     self.assertEqual(type(child), BranchNode)
     self.assertEqual(len(child.children), 2)
Ejemplo n.º 7
0
 def test_repeat_count(self):
     root = parse_regex('a{3}')
     child = self._get_first_child(root)
     self.assertEqual(type(child), RepeatNode)
     self.assertEqual(child.repetitions, 3)
Ejemplo n.º 8
0
 def test_repeat_asterisk_node(self):
     root = parse_regex('a*')
     child = self._get_first_child(root)
     self.assertEqual(type(child), RepeatNode)
     self.assertEqual(child.min_repeat, 0)
Ejemplo n.º 9
0
 def test_category_space_node(self):
     root = parse_regex('\s')
     cat_node = root.children[0].children[0]
     self.assertEqual(type(cat_node), CategoryNode)
     self.assertEqual(cat_node.classname, "space")
Ejemplo n.º 10
0
 def test_character_node(self):
     root = parse_regex('[a-z]')
     child = self._get_first_child(root)
     self.assertEqual(type(child), InNode)