예제 #1
0
def get_examples(regex, count=1, dictionary=None):
    '''
    Generate representative, readable examples 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
    example_visitor = ExampleVisitor(dictionary)

    examples = []
    state_permutations = tree.get_state_permutations()
    random.shuffle(state_permutations)
    for perm in state_permutations[:count]:
        tree.set_state(perm)
        example = example_visitor.visit(tree)
        examples.append(example)

    return examples
예제 #2
0
 def test_category_space_node(self):
     tree = parse_regex('\s')
     cat_node = tree.root.children[0].children[0]
     self.assertEqual(type(cat_node), CategoryNode)
     self.assertEqual(cat_node.classname, "space")
예제 #3
0
 def test_any_node(self):
     tree = parse_regex('.')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), AnyNode)
예제 #4
0
 def test_character_node(self):
     tree = parse_regex('[a-z]')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), InNode)
예제 #5
0
 def test_character_node(self):
     tree = parse_regex('[a-z]')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), InNode)
예제 #6
0
 def test_in_node_negated(self):
     tree = parse_regex('[^a-z]')
     in_node = self._get_first_child(tree)
     self.assertTrue(in_node.negated)
예제 #7
0
 def test_literal_node(self):
     tree = parse_regex('abc')
     child = self._get_first_child(tree)
     self.assertEqual(len(tree.root.children), 3)
     self.assertEqual(type(child), LiteralNode)
예제 #8
0
 def test_repeat_count_is_none_by_default(self):
     tree = parse_regex('a{3}')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), RepeatNode)
     self.assertIsNone(child.repetitions)
예제 #9
0
 def test_branch_node(self):
     ''' This one is tricky -- we have to get all 'or's on the same level '''
     tree = parse_regex('abra|kadabra')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), BranchNode)
     self.assertEqual(len(child.children), 2)
예제 #10
0
 def test_repeat_count_is_none_by_default(self):
     tree = parse_regex('a{3}')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), RepeatNode)
     self.assertIsNone(child.repetitions)
예제 #11
0
 def test_repeat_range(self):
     tree = parse_regex('a{3,5}')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), RepeatNode)
     self.assertEqual(child.min_repeat, 3)
     self.assertEqual(child.max_repeat, 5)
예제 #12
0
 def test_repeat_asterisk_node(self):
     tree = parse_regex('a*')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), RepeatNode)
     self.assertEqual(child.min_repeat, 0)
예제 #13
0
 def test_category_space_node(self):
     tree = parse_regex('\s')
     cat_node = tree.root.children[0].children[0]
     self.assertEqual(type(cat_node), CategoryNode)
     self.assertEqual(cat_node.classname, "space")
예제 #14
0
 def test_any_node(self):
     tree = parse_regex('.')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), AnyNode)
예제 #15
0
 def test_repeat_asterisk_node(self):
     tree = parse_regex('a*')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), RepeatNode)
     self.assertEqual(child.min_repeat, 0)
예제 #16
0
 def test_repeat_range(self):
     tree = parse_regex('a{3,5}')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), RepeatNode)
     self.assertEqual(child.min_repeat, 3)
     self.assertEqual(child.max_repeat, 5)
예제 #17
0
 def test_literal_node(self):
     tree = parse_regex('abc')
     child = self._get_first_child(tree)
     self.assertEqual(len(tree.root.children), 3)
     self.assertEqual(type(child), LiteralNode)
예제 #18
0
 def test_branch_node(self):
     ''' This one is tricky -- we have to get all 'or's on the same level '''
     tree = parse_regex('abra|kadabra')
     child = self._get_first_child(tree)
     self.assertEqual(type(child), BranchNode)
     self.assertEqual(len(child.children), 2)
예제 #19
0
 def test_range_node(self):
     tree = parse_regex('[a-z]')
     rng_node = tree.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'))
예제 #20
0
 def test_range_node(self):
     tree = parse_regex('[a-z]')
     rng_node = tree.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'))
예제 #21
0
 def test_in_node_negated(self):
     tree = parse_regex('[^a-z]')
     in_node = self._get_first_child(tree)
     self.assertTrue(in_node.negated)
예제 #22
0
 def test_parse_repeating_literal(self):
     tree = parse_regex('a+')
     rpt_node = tree.root.children[0]
     self.assertEqual(type(rpt_node), RepeatNode)
     lit_node = rpt_node.children[0]
     self.assertEqual(type(lit_node), LiteralNode)
예제 #23
0
 def test_parse_repeating_literal(self):
     tree = parse_regex('a+')
     rpt_node = tree.root.children[0]
     self.assertEqual(type(rpt_node), RepeatNode)
     lit_node = rpt_node.children[0]
     self.assertEqual(type(lit_node), LiteralNode)